【发布时间】:2021-11-30 16:04:42
【问题描述】:
我的前同事建立了一个包含许多记录集的 Access 数据库,每个记录集都附有一到五张图片。数据库的大小现在非常大(大约 2 GB)而且非常慢。
我没有将图片包含在数据库附件中,而是将图片的路径和名称作为字符串存储在列中,然后在需要时调用它们。
现在我必须在重命名它们后将所有现有图像(大约 3000 张图片)从数据库导出到一个文件夹(它们的描述存储在数据库的另一列中,因为现在它们的名称就像 IMG_#### ,并且我不想在导出后手动查找并重命名它们)。
我在互联网上找到了一些东西。但它只是导出第一个记录集的附件而已。我怎样才能根据需要修改它?
Dim strPath As String
Dim rs As DAO.Recordset
Dim rsPictures As Variant
strPath = Application.CurrentProject.Path
'????How to loop through all record set???
' Instantiate the parent recordset.
Set rs = CurrentDb.OpenRecordset("Assets")
' Instantiate the child recordset.
Set rsPictures = rs.Fields("Attachments").Value
' Loop through the attachments.
While Not rsPictures.EOF
'????How to rename the picture???
' Save current attachment to disk in the "My Documents" folder.
rsPictures.Fields("FileData").SaveToFile strPath & "\Attachment"
rsPictures.MoveNext
Wend
【问题讨论】:
-
文件系统与数据库存储有点圣战话题。您是否考虑过迁移到更强大的后端,例如 SQL Server?
-
嗯,只需对主
rs执行与rsPictures相同的While Not rs.EOF循环即可。
标签: vba ms-access export attachment