Tengo un problema, miren tengo un programa que se encarga de agregar los directorios de los archivos a una base de datos. lo hago seleccionando un directorio con un dirlistbox. El agrega en la base de datos los siguiente campos: codigo, nombre, ruta, tipo. El campo codigo cuenta cuantos archivos hay en la base de datos, pero el problema es que luego de agregar. cuando vuelvo a agregar mas archivos en la base de datos me da un error que no me permite duplicados. (obviamente yo lo bloque porque no quiero duplicados) quisiera saber como puedo obtener el ultimo valor del campo codigo e ir sumando desde alli. CODIGO ES UN NUMERO
Código:
Option Explicit
Dim rs As ADODB.Recordset
Dim db As ADODB.Connection
Private Sub Command1_Click()
Set db = New ADODB.Connection
Set rs = New ADODB.Recordset
With db
.Provider = "Microsoft.jet.oledb.4.0" '(Access 2000)
.ConnectionString = "datos.mdb"
.Open
End With
'Aki tomo el index de k.
Dim k As Long
Dim archivo() As String
Dim Modificar As String
Screen.MousePointer = 11
For k = 0 To Text1.ListCount - 1
If InStr(Text1.List(k), "'") Then
Modificar = Modificar & "Original: " & Text1.List(k) & (Chr(13) & Chr(10))
Text1.List(k) = Replace(Text1.List(k), "'", "")
Modificar = Modificar & " Final: " & Text1.List(k) & (Chr(13) & Chr(10))
End If
archivo = Split(Text1.List(k), "\", -1, vbTextCompare)
Call Grabar_BDCancionero(k + 1, Mid(archivo(4), 1, Len(archivo(4)) - 4), Text1.List(k), 0, Right(archivo(4), 4))
Next k
Screen.MousePointer = 0
MsgBox "Se ha actualizado la base de canciones", vbInformation
MsgBox "Modificaciones Pendientes " & (Chr(13) & Chr(10)) & Modificar
'Set rs = db.Execute("select top 10 * from BDCancionero")
Command1.Visible = False
Command2.Visible = True
End Sub
Private Sub Command2_Click()
'recorremos todos los archivos de musica y guardamos cada uno en la DB.
Dim Carpeta_Genero As Long
Dim Carpeta_Artista As Long
Dim i As Integer
Dim j As Integer
Dim k As Integer
Text1.Clear
Carpeta_Genero = Dir1.ListCount
For i = 0 To Dir1.ListCount - 1
Dir2 = Dir1.List(i)
For j = 0 To Dir2.ListCount - 1
'Dir1 = Dir1.List(i)
'If i = 0 Then
' Dir2.Path = Dir1.List(i)
'Else
' Dir2.Path = Dir1
' Dir1 = Dir1.List(-2)
'End If
File1 = Dir2.List(j)
For k = 0 To File1.ListCount - 1
File1.ListIndex = k
Text1.AddItem (File1.Path & "\" & File1.FileName)
Next k
Next j
Next i
Label2.Caption = Text1.ListCount
End Sub
Public Function Grabar_BDCancionero(Codigo As Long, Nombre As String, Ruta As String, Cantidad As Integer, Tipo As String)
Dim sSql As String
sSql = "INSERT INTO BDCancionero VALUES("
sSql = sSql & Codigo & ", '" & Nombre & "', '" & Ruta & "', " & Cantidad & ", '" & Tipo & "')"
Set rs = db.Execute(sSql)
End Function
Private Sub Dir1_Change()
Dir1.Path = Dir1
End Sub
Private Sub Dir2_Change()
File1.Path = Dir2.Path
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
Private Sub Form_Load()
File1.Pattern = "*.mpg;*.wmv;*.avi;*.mp4;*.m2v;*.vob;*.mov;*.asf;*.mp3;*.mp2;*.swa;*.wma;*.wav;*.mid;*.ogg;*.mkv;*.mpa;*.ac3;*.aac;*.kar;*.flv"
End Sub
Private Sub Image1_Click()
End
End Sub
Dim rs As ADODB.Recordset
Dim db As ADODB.Connection
Private Sub Command1_Click()
Set db = New ADODB.Connection
Set rs = New ADODB.Recordset
With db
.Provider = "Microsoft.jet.oledb.4.0" '(Access 2000)
.ConnectionString = "datos.mdb"
.Open
End With
'Aki tomo el index de k.
Dim k As Long
Dim archivo() As String
Dim Modificar As String
Screen.MousePointer = 11
For k = 0 To Text1.ListCount - 1
If InStr(Text1.List(k), "'") Then
Modificar = Modificar & "Original: " & Text1.List(k) & (Chr(13) & Chr(10))
Text1.List(k) = Replace(Text1.List(k), "'", "")
Modificar = Modificar & " Final: " & Text1.List(k) & (Chr(13) & Chr(10))
End If
archivo = Split(Text1.List(k), "\", -1, vbTextCompare)
Call Grabar_BDCancionero(k + 1, Mid(archivo(4), 1, Len(archivo(4)) - 4), Text1.List(k), 0, Right(archivo(4), 4))
Next k
Screen.MousePointer = 0
MsgBox "Se ha actualizado la base de canciones", vbInformation
MsgBox "Modificaciones Pendientes " & (Chr(13) & Chr(10)) & Modificar
'Set rs = db.Execute("select top 10 * from BDCancionero")
Command1.Visible = False
Command2.Visible = True
End Sub
Private Sub Command2_Click()
'recorremos todos los archivos de musica y guardamos cada uno en la DB.
Dim Carpeta_Genero As Long
Dim Carpeta_Artista As Long
Dim i As Integer
Dim j As Integer
Dim k As Integer
Text1.Clear
Carpeta_Genero = Dir1.ListCount
For i = 0 To Dir1.ListCount - 1
Dir2 = Dir1.List(i)
For j = 0 To Dir2.ListCount - 1
'Dir1 = Dir1.List(i)
'If i = 0 Then
' Dir2.Path = Dir1.List(i)
'Else
' Dir2.Path = Dir1
' Dir1 = Dir1.List(-2)
'End If
File1 = Dir2.List(j)
For k = 0 To File1.ListCount - 1
File1.ListIndex = k
Text1.AddItem (File1.Path & "\" & File1.FileName)
Next k
Next j
Next i
Label2.Caption = Text1.ListCount
End Sub
Public Function Grabar_BDCancionero(Codigo As Long, Nombre As String, Ruta As String, Cantidad As Integer, Tipo As String)
Dim sSql As String
sSql = "INSERT INTO BDCancionero VALUES("
sSql = sSql & Codigo & ", '" & Nombre & "', '" & Ruta & "', " & Cantidad & ", '" & Tipo & "')"
Set rs = db.Execute(sSql)
End Function
Private Sub Dir1_Change()
Dir1.Path = Dir1
End Sub
Private Sub Dir2_Change()
File1.Path = Dir2.Path
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
Private Sub Form_Load()
File1.Pattern = "*.mpg;*.wmv;*.avi;*.mp4;*.m2v;*.vob;*.mov;*.asf;*.mp3;*.mp2;*.swa;*.wma;*.wav;*.mid;*.ogg;*.mkv;*.mpa;*.ac3;*.aac;*.kar;*.flv"
End Sub
Private Sub Image1_Click()
End
End Sub