问题
I'm trying to attach a large number of images (~1000) into Microsoft Access. I figured using VBA to automate the task would be sensible instead of doing it manually.
I do NOT want to link the hyperlink or path or OLE to file's location, which would keep the database's file size down. (Edit: It has been understood that Ms Access has a limit of 2Gb, I would like to go forward under the assumption that the 2Gb limit would not be exceed in this case.)
The database I want to do all this in is named "database1". The table I want to import the images into is named "Table1". In this table there are 3 column so far:
1) The auto-generated ID, which I kept as it is
2) A column with the heading named "file_name", which is currently "image1", "image2", "image3", etc. Alternatively I can change the entries into this column as the path of the files on my computer (e.g. C:\Users\Username\Documents\image1.jpg). I have generated a list of all the images' path on my computer using a .bat file into a .txt file, which is named "file_paths".
3) A column with the heading named "attachment_column". This is the column which I want the images to be put into in my database.
i would like the image to be imported into the corresponding Database entry as per its file_name in column 2), if possible.
I have been looking at various documentation and tried them without any luck. https://msdn.microsoft.com/VBA/Access-VBA/articles/work-with-attachments-in-dao https://access-programmers.co.uk/forums/showthread.php?t=172939
The closet thing I have is something like this below. But I can't figure out how to loop through all the files paths in file_paths.txt to attach all images.
Sub macrotest2()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb 'I guess I don't have to define as database1 ?
Set rsEMployees = db.OpenRecordset("Table1", dbOpenDynaset)
rsEMployees.Edit
Set rsPictures = rsEMployees.Fields("attachment_column").Value
rsPictures.AddNew
rsPictures.Fields("attachment_column").LoadFromFile "C:\Users\Username\Documents\image1.jpg"
'how to automate this to loop all the file paths in file_paths.txt?
rsPictures.Update
rsEMployees.Update
End Sub
Thank you in advanced.
回答1:
Try this:
Dim fileName As String, textRow As String, fileNo As Integer
fileName = "C:\file_paths.txt"
fileNo = FreeFile 'Get first free file number
Dim i as Integer
Dim db As DAO.Database
Dim rsEmployees As DAO.Recordset, rsPictures AS DAO.Recordset
Set db = CurrentDb()
Open fileName For Input As #fileNo
Do While Not EOF(fileNo)
i = i + 1
Set rsEmployees = db.OpenRecordset("Table1", dbOpenDynaset)
rsEmployees.Edit
rsEmployees.AddNew
Line Input #fileNo, textRow
rsEmployees.Fields("file_name").Value = textRow
Set rsPictures = rsEmployees.Fields("attachment_column").Value
rsPictures.AddNew
rsPictures.Fields("FileData").LoadFromFile textRow
rsPictures.Update
rsPictures.Close
rsEmployees.Update
rsEmployees.Close
Loop
Close #fileNo
MsgBox i
There are multiple ways to go through a way line by line, but I like this one.
Note that there can't be empty lines in your text file. Even the last line needs to contain a file link.
来源:https://stackoverflow.com/questions/45659706/using-vba-to-import-a-large-number-of-attachment-into-microsoft-access