首页 > 解决方案 > 将 jpg BLOB 从 Access 365 查询传递到 VBA 函数的问题

问题描述

我在 MS Access 数据库中链接到的 ESRI SDE/MS SQL 表中有 BLOB jpeg。我想我已经编写了一个 VBA 函数(如下),它将(我认为)将它们卸载到我的 Temp 目录。使用包含嵌入式 DAO SQL 查询(此处未共享)的此代码的变体,我可以成功卸载照片。

我不想编写 3 个不同的函数来解释点、线和多边形,而是想在 Access 查询中调用该函数,将两个不同的整数值和 BLOB 字段内容传递给它。

在构建 Access 查询时,我将三个参数传递给函数,但它从未真正调用过该函数。在同一个查询中,我成功调用了一个不同的函数来传递数据。我很困惑为什么在我的访问查询中调用这个函数:

PicFileLoc: Blob2File([Landfill_InspectionPoint_Attach_vw].[ATTACHMENTID],[Landfill_InspectionPoint_Attach_vw].[DATA],[Landfill_InspectionPoint_evw].[ItemNo])

不调用此函数。我错过了什么明显的东西?BLOB 不能传递给用户定义的函数吗?如果我将 blbData 的数据类型从 Byte 更改为 String,代码开始运行,但它显然会失败,因为 BLOB 不是字符串。

Public Function Blob2File(ByVal intAttID As Integer, ByVal blbData As Byte, ByVal intItemNo As Integer) As String
    On Error GoTo Blob2FileError
    Dim strFile As String
    Dim nFileNum As Integer
    Dim abytData As Byte
    abytData = blbData
    Blob2File = Null

    strFile = Environ$("USERPROFILE") & "\Local Settings\Temp\" & intItemNo & "_Pic_" & intAttID & ".jpg" 'placed in the users temp directory on the local c drive.
    nFileNum = FreeFile
    Open strFile For Binary Access Write As nFileNum
    Put #nFileNum, , abytData
    Blob2File = strFile

Blob2FileExit:
    If nFileNum > 0 Then Close nFileNum
    Exit Function

Blob2FileError:
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, _
           "Error writing file in Blob2File"
    Blob2File = Null
    Resume Blob2FileExit

End Function

谢谢,

戴尔

标签: vbams-accessuser-defined-functions

解决方案


您允许您的 blob 数据只有一个字节,而它是一个字节数组。

通过将 () 添加到声明中的名称和函数调用中的类型,将其更改为字节数组:

Public Function Blob2File(ByVal intAttID As Integer, ByVal blbData As Byte(), ByVal intItemNo As Integer) As String
    On Error GoTo Blob2FileError
    Dim strFile As String
    Dim nFileNum As Integer
    Dim abytData() As Byte
    abytData = blbData
    Blob2File = Null

    strFile = Environ$("USERPROFILE") & "\Local Settings\Temp\" & intItemNo & "_Pic_" & intAttID & ".jpg" 'placed in the users temp directory on the local c drive.
    nFileNum = FreeFile
    Open strFile For Binary Access Write As nFileNum
    Put #nFileNum, , abytData
    Blob2File = strFile

Blob2FileExit:
    If nFileNum > 0 Then Close nFileNum
    Exit Function

Blob2FileError:
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, _
           "Error writing file in Blob2File"
    Blob2File = Null
    Resume Blob2FileExit

End Function

请注意,我强烈建议不要在查询中写入文件。数据库引擎可能会根据各种条件多次调用该函数,或者如果它决定只显示部分记录直到全部显示,则根本不调用该函数。


推荐阅读