Code to work with Win32 and Win64

psyc0tic1

Access Moron
Local time
Today, 15:20
Joined
Jul 10, 2017
Messages
360
I have this module to hide the close buttons in the access application to force the user to use my command button on the main form to close the database.

It has the PtrSafe added in to make it work with 64 bit systems but I have one user out of all users that is still on a 32 bit system.

Is anyone willing to modify the below code to work with both systems?
Code:
Private Const GWL_EXSTYLE = (-20)
Private Const GWL_STYLE = (-16)

Private Const WS_MAXIMIZEBOX = &H10000
Private Const WS_MINIMIZEBOX = &H20000
Private Const WS_SYSMENU = &H80000

Private Const HWND_TOP = 0
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const SWP_FRAMECHANGED = &H20
Private Const SWP_DRAWFRAME = SWP_FRAMECHANGED

Private Declare PtrSafe Function SetWindowLong Lib "user32" _
Alias "SetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long, ByVal dwNewLong As Long) _
As Long

Private Declare PtrSafe Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long) As Long

Private Declare PtrSafe Function SetWindowPos Lib "user32" _
(ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long

Sub HideAccessCloseButton()

   Dim lngStyle As Long

   lngStyle = GetWindowLong(hWndAccessApp, GWL_STYLE)
   lngStyle = lngStyle And Not WS_SYSMENU
   Call SetWindowLong(hWndAccessApp, GWL_STYLE, lngStyle)
   Call SetWindowPos(hWndAccessApp, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_DRAWFRAME)

End Sub

I tried using the IF and Else for the private declarations but it didn't work.
 
I tried this but it errors on everything after the #Else stating it needed to be updated for 64 bit systems:
Code:
#If VBA7 Then
    Private Declare PtrSafe Function SetWindowLong Lib "user32" _
        Alias "SetWindowLongA" (ByVal hwnd As Long, _
        ByVal nIndex As Long, ByVal dwNewLong As Long) _
        As Long

    Private Declare PtrSafe Function GetWindowLong Lib "user32" _
        Alias "GetWindowLongA" (ByVal hwnd As Long, _
        ByVal nIndex As Long) As Long

    Private Declare PtrSafe Function SetWindowPos Lib "user32" _
        (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
        ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
        ByVal cy As Long, ByVal wFlags As Long) As Long

#Else
    [COLOR="Red"]Private Declare Function SetWindowLong Lib "user32" _
        Alias "SetWindowLongA" (ByVal hwnd As Long, _
        ByVal nIndex As Long, ByVal dwNewLong As Long) _
        As Long

    Private Declare Function GetWindowLong Lib "user32" _
        Alias "GetWindowLongA" (ByVal hwnd As Long, _
        ByVal nIndex As Long) As Long

    Private Declare Function SetWindowPos Lib "user32" _
        (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
        ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
        ByVal cy As Long, ByVal wFlags As Long) As Long[/COLOR]
#End If
 
No, the #VBA7 is only one of the directives. I searched on that as that was what I remembered.
You need #Win64 I believe, see the links.
 
You also need to change Long to LongPtr for arguments that serve s pointers e.g hWnd.
Try this (not tested)

Code:
#If VBA7 Then
    Private Declare PtrSafe Function SetWindowLong Lib "user32" _
        Alias "SetWindowLongA" (ByVal hwnd As LongPtr, _
        ByVal nIndex As Long, ByVal dwNewLong As Long) _
        As Long

    Private Declare PtrSafe Function GetWindowLong Lib "user32" _
        Alias "GetWindowLongA" (ByVal hwnd As LongPtr, _
        ByVal nIndex As Long) As Long

    Private Declare PtrSafe Function SetWindowPos Lib "user32" _
        (ByVal hwnd As LongPtr, ByVal hWndInsertAfter As LongPtr, _
        ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
        ByVal cy As Long, ByVal wFlags As Long) As Long

#Else
    [COLOR="Red"]Private Declare Function SetWindowLong Lib "user32" _
        Alias "SetWindowLongA" (ByVal hwnd As Long, _
        ByVal nIndex As Long, ByVal dwNewLong As Long) _
        As Long

    Private Declare Function GetWindowLong Lib "user32" _
        Alias "GetWindowLongA" (ByVal hwnd As Long, _
        ByVal nIndex As Long) As Long

    Private Declare Function SetWindowPos Lib "user32" _
        (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
        ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
        ByVal cy As Long, ByVal wFlags As Long) As Long[/COLOR]
#End If

Don't worry about the 32-bit code showing in red on a 64-bit system. That's normal

However if the code is to be used with Access 2007 or earlier, you will need an additional step.
 
No, the #VBA7 is only one of the directives. I searched on that as that was what I remembered.
You need #Win64 I believe, see the links.

I tried the Win64 as well remembering ridders mentioning that in another one of my posts but it didn't change anything.

You are suggesting that I use the solution posted by arnelgp instead of what I am using??
 
No, merely showing the compiler directives.
Ridders has said the red is OK, but I think you should be using #Win64

arnelgp code uses that as well.?
 
You also need to change Long to LongPtr for arguments that serve s pointers e.g hWnd.
Try this (not tested)

Code:
#If VBA7 Then
    Private Declare PtrSafe Function SetWindowLong Lib "user32" _
        Alias "SetWindowLongA" (ByVal hwnd As LongPtr, _
        ByVal nIndex As Long, ByVal dwNewLong As Long) _
        As Long

    Private Declare PtrSafe Function GetWindowLong Lib "user32" _
        Alias "GetWindowLongA" (ByVal hwnd As LongPtr, _
        ByVal nIndex As Long) As Long

    Private Declare PtrSafe Function SetWindowPos Lib "user32" _
        (ByVal hwnd As LongPtr, ByVal hWndInsertAfter As LongPtr, _
        ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
        ByVal cy As Long, ByVal wFlags As Long) As Long

#Else
    [COLOR="Red"]Private Declare Function SetWindowLong Lib "user32" _
        Alias "SetWindowLongA" (ByVal hwnd As Long, _
        ByVal nIndex As Long, ByVal dwNewLong As Long) _
        As Long

    Private Declare Function GetWindowLong Lib "user32" _
        Alias "GetWindowLongA" (ByVal hwnd As Long, _
        ByVal nIndex As Long) As Long

    Private Declare Function SetWindowPos Lib "user32" _
        (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
        ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
        ByVal cy As Long, ByVal wFlags As Long) As Long[/COLOR]
#End If

Don't worry about the 32-bit code showing in red on a 64-bit system. That's normal

However if the code is to be used with Access 2007 or earlier, you will need an additional step.

Thanks Colin... everyone has access 2013 but just one user has it in 32 bit and all others including myself are on 64 bit versions.

Trying it now.
 

Users who are viewing this thread

Back
Top Bottom