database - 选择多个图像并存储它们的路径。MSAccess-VBA
问题描述
我有一个数据库,我的客户在其中创建了一个检查记录,每次检查都附有多张图片。
到目前为止,我们已经使用附件字段将图片存储在数据库本身中。唯一的问题是我们现在正在接近 2Gb 的限制。
出于这个原因,我试图将图片链接到记录。我创建了一个名为Pictures
3 个字段的新表: PK AutoID
、 FK PONumber
、Path
.
我设法得到一个按钮来将路径插入表中,唯一的问题是它没有循环通过 FileDialog 中的选择,为每个路径创建新记录。
更新我创建了一个新表单只是为了添加图片。表单链接到表格图片,所以现在我只需要通过用户选择进行子循环并将路径存储在路径字段中,为每个路径添加一条新记录,但保持相同的 PONumber 以便它可以链接到负载表。
这是我用于按钮的代码:
Private Sub PictureSelector_Click()
Dim fDialog As FileDialog
Dim varFile As Variant
' Set up the File Dialog.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
' Allow user to make multiple selections in dialog box
.AllowMultiSelect = True
' Set the title of the dialog box.
.Title = "Please select one or more files"
'Set the starting folder.
.InitialFileName = "Q:\"
'Show only graphic/image type files.
.Filters.Clear
.Filters.Add "Images", "*.jpg; *.jpeg", 1
' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If fDialog.Show Then
For Each varFile In .SelectedItems
Path = varFile
Next varFile
End If
End With
End Sub
解决方案
使用此代码解决:
Private Sub UploadPicture_Click()
Dim fDialog As FileDialog
Dim varFile As Variant
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("Pictures")
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.AllowMultiSelect = True
.Title = "Please select one or more files"
.InitialFileName = "Q:\"
.Filters.Clear
.Filters.Add "Images", "*.jpg; *.jpeg", 1
' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If fDialog.Show Then
For Each varFile In .SelectedItems
rs.AddNew
rs!PONumber = Me.PONumber
rs!Path = varFile
rs.Update
Next varFile
End If
End With
End Sub
推荐阅读
- azure-keyvault - 在 Blob 存储中存储机密的缺点
- reactjs - 反应 + 打字稿
- python - 如何从列表中进行选择并将列表中的对象用作标签?
- python - 无法使用我的 py 文件通过命令创建文件夹?
- c# - 动态设置 RibbonComboBox 的选定项(在运行时)?
- html - 如何在 If 语句中放置两个 href 链接?
- python - 如何从 txt 文件中获取结果或将几个文件合并为一个?
- c# - 网格上点之间的最近路径
- linux - qemu-arm qemu:未捕获的目标信号 11(分段错误)- 核心转储
- spring - 将接口传递给控制器构造函数的问题