显示特定记录Microsoft Access的附件

时间:2014-11-01 16:11:31

标签: vba ms-access

我已经尝试过咨询Google和我的Access书籍,但没有运气。

问题出现了:

如果您在Access中创建一个看起来像这样的表

enter image description here

附件字段名为MyImage,包含Bar的图像,但不包含Foo。

我创建了一个带附件控件的表单,以便为不同的用户轻松添加,删除和显示图像。

enter image description here

我想要做的是能够在我的组合框中选择一个用户,并在附件控件中显示该用户的图像。

我该怎么做?

我通过进入设计视图并单击添加现有字段来创建附件控件。然后我将MyImage-field拖到表单中。我注意到如果Foo有图像,我会出现在附件控件中。所以控制似乎只看Foo。我认为属性表中有一个属性,我可以在我的VBA代码中使用它来更改附件控件的哪个记录"查看",但我找不到任何内容。是否有这样的属性,若然,它叫什么?

2 个答案:

答案 0 :(得分:0)

以另一种方式解决。使用FileDialog打开一个用户可以选择文件的对话框。

将文件读入数据库并显示在ImageBox中。我现在唯一的问题是如何从数据库中检索附件并显示它。

  Private Sub Command1_Click()
       ' Note that you need to add a reference to Microsoft Office
       ' 15.0 Object Library using Tools -> References... for the
       ' file picker to work
       With Application.FileDialog(msoFileDialogFilePicker)
          ' Prevent multiple selections
          .AllowMultiSelect = False
          ' Set the caption of the dialog box
          .Title = "Please pick a signature"
          ' Add filters for common image formats
          .Filters.Clear
          .Filters.Add "Joint Photographic Experts Group (JPG)", "*.JPG"
          .Filters.Add "Joint Photographic Experts Group (JPEG)", "*.JPEG"
          .Filters.Add "Portable Network Graphics", "*.PNG"
          .Filters.Add "Bitmap", "*.BMP"
          If .Show = True Then ' File selected
             For Each imagePath In .SelectedItems
                ' Instantiate the parent recordset
                Set rsImage = CurrentDb.OpenRecordset("Image")
                ' Step to record (We know record exist, else add rsImage.EOF)
                While rsImage.Fields("ID") <> 3 ' Or whatever
                    rsImage.MoveNext
                Wend
                ' Instantiate the child recordset
                Set rsPictures = rsImage.Fields("MyImage").Value
                ' Clear attachments (we only want one attachment at a time)
                Do While Not rsPictures.EOF
                    rsPictures.Delete
                    rsPictures.MoveNext
                Loop
                ' Activate edit mode
                rsImage.Edit
                ' Add the attachment selected by the user
                rsPictures.AddNew
                rsPictures.Fields("FileData").LoadFromFile imagePath
                rsPictures.Update
                ' Update the parent record
                rsImage.Update
                ' Set the content of the ImageBox
                Me.ImageBox.Picture = imagePath
             Next
          Else ' User clicked "Cancel"

          End If
       End With
    End Sub

enter image description here

答案 1 :(得分:0)

如何以编程方式在ms access中将附件字段值检索到附件控件中

我有一个表名 EmployeeeInfo,其中有两个字段“EmployeeId”、“Attach”

#Declare a sub routine 

Private Sub ShowPic(id as integer)
 Me.RecordSource = "SELECT * FROM EMPLOYEEINFO WHERE EMPLOYEEID=" & id
 Me.atchmntControl.ControlSource = "attach"
End Sub

#Call the method