What is wrong with the following code please??
I'm trying to run a sample file name dialog box exercise, but on clicking the Open button it always show "Cancel button was pressed", and dialog box never opens!!
Pasting code below. Using Access 2010. Thanks.
--------------------------------------------
Option Compare Database
Private Declare PtrSafe Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As LongLong
Private Type OPENFILENAME
lStructSize As Long
hwndOwner 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 Sub ShowFileDialog()
Dim MyFile As OPENFILENAME
Dim ReturnValue As LongLong
Dim strFilter As String
MyFile.lStructSize = Len(MyFile)
MyFile.hwndOwner = Me.Hwnd
strFilter = "Text Files (*.txt)" & Chr(0) & "*.TXT" & Chr(0)
MyFile.lpstrFilter = strFilter
MyFile.nFilterIndex = 1
MyFile.lpstrFile = String(257, 0)
MyFile.nMaxFile = Len(MyFile.lpstrFile) - 1
MyFile.lpstrFileTitle = MyFile.lpstrFile
MyFile.nMaxFileTitle = MyFile.nMaxFile
MyFile.lpstrInitialDir = "C:\"
MyFile.lpstrTitle = "Select a File"
MyFile.flags = 0
ReturnValue = GetOpenFileName(MyFile)
If ReturnValue = 0 Then
MsgBox "Cancel button was pressed"
Else
MsgBox MyFile.lpstrFile
End If
End Sub
Private Sub btnOpen_Click()
Call ShowFileDialog
End Sub
I'm trying to run a sample file name dialog box exercise, but on clicking the Open button it always show "Cancel button was pressed", and dialog box never opens!!
Pasting code below. Using Access 2010. Thanks.
--------------------------------------------
Option Compare Database
Private Declare PtrSafe Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As LongLong
Private Type OPENFILENAME
lStructSize As Long
hwndOwner 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 Sub ShowFileDialog()
Dim MyFile As OPENFILENAME
Dim ReturnValue As LongLong
Dim strFilter As String
MyFile.lStructSize = Len(MyFile)
MyFile.hwndOwner = Me.Hwnd
strFilter = "Text Files (*.txt)" & Chr(0) & "*.TXT" & Chr(0)
MyFile.lpstrFilter = strFilter
MyFile.nFilterIndex = 1
MyFile.lpstrFile = String(257, 0)
MyFile.nMaxFile = Len(MyFile.lpstrFile) - 1
MyFile.lpstrFileTitle = MyFile.lpstrFile
MyFile.nMaxFileTitle = MyFile.nMaxFile
MyFile.lpstrInitialDir = "C:\"
MyFile.lpstrTitle = "Select a File"
MyFile.flags = 0
ReturnValue = GetOpenFileName(MyFile)
If ReturnValue = 0 Then
MsgBox "Cancel button was pressed"
Else
MsgBox MyFile.lpstrFile
End If
End Sub
Private Sub btnOpen_Click()
Call ShowFileDialog
End Sub