Importing images into MS Access using VBA
I hav开发者_如何学Ce a directory with hundreds of images that I would like to use to create and populate records in Access. How do I do this using VBA? I essentially want to do:
choose directory
for each image in the directory:
create new record
set "name" field of the record to the file name
add the image to the "image" attachment field of the record
Choose Directory:
Because there are many different ways to do this, I'll leave this part up to you. Here's some code if you want to use the Common 'Browse for Folder' Dialog window.
To find each image in a directory:
Public Sub LogPictureFilesToDatabase(sFolderPath As String)
Dim sFileName As String
sFileName = Dir(sFolderPath)
Do Until sFileName = ""
Select Case LCase(Right(sFileName, 4))
Case ".jpg", ".gif", ".bmp"
'Put your SQL Insert Statement here
'Or you can use DAO or ADO to add new records instead, if you prefer
'You may also want to use a function to insert a blob if you want to save
'the file inside the database, which I do not recommend
Case Else
'Ignore other file extentions
End Select
sFileName = Dir 'Get next file
Loop
End Sub
精彩评论