从一般数据库的角度来看,您不应该将图像直接存储在数据库中,除非它们非常小(大约小于 1 MB),并且您实际上有空间来存储它们。最常见的做法是将图像路径存储在数据库中,然后在需要时直接从存储图像的文件目录中加载这些图像。它会增加另一个级别的复杂性,但会从根本上减少您的数据库大小。
更新:
此代码允许用户单击表单中的某个元素框架,将路径存储在数据库中(路径为 username.jpg),然后在单击的框架中向用户显示图片:
Private Sub SignatureFrame_Click()
On Error GoTo ErrorHandler
' Get path for the new picture from a dialog box
Dim Path As String
Dim fd As FileDialog
Dim ffs As FileDialogFilters
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
Set ffs = .Filters
With ffs
.Clear
.Add "Pictures", "*.jpg"
End With
.AllowMultiSelect = False
If .Show = False Then Exit Sub
Path = .SelectedItems(1)
End With
' Update the picture in the user interface
Me![SignatureFrame_Click].Picture = Path
' Copy the signature into the local Signature folder of the DB
Dim fs As Object
Dim oldPath As String, newPath As String, file As String
oldPath = Path 'Full path the file is located in
newPath = CurrentProject.Path & "\Signatures\Users\" & Screen.ActiveForm.UserName & ".jpg" 'Folder and path to copy file to
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile oldPath, newPath 'This file was an .jpg file
Set fs = Nothing
' Set the new picture path for the form
Screen.ActiveForm.SignaturePath = newPath
Exit Sub
ErrorHandler:
MsgBox "Could not upload the image. Please check that the file format is of type jpg."
Resume Next
End Sub
然后在稍后的某个时间点以其他形式,您可以像这样检索图像:
Me!SignatureFrame.Picture = CurrentProject.Path & "\Signatures\username.jpg"
PS:代码已经在SO编辑器中翻译过了,所以我不能保证没有错误。
希望有帮助!