Access数据库中,图片一般保存在外部文件夹内。数据库读取其路径显示。这样做的目的是为了防止数据库过度添加图片,导致数据库膨胀,影响效率。下面我们介绍一下如果添加图片。
前面我们已经做过如何示例来显示图片,这里不再详细说明。如下链接
http://www.office-cn.net/thread-118136-1-1.html
如下图所示:程序可以添加图片和删除图片,当图片存在的时候,可以提示是否覆盖修改图片。
详细源码:
判断图片是否存在函数:
Public Function FileExistCheck(ByVal strFileName As String) As Integer
''测试路径是否有效
'这段代码更好
'返回0代表文件或路径不存在
'返回1代表该文件存在
'返回2代表该文件夹存在
Dim intAttr As Integer
On Error GoTo Err:
FileExistCheck = 0
If Len(strFileName) > 0 Then
intAttr = GetAttr(strFileName)
If (intAttr And vbDirectory) Then
FileExistCheck = 2 '
Else
FileExistCheck = 1 '
End If
End If
Exit Function
Err:
End Function
函数调用:
Private Sub Command32_Click()
On Error GoTo Err:
Dim PhotoPath As String
Dim TempPath As String
PhotoPath = dlgGetFile(, , , , , "查找员工相片")
If PhotoPath = "" Then Exit Sub
TempPath = CurrentProject.path & "\" & Me.ITEM_DESC & ".jpg"
If FileExistCheck(TempPath) = 1 Then
If MsgBox("系统已存在该图片,是否要替换?", vbQuestion + vbYesNo) = vbNo Then Exit Sub
Kill TempPath
End If
FileCopy PhotoPath, TempPath
Me.ImageFrame.Picture = TempPath
Err:
Debug.Print Err.Description
Exit Sub
End Sub
Private Sub Command33_Click()
Dim TempPath As String
TempPath = CurrentProject.path & "\" & Me.ITEM_DESC & ".jpg"
If FileExistCheck(TempPath) = 1 Then
If MsgBox("确认要删除该图片,删除后无法恢复?", vbQuestion + vbYesNo) = vbNo Then Exit Sub
Kill TempPath
Me.ImageFrame.Picture = ""
End If
End Sub
Private Sub Form_Current()
Dim PhotoPath As String
PhotoPath = CurrentProject.path & "\" & Me.ITEM_DESC & ".jpg"
If FileExistCheck(PhotoPath) = 1 Then
Me.ImageFrame.Picture = PhotoPath
Else
Me.ImageFrame.Picture = ""
End If
End Sub