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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open Dialog Box in 32-bit and 64-bit Compile Problem

Status
Not open for further replies.

joel009

Programmer
Jul 7, 2000
272
US
Hi All,
I have been trying to get the API calls for a common dialog box to work for both the 32-bit and 64-bit Access versions and it works fine for the 32-bit applications but won't compile in the 64-bit environment. I have found some code recomended for this but I can't get it to work. Here is the code I a working with.
#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
When I open or try to compile the db it gives me the warning that the Function Declaration after the Else statement require the PTRSafe.

Is there a way around this? What am I doing wrong?

Joel
 
The resolution for your problem is this code

Option Compare Database
Option Explicit

'Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
#If Win64 Then
Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
#Else
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
#End If


Public Function OpenFile(sFileName As String)
On Error GoTo Err_OpenFile

OpenFile = ShellExecute(Application.hWndAccessApp, "Open", sFileName, "", "C:\", 1)

Exit_OpenFile:
Exit Function

Err_OpenFile:
MsgBox Err.Number & " - " & Err.Description
Resume Exit_OpenFile

End Function

Public Function TestOpeningFile()
On Error GoTo Err_TestOpeningFile

OpenFile "C:\Windows\Win.ini"

Exit_TestOpeningFile:
Exit Function

Err_TestOpeningFile:
MsgBox Err.Number & " - " & Err.Description
Resume Exit_TestOpeningFile

End Function



If you need more help you can ask me
 
Thanks for the reply JoseMarques. I will have to wait until tomorrow to try this. Part of my problem is our COE is not consistant (imagine that). I have been told that I can have a combination of win64 and VBA6 or VBA7 so I could change it to:
#If WIN64 And VBA7 then
Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
#Else
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
#End If
So far I can get it to work properly without the #If statment in either VBA 6 or 7 with the correct functon declaration, I just have to get the multiple conditions to compile and work.

I'll let you know.

Joel
 
JoseMarques - That is not what I am looking for. This uses the shell command to open a file. What I am trying to do is open the common dialog box allowing the user to select a file then after the common dialog box selection is made I can continue processing the selected file.

Putting together some things I found on the web I came up with:
Option Compare Database
Option Explicit

Private Type tagOPENFILENAME
lStructSize As Long
hwndOwner As LongPtr
hInstance As LongPtr
strFilter As String
strCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
strFile As String
nMaxFile As Long
strFileTitle As String
nMaxFileTitle As Long
strInitialDir As String
strTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
strDefExt As String
lCustData As Long
lpfnHook As LongPtr
lpTemplateName As String
End Type

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

Private Declare PtrSafe Function aht_apiGetSaveFileName Lib "comdlg32.dll" _
Alias "GetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean
Private Declare PtrSafe Function CommDlgExtendedError Lib "comdlg32.dll" () As Long

I also had to change:

.lStructSize = LenB(OFN)

hopefully to get it to work in 64 bit environment. I've tested it in a 32 bit environment but will have to wait until tomorrow to test in a 64 bit environment.


Joel
 
What I sent to you was an example of where you have to start.
It is not easy but I will help you out.

If I understand you're using an old work on a new access.
work made in Access 2003 and 2007 to be edit in Access 2010 you have some problems of code not only for VB7 but so for references tools. In a job where I had to improve this happened.
 
try:
All 'Long' have to be replace 'LongPtr'

Private Type tagOPENFILENAME
lStructSize As LongPtr
hwndOwner As LongPtr
hInstance As LongPtr
strFilter As String
strCustomFilter As String
nMaxCustFilter As LongPtr
nFilterIndex As LongPtr
strFile As String
nMaxFile As LongPtr
strFileTitle As String
nMaxFileTitle As LongPtr
strInitialDir As String
strTitle As String
Flags As LongPtr
nFileOffset As Integer
nFileExtension As Integer
strDefExt As String
lCustData As LongPtr
lpfnHook As LongPtr
lpTemplateName As StringEnd Type
 
My posted code is working in Access 32 or 64 bit on OS with 32 or 64 bit. I am not worried about backwards compatibility to previous versions of Access so I don't have to play with the #if #else #end if for WIN64 or VBA7 which simplified it a bit. Thanks for the help!
I also found some very good information in Access help, shoulda looked there first.

Joel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top