File Dialog Box??

khurram7x

Registered User.
Local time
Today, 18:06
Joined
Mar 4, 2015
Messages
226
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 would try declaring ReturnValue as a string and test for its length being greater than 0.
 
Thanks. I changed the ReturnValue to String and its length is 1, but when ReturnValue is LongLong it returns 8. Why is it so? What does it mean?

One interesting this is that 'strFilter' value is just first few characters of the actual string and not the full string, as seen in the attachment. Why is it behaving like this?
 

Attachments

  • strFilter.JPG
    strFilter.JPG
    65.7 KB · Views: 128
Some changes:
Code:
Private Declare PtrSafe Function GetOpenFileName Lib "comdlg32.dll" Alias _
                                 "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long

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

Public Sub ShowFileDialog()
    Dim MyFile      As OPENFILENAME
    Dim ReturnValue As Long
    Dim strFilter   As String
    
    With MyFile
        .lStructSize = Len(MyFile)
        .hwndOwner = Me.hWnd
        strFilter = "Text Files (*.txt)" & Chr(0) & "*.TXT" & Chr(0)
        .lpstrFilter = strFilter
        .nFilterIndex = 1
        .lpstrFile = String$(257, 0)
        .nMaxFile = Len(MyFile.lpstrFile) - 1
        .lpstrFileTitle = MyFile.lpstrFile
        .nMaxFileTitle = MyFile.nMaxFile
        .lpstrInitialDir = "C:\"
        .lpstrTitle = "Select a File"
        .flags = 0
    End With
    
    ReturnValue = GetOpenFileName(MyFile)
    
    If ReturnValue = 0 Then
        MsgBox "Cancel button was pressed"
    Else
        MsgBox MyFile.lpstrFile
    End If
End Sub
... but why don't you use the simple FileDialog from Office?
 
Thanks for the code, but it didn't work. i found out that it is because I'm using 64-bit version of Access and comdlg32.dll doesn't work for it.

Thanks for FileDialog tip, I worked on it and it worked well. I didn't know about it, cuz most of you people probably know that I'm still in learning phase.

Could you guys please tell me the difference Office.FileDialog and Application.FileDialog, and when to use it please?
 
Last edited:

Users who are viewing this thread

Back
Top Bottom