Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

32-bit API OpenFile dialog not working 1

Status
Not open for further replies.

Goondu

Technical User
Jan 14, 2004
92
SG
Windows 7 and Access 2010 running in 64-bit.
(not WOW64)

The dialog did not appear but works in XP/Vista 32-bit and Access 2007 to 2000 32-bit. Note that PtrSafe is included for 64-bit version compatibility and there is no error message.

The return value of the API is False which should not be in this case. The code return "MsgBox "Cancel was pressed" " which indicates that it is returning False. The actual should be True.

[code/]
Option Compare Database
Option Explicit

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

Private Sub Form_Load()
Dim OFName As OPENFILENAME
OFName.lStructSize = Len(OFName)
'Set the parent window
OFName.hwndOwner = Me.Hwnd
'Set the application's instance
'OFName.hInstance = Application.hWndAccessApp 'app.hInstance
'Select a filter
OFName.lpstrFilter = "Text Files (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) + "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
'create a buffer for the file
OFName.lpstrFile = Space$(254)
'set the maximum length of a returned file
OFName.nMaxFile = 255
'Create a buffer for the file title
OFName.lpstrFileTitle = Space$(254)
'Set the maximum length of a returned file title
OFName.nMaxFileTitle = 255
'Set the initial directory
OFName.lpstrInitialDir = "C:\"
'Set the title
OFName.lpstrTitle = "Open File"
'No flags
OFName.flags = 0
'Debug.Print GetOpenFileName(OFName)
'Show the 'Open File'-dialog
If GetOpenFileName(OFName) Then
MsgBox "File to Open: " + Trim$(OFName.lpstrFile)
Else
MsgBox "Cancel was pressed"
End If
End Sub
[/code]

Are there anyone encountering this problem yet.
 
Ok, I tried using “CommDlgExtendedError” to trap the error. Here’s what I got.
Code:
The lStructSize member of initialization structure for the corresponding common dialog box is invalid.
Code:
Private Declare PtrSafe Function GetOpenFileName Lib “comdlg32.dll” Alias “GetOpenFileNameA” (pOpenfilename As OPENFILENAME) As Long

Private Type OPENFILENAME
     LStructSize As Long
…..so on…

I have tried the function return value as “LongPtr” and changed lStructSize to “LongPtr”. Unfortunately, it did not work.

I’m not an expert with APIs. I seems that Access2010 64-bit and Windows 7 64-bit doesn’t work with this API. (I believed it will work with Access2010 32-bit in Windows 7 64-bit under WOW64)

So far, has anyone tried this/these 32-bit API in a full 64-bit system/environment?
 
Thanks to HansV at Eileen's Lounge for the following:
Code:
[blue]#If VBA7 Then

Type OPENFILENAME
  lStructSize As Long
  hwndOwner As LongPtr
  hInstance As LongPtr
  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 LongPtr
  lpTemplateName As String
End Type

Private Declare PtrSafe Function GetOpenFileName Lib "comdlg32.dll" _
  Alias "GetOpenFileNameA" (OFN As OPENFILENAME) As Boolean

Declare PtrSafe Function GetSaveFileName Lib "comdlg32.dll" _
  Alias "GetSaveFileNameA" (OFN As OPENFILENAME) As Boolean

#Else

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 Declare Function GetOpenFileName Lib "comdlg32.dll" _
  Alias "GetOpenFileNameA" (OFN As OPENFILENAME) As Boolean

Private Declare Function GetSaveFileName Lib "comdlg32.dll" _
  Alias "GetSaveFileNameA" (OFN As OPENFILENAME) As Boolean

#End If[/blue]

Note the compiler directives to enable this to run under different versions of Access
 
Thanks strongm,

But that's not what I'm looking for. The Access 32-bit version will work but not 64-bit. The code will work in 32-bit (wow64).

Although the code like it says will work in a 64-bit environment, the example did not work in my computer which is running FULL 64-bit. The code you gave and I have tried it out, it's getting the same error as in my post. (besides, the code will not work in the 64-bit environment as the 32-bit API part will be red in color)
 
Found a solution while digging thru the internet.

The problem is this

.lStructSize = Len(OFN)

Change it to

.lStructSize = LenB(OFN)

Part of the datatype OPENFILENAME also needs to modify.
Code:
Type OPENFILENAME  
lStructSize As Long  
hwndOwner As [b]LongPtr[/b]
'hInstance may not be required to change? as it does not 
'cause the API to failed
hInstance As [b]LongPtr[/b]  
.... 
lpfnHook As [b]LongPtr[/b]  
....
End Type
Hope it helps someone.
 
>Part of the datatype

Um - just to be clear, those are the changes given in the code I posted from Eileen's Lounge.
 
strongm,

Yes, but changing the datatype won't work. That's just additional infomation I put up on the DataType. But the main problem was "Len()" function. The IStructSize need to be converted to a string with Byte, thus the "LenB()" is needed. Which is the case that I'm getting the error...
Code:
The lStructSize member of initialization structure for the corresponding common dialog box is invalid.

The compiler directive would work in 64-bit but in the VBA7 (Access2010 64-bit) code, the 32-bit part of the API will still show "Red". (It would compile but still in "Red".)

Hope that's clear.
 
PS. Probably a confusion somewhere as I did not post that I did changed the datatype from Long to LongPtr in "OPENFILENAME". Which of course did not work without changing the function from "Len()" to "LenB()".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top