Solved How to fix API declaration from 32-bit to 64-bit? (1 Viewer)

Sampoline

Member
Local time
Tomorrow, 10:41
Joined
Oct 19, 2020
Messages
144
Hi. I have a bit of code that is designed to disable an user from closing the database via the top-right corner "X". Instead they follow the method setup in my own database to Close the Database. I've done this as there are some data that may be left in limbo if the database isn't saved correctly.

Anyway, I found some code that was viable for 32-bit systems but wanted to fix it up so that it can work on 64-bit systems too. I've added the PtrSafe and LongPtr keywords to the code. So the Declare Functions work. But the actual code for disabling the close database is giving a compile error.

Code:
Private Declare PtrSafe Function GetSystemMenu Lib "user32" (ByVal hwnd As LongPtr, ByVal wRevert As Boolean) As LongPtr
Private Declare PtrSafe Function EnableMenuItem Lib "user32" (ByVal hMenu As LongPtr, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Boolean

Public Sub AccessCloseButtonEnabled(pfEnabled As Boolean)

On Error Resume Next

  Const clngMF_ByCommand As Long = &H0&
  Const clngMF_Grayed As Long = &H1&
  Const clngSC_Close As Long = &HF060&

  Dim lngWindow As Long
  Dim lngMenu As Long
  Dim lngFlags As Long

  lngWindow = Application.hWndAccessApp
  lngMenu = GetSystemMenu(lngWindow, 0)
  If pfEnabled Then
    lngFlags = clngMF_ByCommand And Not clngMF_Grayed
  Else
    lngFlags = clngMF_ByCommand Or clngMF_Grayed
  End If
  Call EnableMenuItem(lngMenu, clngSC_Close, lngFlags)

End Sub

I'm receiving a compile error type mismatch on lngMenu = GetSystemMenu(lngWindow, 0) .. how do I fix this?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 15:41
Joined
Oct 29, 2018
Messages
14,041
Hi. If you want to avoid using APIs, there's another method for preventing the user from closing Access other than your own Close button.

Otherwise, try changing your first API function to return a Long instead of a LongPtr.
 

Sampoline

Member
Local time
Tomorrow, 10:41
Joined
Oct 19, 2020
Messages
144
Hi. If you want to avoid using APIs, there's another method for preventing the user from closing Access other than your own Close button.

Otherwise, try changing your first API function to return a Long instead of a LongPtr.
Hi, I would love to hear your preferred method of preventing the user from closing Access! I just did it the API way because I got a lead on it from someone else's post.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 15:41
Joined
Oct 29, 2018
Messages
14,041
Hi, I would love to hear your preferred method of preventing the user from closing Access! I just did it the API way because I got a lead on it from someone else's post.
It's really very simple. The Form's UnLoad event can be canceled, so we take advantage of that.

Create a form that you can open at startup and leave it open until the user closes your app. Some people use a login form, a switchboard, or a main menu form for this. If you don't have those, you can just use a hidden form.

In the Open event of that form, you need to set a global variable to indicate whether it's okay to quit your app or not. For example,

Code:
TempVars!KeepOpen=True

Then, in the UnLoad event of your hidden Form, use something like:

Code:
Cancel=Nz(TempVars!KeepOpen,True)

Now, all need to do is set that variable to False when the user clicks your own Quit button.

Sent from phone...
 

Sampoline

Member
Local time
Tomorrow, 10:41
Joined
Oct 19, 2020
Messages
144
It's really very simple. The Form's UnLoad event can be canceled, so we take advantage of that.

Create a form that you can open at startup and leave it open until the user closes your app. Some people use a login form, a switchboard, or a main menu form for this. If you don't have those, you can just use a hidden form.

In the Open event of that form, you need to set a global variable to indicate whether it's okay to quit your app or not. For example,

Code:
TempVars!KeepOpen=True

Then, in the UnLoad event of your hidden Form, use something like:

Code:
Cancel=Nz(TempVars!KeepOpen,True)

Now, all need to do is set that variable to False when the user clicks your own Quit button.

Sent from phone...
Awesome, this is much easier! Thankyou!!
 

Users who are viewing this thread

Top Bottom