使用VBA将图像导入MS Access

时间:2011-03-08 21:03:56

标签: ms-access vba import attachment

我有一个包含数百个图像的目录,我想用它来创建和填充Access中的记录。如何使用VBA执行此操作?我基本上想做:

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

1 个答案:

答案 0 :(得分:3)

选择目录:
因为有很多不同的方法可以做到这一点,我会把这部分留给你。 Here's some code if you want to use the Common 'Browse for Folder' Dialog window.

查找目录中的每个图像:

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