首页 > 解决方案 > 选择多个图像并存储它们的路径。MSAccess-VBA

问题描述

我有一个数据库,我的客户在其中创建了一个检查记录,每次检查都附有多张图片。

到目前为止,我们已经使用附件字段将图片存储在数据库本身中。唯一的问题是我们现在正在接近 2Gb 的限制。

出于这个原因,我试图将图片链接到记录。我创建了一个名为Pictures3 个字段的新表: PK AutoID、 FK PONumberPath.

我设法得到一个按钮来将路径插入表中,唯一的问题是它没有循环通过 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

这是关系图: https ://i.stack.imgur.com/QGXGW.jpg

标签: databasevbams-access

解决方案


使用此代码解决:

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

推荐阅读