Hello,
i am trying to add info to a RS and i have to move to the last record to do this.
I have 4420 records in the Database but the MoveLast function takes me to record 4389 and because i am renaming the data as i put it into the rs acording to the last record the module fails because it says it already has a file of that name.
Do i need to sort the record??
Thanks
d
i am trying to add info to a RS and i have to move to the last record to do this.
I have 4420 records in the Database but the MoveLast function takes me to record 4389 and because i am renaming the data as i put it into the rs acording to the last record the module fails because it says it already has a file of that name.
Do i need to sort the record??
Thanks
d
Code:
Function ImportImages()
'Open RecordSet
Dim dbs As Database, rst As Recordset, strSQL As String
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("Admin")
'name the var that will hold the pathe to the directory of the system
Dim sysPath As String
Dim ImgPath As String
Dim ImagePath As String
sysPath = rst.Fields("sysPath")
ImagePathArchive = rst.Fields("ImagePathArchive")
ImagePath = rst.Fields("ImagePath")
'Count files in folder
Dim varFiles() As Variant
varFiles() = GetAllFilesInDir(sysPath & ImagePath)
rst.Close
'Open RecordSet
Set rst = dbs.OpenRecordset("Images")
'Obtain Rows in table IMAGE & Folder "New"
'Z = rst.RecordCount
' Do While rst.EOF <> True
' rst.MoveNext
' Loop
' rst.MovePrevious
rst.MoveLast
Z = rst.Fields("ImgSrc")
Z = Left(Z, Len(Z) - 4)
Z = Right(Z, 6)
'ADD Error handling for Zero Files
X = (UBound(varFiles()) + 1)
If X < 0 Then
MsgBox "No files to add to database."
Exit Function
End If
'Obtain Next New File Name
Y = CInt(Z) + 1
'Add new items to IMAGE table
For W = 0 To UBound(varFiles())
'Check file type
If UCase(Right(varFiles(W), 3)) = "JPG" Or UCase(Right(varFiles(W), 3)) = "JPEG" Then
'Build File Name of next image
Select Case Len(Y)
Case 1
NewName = "00000" & Y & ".jpg"
Case 2
NewName = "0000" & Y & ".jpg"
Case 3
NewName = "000" & Y & ".jpg"
Case 4
NewName = "00" & Y & ".jpg"
Case 5
NewName = "0" & Y & ".jpg"
Case 6
NewName = Y & ".jpg"
Case Else
MsgBox "Database cannot accept more than one million images."
Exit Function
End Select
'renames and moves the file
OldFilePath = sysPath & ImagePath & varFiles(W) ' original file location
NewFilePath = sysPath & ImagePathArchive & NewName ' new file location
Name OldFilePath As NewFilePath
'move the file
With rst
.AddNew
'adding the path into the table
.Fields("ImgSrc") = ImagePathArchive & NewName
.Fields("Status") = 1
End With
rst.Update
Y = Y + 1
End If
Next W
Set dbs = Nothing
End Function
Function GetAllFilesInDir(ByVal strDirPath As String) As Variant
' Loop through the directory specified in strDirPath and save each
' file name in an array, then return that array to the calling
' procedure.
' Return False if strDirPath is not a valid directory.
Dim strTempName As String
Dim varFiles() As Variant
Dim lngFileCount As Long
On Error GoTo GetAllFiles_Err
' Make sure that strDirPath ends with a "\" character.
If Right$(strDirPath, 1) <> "\" Then
strDirPath = strDirPath & "\"
End If
' Make sure strDirPath is a directory.
If GetAttr(strDirPath) = vbDirectory Then
strTempName = Dir(strDirPath, vbDirectory)
Do Until Len(strTempName) = 0
' Exclude ".", "..".
If (strTempName <> ".") And (strTempName <> "..") Then
' Make sure we do not have a sub-directory name.
If (GetAttr(strDirPath & strTempName) _
And vbDirectory) <> vbDirectory Then
' Increase the size of the array
' to accommodate the found filename
' and add the filename to the array.
ReDim Preserve varFiles(lngFileCount)
varFiles(lngFileCount) = strTempName
lngFileCount = lngFileCount + 1
End If
End If
' Use the Dir function to find the next filename.
strTempName = Dir()
Loop
' Return the array of found files.
GetAllFilesInDir = varFiles
End If
GetAllFiles_End:
Exit Function
GetAllFiles_Err:
GetAllFilesInDir = False
Resume GetAllFiles_End
End Function