使用 VBA 将图像导入 MS Access
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5238299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Importing images into MS Access using VBA
提问by Senior
I have 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:
我有一个包含数百张图像的目录,我想用这些图像在 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
回答by HK1
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