M
Mike
I have created a DB in Access 2000. There is a form with a custom
"BrowseButton" which opens an "open file" dialog box. It works fine in Access
2000, but it doesn't in Access 2003. Even the error handler reports nothing.
Here is the code for the "BrowseButton" on-click event and some additional
related code:
Option Compare Database
Option Explicit
Private Type OPENFILENAME
lStructSize As Long
hwnd As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Private Declare Function GetOpenFileName _
Lib "comdlg32.dll" Alias "GetOpenFileNameA" _
(pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetSaveFileName _
Lib "comdlg32.dll" Alias "GetSaveFileNameA" _
(pOpenfilename As OPENFILENAME) As Long
Private Const OFN_FILEMUSTEXIST = &H1000
Private Const OFN_HIDEREADONLY = &H4
Private Const OFN_OVERWRITEPROMPT = &H2
Private Const OFN_PATHMUSTEXIST = &H800
Private Const OFN_SAVE = 0
Private Const OFN_OPEN = 1
Private Function DialogFile(wMode As Integer, szDialogTitle As String,
szFileName As String, szFilter As String, szDefDir As String, szDefExt As
String) As String
Dim X As Long, OFN As OPENFILENAME, szFile As String, szFileTitle As String
With OFN
.lStructSize = Len(OFN)
.hwnd = Me.hwnd
.lpstrTitle = szDialogTitle
.lpstrFile = szFileName & String$(250 - Len(szFileName), 0)
.nMaxFile = 255
.lpstrFileTitle = String$(255, 0)
.nMaxFileTitle = 255
.lpstrFilter = NullSepString(szFilter)
.nFilterIndex = 2
.lpstrInitialDir = szDefDir
.lpstrDefExt = szDefExt
If wMode = 1 Then
OFN.Flags = OFN_HIDEREADONLY Or OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST
X = GetOpenFileName(OFN)
Else
OFN.Flags = OFN_HIDEREADONLY Or OFN_OVERWRITEPROMPT Or OFN_PATHMUSTEXIST
X = GetSaveFileName(OFN)
End If
If X <> 0 Then
If InStr(.lpstrFile, Chr$(0)) > 0 Then
szFile = Left$(.lpstrFile, InStr(.lpstrFile, Chr$(0)) - 1)
End If
DialogFile = szFile
Else
DialogFile = ""
End If
End With
End Function
Private Function NullSepString(ByVal CommaString As String) As String
Dim intInstr As Integer
Const vbBar = "|"
Do
intInstr = InStr(CommaString, vbBar)
If intInstr > 0 Then Mid$(CommaString, intInstr, 1) = vbNullChar
Loop While intInstr > 0
NullSepString = CommaString
End Function
Private Sub BrowseButton_Click()
On Error GoTo ErrHandler
With Me.FilePath
.Value = DialogFile(1, _
"Choose file...", _
vbNullString, _
"Microsoft Excel Book (*.xls)|*.xls", _
CurDir(), _
vbNullString)
End With
ExitHere:
Exit Sub
ErrHandler:
With Err
MsgBox "Error: " & .Number & vbCrLf & .Description, _
vbCritical Or vbOKOnly, .Source
End With
Resume ExitHere
End Sub
Why doesn't it work in Access 2003?
Thank you in advance for any tips!
"BrowseButton" which opens an "open file" dialog box. It works fine in Access
2000, but it doesn't in Access 2003. Even the error handler reports nothing.
Here is the code for the "BrowseButton" on-click event and some additional
related code:
Option Compare Database
Option Explicit
Private Type OPENFILENAME
lStructSize As Long
hwnd As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Private Declare Function GetOpenFileName _
Lib "comdlg32.dll" Alias "GetOpenFileNameA" _
(pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetSaveFileName _
Lib "comdlg32.dll" Alias "GetSaveFileNameA" _
(pOpenfilename As OPENFILENAME) As Long
Private Const OFN_FILEMUSTEXIST = &H1000
Private Const OFN_HIDEREADONLY = &H4
Private Const OFN_OVERWRITEPROMPT = &H2
Private Const OFN_PATHMUSTEXIST = &H800
Private Const OFN_SAVE = 0
Private Const OFN_OPEN = 1
Private Function DialogFile(wMode As Integer, szDialogTitle As String,
szFileName As String, szFilter As String, szDefDir As String, szDefExt As
String) As String
Dim X As Long, OFN As OPENFILENAME, szFile As String, szFileTitle As String
With OFN
.lStructSize = Len(OFN)
.hwnd = Me.hwnd
.lpstrTitle = szDialogTitle
.lpstrFile = szFileName & String$(250 - Len(szFileName), 0)
.nMaxFile = 255
.lpstrFileTitle = String$(255, 0)
.nMaxFileTitle = 255
.lpstrFilter = NullSepString(szFilter)
.nFilterIndex = 2
.lpstrInitialDir = szDefDir
.lpstrDefExt = szDefExt
If wMode = 1 Then
OFN.Flags = OFN_HIDEREADONLY Or OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST
X = GetOpenFileName(OFN)
Else
OFN.Flags = OFN_HIDEREADONLY Or OFN_OVERWRITEPROMPT Or OFN_PATHMUSTEXIST
X = GetSaveFileName(OFN)
End If
If X <> 0 Then
If InStr(.lpstrFile, Chr$(0)) > 0 Then
szFile = Left$(.lpstrFile, InStr(.lpstrFile, Chr$(0)) - 1)
End If
DialogFile = szFile
Else
DialogFile = ""
End If
End With
End Function
Private Function NullSepString(ByVal CommaString As String) As String
Dim intInstr As Integer
Const vbBar = "|"
Do
intInstr = InStr(CommaString, vbBar)
If intInstr > 0 Then Mid$(CommaString, intInstr, 1) = vbNullChar
Loop While intInstr > 0
NullSepString = CommaString
End Function
Private Sub BrowseButton_Click()
On Error GoTo ErrHandler
With Me.FilePath
.Value = DialogFile(1, _
"Choose file...", _
vbNullString, _
"Microsoft Excel Book (*.xls)|*.xls", _
CurDir(), _
vbNullString)
End With
ExitHere:
Exit Sub
ErrHandler:
With Err
MsgBox "Error: " & .Number & vbCrLf & .Description, _
vbCritical Or vbOKOnly, .Source
End With
Resume ExitHere
End Sub
Why doesn't it work in Access 2003?
Thank you in advance for any tips!