Professional Documents
Culture Documents
oad, cleaning up on form unload, and a simple prerequisites check that the user
has specified the needed input
Private Sub DirList_Change()
' Update the file list box to synchronize with the directory list box.
filList.Path = dirList.Path
filList.Pattern = Me.txtSearchSpec.Text
End Sub
Private Sub DirList_LostFocus()
dirList.Path = dirList.List(dirList.ListIndex)
End Sub
Private Sub DrvList_Change()
On Error GoTo DriveHandler
dirList.Path = drvList.Drive
Exit Sub
DriveHandler:
drvList.Drive = dirList.Path
Exit Sub
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set cn = Nothing
End
End Sub
Private Sub txtSearchSpec_Change()
' Update file list box if user changes pattern.
filList.Pattern = txtSearchSpec.Text
End Sub
Private Sub txtSearchSpec_GotFocus()
txtSearchSpec.SelStart = 0
' Highlight the current entry.
txtSearchSpec.SelLength = Len(txtSearchSpec.Text)
End Sub
Private Sub checkSettings()
' If media category has not been chosen
If Me.cmbCategories.ListIndex = -1 Then
Err.Raise 10000, , "You must select a media category to upload to!"
ElseIf Me.cmbCategories.ListIndex > -1 And Me.cmbCategories.ItemData(Me.cmbC
ategories.ListIndex) = -1 Then
Err.Raise 10003, , "The category you have selected is not a category but
a category group. Please try again but this time only select from the category
listings starting with '--'"
ElseIf Me.txtPath.Text = "" Or Right(Me.txtPath.Text, 1) <> "\" Then
Err.Raise 10001, , "You must specify a path to the directory on the webs
erver " & _
"in which you want to save the media files, the path mu
st end with a '\' (backslash)"
ElseIf Me.filList.ListCount = 0 Then
Err.Raise 10002, , "There was no files with the extensions " & Me.txtSea
rchSpec.Text & " in the chosen directory. No media files where uploaded. "
End If
End Sub
Finally we have the method that is invoked when the user is actually uploading t
he images to the database
Private
Dim
Dim
Dim
Dim
Dim
Sub cmdUpload_Click()
x As Long
rst As ADODB.Recordset, rst2 As ADODB.Recordset
fs As FileSystemObject, f As File
mime As String, orgFilePath As String, orgFile As String
catID As Long, media_id As Long, fileSize As Long
Set f = fs.GetFile(orgFilePath)
fileSize = f.Size
' Insert new record
With rst
.AddNew
!media_id = media_id
Call FileToBlob(rst.Fields("media"), orgFilePath)
!mime_type = mime
!org_file_name = orgFile
!file_size = fileSize
!Description = "Inserted by media bulk upload tool"
!category_id = catID
!user_created = "BULKUPLOAD"
!date_created = Now()
!user_changed = "BULKUPLOAD"
!date_changed = Now()
.Update
End With
' Copy the file
fs.CopyFile orgFilePath, txtPath & media_id & "." & fs.GetExtensionName(
filList.Path & "\" & filList.List(x))
Debug.Print "Copying: " & filList.Path & "\" & filList.List(x) & " -> "
& Me.txtPath.Text & rst2.Fields("id").Value & "." & fs.GetExtensionName(filList.
Path & "\" & filList.List(x))
x = x + 1
Wend
frmUpload.Hide
Exit Sub
Error_Handler:
MsgBox (Err.Description)
Exit Sub
End Sub
And the helper method that does the heavy lifting with regards to loading the by
tes from the file into the vb record.
Sub FileToBlob(fld As ADODB.Field, filename As String, _
Optional ChunkSize As Long = 8192)
Dim fnum As Integer, bytesLeft As Long, bytes As Long
Dim tmp() As Byte
' Raise an error if the field doesn't support GetChunk.
If (fld.Attributes And adFldLong) = 0 Then
Err.Raise 1001, , "Field doesn't support the GetChunk method."
End If
' Open the file; raise an error if the file doesn't exist.
If Dir$(filename) = "" Then Err.Raise 53, , "File not found"
fnum = FreeFile
Open filename For Binary As fnum
' Read the file in chunks, and append data to the field.
bytesLeft = LOF(fnum)
Do While bytesLeft
bytes = bytesLeft