在提供我的答案之前,我将简要介绍一下您的问题及其要求。在我看来,您希望能够加载二进制文件对象,在本例中为图片,使用 VBA、表中的 OLE 对象字段和绑定对象框架。
您最好的选择是停止尝试使用绑定对象框架,因为它有太多限制。
对于您正在尝试做的事情,基本上有两种推荐的方法。
1) 只存储图片文件的链接,然后使用图片控件(可以绑定到您的图片字段)来显示图片。
2) 使用代码将图像存储在 OLE 对象字段中,以将图像作为二进制数据读取。当您需要显示图像时,您需要将其写入临时文件,然后您可以将图像控件上的图片属性设置为临时图像文件的完整路径和文件名。您可以将图像文件作为临时文件进行管理。您可以使用 Windows 的临时目录,也可以在每次需要显示图像时简单地写入相同的文件名。
这些技术都不是太难。这里有一篇非常好的文章可以帮助你进一步理解我在说什么:http://www.jamiessoftware.tk/articles/handlingimages.html
这里有一个读入二进制数据的函数(在本例中是您的图片文件)和另一个写出二进制数据的函数:http://www.ammara.com/access_image_faq/read_write_blob.html 这非常适合将您的图片写入“临时”文件。然后您所要做的就是将图像控件上的 Picture 属性设置为临时文件的文件路径和名称。
您还可以使用 ADO Stream 对象以及 ADO RecordSet 对象和 ADO 连接对象来读取和写入二进制数据。您必须在 Access to Microsoft ActiveX Data Objects 2.8 Library 中设置引用。
下面是一些使用 ADO 将图片添加到数据库的代码:
Private Function LoadPicIntoDatabase(sFilePathAndName As String) As Boolean
On Error GoTo ErrHandler
'Test to see if the file exists. Exit if it does not.
If Dir(sFilePathAndName) = "" Then Exit Function
LoadPicIntoDatabase = True
'Create a connection object
Dim cn As ADODB.Connection
Set cn = CurrentProject.Connection
'Create our other variables
Dim rs As ADODB.Recordset
Dim mstream As ADODB.Stream
Set rs = New ADODB.Recordset
'Configure our recordset variable and open only 1 record (if one exists)
With rs
.LockType = adLockOptimistic
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
.Open "SELECT TOP 1 * FROM tblArticles", cn
End With
'Open our Binary Stream object and load our file into it
Set mstream = New ADODB.Stream
mstream.Open
mstream.Type = adTypeBinary
mstream.LoadFromFile sFilePathAndName
'add a new record and read our binary file into the OLE Field
rs.AddNew
rs.Fields("olepicturefield") = mstream.Read
rs.Update
'Edit: Removed some cleanup code I had inadvertently left here.
Cleanup:
On Error Resume Next
rs.Close
mstream.Close
Set mstream = Nothing
Set rs = Nothing
Set cn = Nothing
Exit Function
ErrHandler:
MsgBox "Error: " & Err.Number & " " & Err.Description
LoadPicIntoDatabase = False
Resume Cleanup
End Function
Private Sub Command0_Click()
If IsNull(Me.txtFilePathAndName) = False Then
If Dir(Me.txtFilePathAndName) <> "" Then
If LoadPicIntoDatabase(Me.txtFilePathAndName) = True Then
MsgBox Me.txtFilePathAndName & " was successfully loaded into the database."
End If
End If
End If
End Sub
编辑1:
根据您的要求,这里是查找/加载给定文章图片的代码。为了保持一致性,我还更改了上面的表和字段名称,以更好地反映您的项目并匹配下面的代码。我测试了这段代码,它对我来说工作正常。
Private Sub Command1_Click()
If IsNull(Me.txtArticleID) = False Then
If DCount("articleid", "tblArticles", "articleid = " & Me.txtArticleID) = 1 Then
Dim rs As DAO.Recordset, sSQL As String, sTempPicture As String
sSQL = "SELECT * FROM tblArticles WHERE ArticleID = " & Me.txtArticleID
Set rs = CurrentDb.OpenRecordset(sSQL)
If Not (rs.EOF And rs.BOF) Then
sTempPicture = "C:\MyTempPicture.jpg"
Call BlobToFile(sTempPicture, rs("olepicturefield"))
If Dir(sTempPicture) <> "" Then
Me.imagecontrol1.Picture = sTempPicture
End If
End If
rs.Close
Set rs = Nothing
Else
MsgBox "Article Not Found"
End If
Else
MsgBox "Please enter an article id"
End If
End Sub
Private Function BlobToFile(strFile As String, ByRef Field As Object) As Long
On Error GoTo BlobToFileError
Dim nFileNum As Integer
Dim abytData() As Byte
BlobToFile = 0
nFileNum = FreeFile
Open strFile For Binary Access Write As nFileNum
abytData = Field
Put #nFileNum, , abytData
BlobToFile = LOF(nFileNum)
BlobToFileExit:
If nFileNum > 0 Then Close nFileNum
Exit Function
BlobToFileError:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, _
"Error writing file in BlobToFile"
BlobToFile = 0
Resume BlobToFileExit
End Function