Dyl*_*lan 3 ms-access vba image ms-access-2010
我一直在 Google 上搜索以找到一种更好的方法来在 Access 中显示图像,而无需将图像实际插入数据库中。
我通过这个线程找到了这篇文章“ http://support.microsoft.com/kb/285820 ”“有没有办法获得 ms-access 以显示来自外部文件的图像”,其中详细介绍了如何设置路径图片通过文件夹/文件,对于“设置”图片,它效果很好。但是,当我切换到不同的记录时,我希望显示不同的图片。
这是文章中的代码:
Option Compare Database
Option Explicit
Public Function DisplayImage(ctlImageControl As Control, strImagePath As Variant) As String
On Error GoTo Err_DisplayImage
Dim strResult As String
Dim strDatabasePath As String
Dim intSlashLocation As Integer
With ctlImageControl
If IsNull(strImagePath) Then
.Visible = False
strResult = "No image name specified."
Else
If InStr(1, strImagePath, "\") = 0 Then
' Path is relative
strDatabasePath = CurrentProject.FullName
intSlashLocation = InStrRev(strDatabasePath, "\", Len(strDatabasePath))
strDatabasePath = Left(strDatabasePath, intSlashLocation)
strImagePath = strDatabasePath & strImagePath
End If
.Visible = True
.Picture = strImagePath
strResult = "Image found and displayed."
End If
End With
Exit_DisplayImage:
DisplayImage = strResult
Exit Function
Err_DisplayImage:
Select Case Err.Number
Case 2220 ' Can't find the picture.
ctlImageControl.Visible = False
strResult = "Can't find image in the specified name."
Resume Exit_DisplayImage:
Case Else ' Some other error.
MsgBox Err.Number & " " & Err.Description
strResult = "An error occurred displaying image."
Resume Exit_DisplayImage:
End Select
End Function
Run Code Online (Sandbox Code Playgroud)
我有一种感觉,我需要放置一行代码来说明诸如在 Image.ID = "ID" 处显示图像,但我无法弄清楚将它放在哪里而不会出错。我是否可能忽略了某些东西,或者我是否以错误的方式接近这个问题?我只是不想用 .bmp 图像在内存方面弄乱我的数据库,但我觉得我将不得不这样做。
已解决:更简单的解决方案是 Gord Thompson 在下面的评论中所描述的。根据我自己的经验,对 .bmp 图像使用这种方法会使图片失真且失去对比度。我测试了 .jpg 的图像,效果很好!我希望这可以帮助其他遇到类似问题的人发现这篇文章很有帮助。
您引用的 Microsoft 支持文章适用于 Access 2003。在 Access 2010(及更高版本)中,有一种更简单的方法。您需要做的就是在窗体上放置一个 Image 控件并将其绑定到表中包含图像文件路径的字段。
例如,像这样的 [Employees] 表
EmployeeID FirstName LastName PhotoPath
---------- --------- -------- ---------------------------------
1 Gord Thompson C:\Users\Public\Pictures\Gord.jpg
2 Hank Kingsley C:\Users\Public\Pictures\Hank.png
Run Code Online (Sandbox Code Playgroud)
您可以使用Control Source[PhotoPath] 字段的图像控件...

...并且图像将自动从指定的文件中检索

不需要VBA。
另请注意,图像文件不必是 .bmp 文件。
由匿名用户添加:
对于 Microsoft Office 365 版 MS Access(约 2020 年)的用户,为了使这个出色的解决方案发挥作用,您可能需要了解以下内容:其数据表中的 [PhotoPath] 字段需要为“长文本” " 数据类型,如果使用长路径或长文件名。[PhotoPath] 字段格式“是超链接”可能需要设置为“否”。我在我的文本输入中从 Access 获得了额外的、不需要的编码。[Image3] 控件可能需要指定“Linked”而不是“Embedded”。
| 归档时间: |
|
| 查看次数: |
11756 次 |
| 最近记录: |