Microsoft Access - 根据文件夹目录+名字自动将图片链接到数据库

Microsoft Access - 根据文件夹目录+名字自动将图片链接到数据库

问题描述:

嗨我找不到任何关于此的东西,如果有人能指出我的方向是正确的,那就太好了。Microsoft Access - 根据文件夹目录+名字自动将图片链接到数据库

我有4000个健身房成员的访问数据库。 我想通过从给定的目录链接来添加个人资料图片。 我不想手动链接4000张图片

我希望它在目录中自动搜索和匹配的名字+姓氏+ DOB与图片中的部件,其具有相同 例如:bobjones05121989

感谢您的帮助。

+0

太宽泛的问题,需要额外的信息。你从哪里得到名字+姓氏+出生地点?我认为的一种形式?您是否希望将图片路径存储到数据库中,或者在表单加载后立即显示它? (如果是这种情况)。请澄清。 –

+0

感谢您的回复。是的,数据将来自表单。我只是想显示,例如链接。不存储在数据库中 –

一您是否使用Access 2007或更高版本?使用Image控件的ControlSource属性。的表达可以构造的文件路径,如:

="C:\somepath\" & [FirstName] & [LastName] & Format([DOB], "ddmmyyyy") & ".jpg"

如果图像不存在图像控件将是空白的。

+0

完美谢谢 –

当窗体加载时试试这个。

您需要将字段名称更改为数据库的实际名称。

Private Sub Form_Load() 
    Dim imagepath As String 
    With Me 
     imagepath = "Drive:\" & LCase(![FirstName]) & LCase(![LastName]) & Format(![DOB], "ddmmyyyy") & ".jpg" 
    End With 

    If Len(Dir(imagepath)) > 0 Then 
     Me.ImageControl.Picture = imagepath 
    End If 
End Sub 
+0

如果使用Access 2007或更高版本,只需使用构建文件路径的表达式来设置Image控件的ControlSource属性即可。不需要VBA。 – June7

您可以修改功能UrlContent从其他一些领域(S)比ID链接,而不是(这里)下载任何东西(你已经有文件),但只返回图片的路径:

' Download (picture) file from a URL of a hyperlink field to a 
' (temporary) folder, and return the full path to the downloaded file. 
' 
' This can be used as the control source for a bound picture control. 
' If no Folder is specified, the user's IE cache folder is used. 
' 
' Typical usage in the RecordSource for a form or report where Id is 
' the unique ID and Url is the hyperlink field holding the URL to 
' the picture file to be displayed: 
' 
'   - to a cached file where parameter Id is not used: 
' 
'   Select *, UrlContent(0, [Url]) As Path From SomeTable; 
' 
'   - or, where Id is used to create the local file name: 
' 
'   Select *, UrlContent([Id], [Url], "d:\somefolder") As Path From SomeTable; 
' 
' Then, set ControlSource of the bound picture control to: Path 
' 
' 2017-05-28. Gustav Brock, Cactus Data ApS, CPH. 
' 
Public Function UrlContent(_ 
    ByVal Id As Long, _ 
    ByVal Url As String, _ 
    Optional ByVal Folder As String) _ 
    As Variant 

    Const NoError   As Long = 0 
    Const Dot       As String = "." 
    Const BackSlash As String = "\" 
    
    Dim Address     As String 
    Dim Ext         As String 
    Dim Path        As String 
    Dim Result      As String 
    
    ' Strip leading and trailing octothorpes from URL string. 
    Address = HyperlinkPart(Url, acAddress) 
    ' If Address is a zero-length string, Url was not wrapped in octothorpes. 
    If Address = "" Then 
        ' Use Url as is. 
        Address = Url 
    End If 
    
    If Folder = "" Then 
        ' Import to IE cache. 
        Result = DownloadCacheFile(Address) 
    Else 
        If Right(Folder, 1) <> BackSlash Then 
            ' Append a backslash. 
            Folder = Folder & BackSlash 
        End If 
    
        ' Retrieve extension of file name. 
        Ext = StrReverse(Split(StrReverse(Address), Dot)(0)) 
        ' Build full path for downloaded file. 
        Path = Folder & CStr(Id) & Dot & Ext 
        
        If DownloadFile(Address, Path) = NoError Then 
            Result = Path 
        End If 
    End If 
    
    UrlContent = Result 
    
End Function 

的文章,一个完整的演示可以在这里找到:

Show pictures directly from URLs in Access forms and reports

+0

这是不适用的,因为我理解它,因为图片已经在目录中,而不是在互联网上。因此不需要使用HTTP下载来获取它们。 –

+0

答案说修改代码不下载 - 只是为了返回文件路径。 – June7

+0

@ErikvonAsmuth:是的。我相信你错过了主句:_修改函数..到..不..下载任何东西..但只是返回picture_的路径。在本质上,省略下载功能。我们对图片或命名风格一无所知,因此需要修改_how_以便提问者解决。 – Gustav