2011-03-08 219 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