【问题标题】:Select multiple images and store their path. MSAccess - VBA选择多个图像并存储它们的路径。 MSAccess-VBA
【发布时间】:2019-12-09 16:27:59
【问题描述】:

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

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

出于这个原因,我试图将图片链接到记录。我创建了一个名为 Pictures 的新表,其中包含 3 个字段: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

【问题讨论】:

  • 我看不到任何将信息写入表的代码,因此无法尝试重现您的问题。仅根据我们看到的代码,我会说您只获得最后选择的条目?您需要在For Each 循环中处理每个Path
  • Path 也是表单中控件文本框的名称,链接到表格。你能详细说明一下吗?我对VBA相当陌生。是的,我只得到最后一个选定的项目
  • VBA 在猜测编写代码的人想要什么方面比其他一些语言要好得多,但是如果没有明确的选择,它可能会做出错误的选择。对我来说,它看起来像 Path 是一个 (String) 变量,所以对我来说这是一个表单控件并不明显(从问题中甚至不清楚你正在使用表单 - 你只提一张桌子)。如果 VBA 正确识别 Path,那么您可能需要每次通过循环更改表单中显示的 record。就像您手动填写信息一样。
  • edit此信息放入问题中是个好主意...因此,您是否希望所有文件路径都在这一字段中,连接成一个字符串,例如, 逗号还是分号?还是应该为每条路径设置一个条目?
  • 在开始编码之前完成您的设计决策总是一个好主意...这是您需要决定的事情。就个人而言,是的,我会有一个 PK 条目 + 属于它的每个条目。不确定是否需要 FK 条目,因为我不知道整个数据库...

标签: database vba ms-access


【解决方案1】:

用这段代码解决了:

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

【讨论】:

    猜你喜欢
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多