VB/MS Access 2010,检查文件是否存在
VB/MS Access 2010, check if file exists
我是 VB/MS Access 的新手。我正在尝试检查文件是否存在但出现错误。有人能告诉我这段代码有什么问题吗:
Private Sub Form_Current()
On Error Resume Next
GetDBPath = CurrentProject.Path & "\graphics\"
If FileExists(GetDBPath & Me![Materiel_BILDNAMN]) Then
Me![materialbildnamn].Picture = GetDBPath & Me![Materiel_BILDNAMN]
Else
Me![materialbildnamn].Picture = GetDBPath & "blank.jpg"
End If
End Sub
您的问题是因为您正在调用一个名为 FileExists
的函数,但您没有在任何地方定义它。如果我是正确的,代码可以在 Allen Browne 的网站上找到。给它,
Function FileExists(ByVal strFile As String, Optional bFindFolders As Boolean) As Boolean
'Purpose: Return True if the file exists, even if it is hidden.
'Arguments: strFile: File name to look for. Current directory searched if no path included.
' bFindFolders. If strFile is a folder, FileExists() returns False unless this argument is True.
'Note: Does not look inside subdirectories for the file.
'Author: Allen Browne. http://allenbrowne.com June, 2006.
Dim lngAttributes As Long
'Include read-only files, hidden files, system files.
lngAttributes = (vbReadOnly Or vbHidden Or vbSystem)
If bFindFolders Then
lngAttributes = (lngAttributes Or vbDirectory) 'Include folders as well.
Else
'Strip any trailing slash, so Dir does not look inside the folder.
Do While Right$(strFile, 1) = "\"
strFile = Left$(strFile, Len(strFile) - 1)
Loop
End If
'If Dir() returns something, the file exists.
On Error Resume Next
FileExists = (Len(Dir(strFile, lngAttributes)) > 0)
End Function
将代码复制到 标准模块 中,给它起一个类似 mod_FileCheck
的名称。然后编译程序。应该没问题!
我是 VB/MS Access 的新手。我正在尝试检查文件是否存在但出现错误。有人能告诉我这段代码有什么问题吗:
Private Sub Form_Current()
On Error Resume Next
GetDBPath = CurrentProject.Path & "\graphics\"
If FileExists(GetDBPath & Me![Materiel_BILDNAMN]) Then
Me![materialbildnamn].Picture = GetDBPath & Me![Materiel_BILDNAMN]
Else
Me![materialbildnamn].Picture = GetDBPath & "blank.jpg"
End If
End Sub
您的问题是因为您正在调用一个名为 FileExists
的函数,但您没有在任何地方定义它。如果我是正确的,代码可以在 Allen Browne 的网站上找到。给它,
Function FileExists(ByVal strFile As String, Optional bFindFolders As Boolean) As Boolean
'Purpose: Return True if the file exists, even if it is hidden.
'Arguments: strFile: File name to look for. Current directory searched if no path included.
' bFindFolders. If strFile is a folder, FileExists() returns False unless this argument is True.
'Note: Does not look inside subdirectories for the file.
'Author: Allen Browne. http://allenbrowne.com June, 2006.
Dim lngAttributes As Long
'Include read-only files, hidden files, system files.
lngAttributes = (vbReadOnly Or vbHidden Or vbSystem)
If bFindFolders Then
lngAttributes = (lngAttributes Or vbDirectory) 'Include folders as well.
Else
'Strip any trailing slash, so Dir does not look inside the folder.
Do While Right$(strFile, 1) = "\"
strFile = Left$(strFile, Len(strFile) - 1)
Loop
End If
'If Dir() returns something, the file exists.
On Error Resume Next
FileExists = (Len(Dir(strFile, lngAttributes)) > 0)
End Function
将代码复制到 标准模块 中,给它起一个类似 mod_FileCheck
的名称。然后编译程序。应该没问题!