2011-03-08 166 views
2

我有一个包含数百个图像的目录,我想用它来在Access中创建和填充记录。我如何使用VBA来做到这一点?我基本上是想做的事:使用VBA将图像导入到MS Access中

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 
+0

你是什么意思“的图像添加到记录的图像附件栏你意图的实际图像文件的副本存储在您的数据库还是仅仅链接到文件? – HK1 2011-03-09 14:46:26

+1

向数据库添加图像很少是一个好主意,甚至需要仔细考虑,例如SQL Server。您可能想说明您希望用这种方法解决什么问题,因为你可能会得到一个选择的想法 – Fionnuala 2011-03-09 15:42:40

+0

我想存储在数据库中的文件的副本如果这不是一个好主意,为什么在Access中有一个附件字段可以让我呈现图像附加形式? – Senior 2011-03-10 14:39:05

回答

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 
+0

谢谢,我将以此为出发点! – Senior 2011-03-10 14:56:09