vba 使用路径从外部源将图像添加到表单中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25856575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:32:46  来源:igfitidea点击:

Adding Images into Forms from External Sources using Paths

imagevbams-accessaccess-vbams-access-2010

提问by Dylan

I've been searching around on Google to find a better way to show images in Access without actually inserting the image into the database.

我一直在 Google 上搜索以找到一种更好的方法来在 Access 中显示图像,而无需将图像实际插入数据库中。

I found this article 'http://support.microsoft.com/kb/285820' via this thread 'Is there a way to get ms-access to display images from external files' which goes into great detail on how to set paths to pictures through folders/files, and it works great, for a 'set' picture. However, I want a different picture to display when I switch to a different record.

我通过此线程找到了这篇文章“ http://support.microsoft.com/kb/285820”“有没有办法获得 ms-access 以显示来自外部文件图像”,其中详细介绍了如何设置路径图片通过文件夹/文件,对于“设置”图片,它效果很好。但是,当我切换到不同的记录时,我希望显示不同的图片。

Here is the code from the article:

这是文章中的代码:

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

I have a feeling that I need to place a line of code that states something like, show image where Image.ID = "ID", but I can't figure out where to put it without getting errors. Am I over-looking something perhaps, or am I approaching this the wrong way? I just don't want to clutter my database, memory-wise, with .bmp images, but I feel like I am going to have to.

我有一种感觉,我需要放置一行代码来说明诸如在 Image.ID = "ID" 处显示图像,但我无法弄清楚将它放在哪里而不会出错。我是否可能忽略了某些东西,或者我是否以错误的方式接近这个问题?我只是不想用 .bmp 图像在内存方面弄乱我的数据库,但我觉得我将不得不这样做。

SOLVED: A much easier solution is as Gord Thompson has described below in the comments. And from my own experience, using this method for .bmp images leaves the picture distorted and out of contrast. I tested the image for .jpg and it worked perfectly! I hope this helps others who are having trouble with similar problems finds this post helpful.

已解决:更简单的解决方案是 Gord Thompson 在下面的评论中所描述的。根据我自己的经验,对 .bmp 图像使用这种方法会使图片失真且失去对比度。我测试了 .jpg 的图像,效果很好!我希望这可以帮助其他遇到类似问题的人发现这篇文章很有帮助。

回答by Gord Thompson

The Microsoft Support article you cited applies to Access 2003. In Access 2010 (and later) there is a muchsimpler way to do it. All you need to do is place an Image control on the form and bind it to the field in your table that contains the path to the image file.

您引用的 Microsoft 支持文章适用于 Access 2003。在 Access 2010(及更高版本)中,有一种简单的方法。您需要做的就是在窗体上放置一个 Image 控件并将其绑定到表中包含图像文件路径的字段。

For example, with an [Employees] table like this

例如,像这样的 [Employees] 表

EmployeeID  FirstName  LastName  PhotoPath                        
----------  ---------  --------  ---------------------------------
         1  Gord       Thompson  C:\Users\Public\Pictures\Gord.jpg
         2  Hank       Kingsley  C:\Users\Public\Pictures\Hank.png

you could use an Image control whose Control Sourceis the [PhotoPath] field ...

您可以使用Control Source[PhotoPath] 字段的图像控件...

DesignView.png

设计视图.png

... and the image will automatically be retrieved from the file specified

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

FormView.png

窗体视图.png

No VBA required.

不需要VBA。

Note also that the image files do not have to be .bmp files.

另请注意,图像文件不必是 .bmp 文件。

Added by an anonymous user:

由匿名用户添加:

For users of the Microsoft Office 365 version of MS Access (c. 2020), here is what you may need to know in order to get this terrific solution to work: The [PhotoPath] field in its data table needs to be "Long Text" data type, if using long paths or long file names. The [PhotoPath] field format "Is Hyperlink" may need to be set to "No." I was getting additional, unwanted coding from Access on my text inputs. The [Image3] control may need to specify "Linked" rather than "Embedded."

对于 Microsoft Office 365 版 MS Access(约 2020 年)的用户,为了使这个出色的解决方案发挥作用,您可能需要了解以下内容:其数据表中的 [PhotoPath] 字段需要为“长文本” " 数据类型,如果使用长路径或长文件名。[PhotoPath] 字段格式“是超链接”可能需要设置为“否”。我在我的文本输入中从 Access 获得了额外的、不需要的编码。[Image3] 控件可能需要指定“Linked”而不是“Embedded”。