2

I am trying to implement a script which would disable "close" button of the ms access window.

However, I get compilation error when trying to declare functions:

Option Compare Database
Option Explicit
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal wRevert As Long) As Long
Private Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long

Public Sub AccessCloseButtonEnabled(pfEnabled As Boolean)
  ' Comments: Control the Access close button.
  '           Disabling it forces the user to exit within the application
  ' Params  : pfEnabled       TRUE enables the close button, FALSE disabled it
  ' Owner   : Copyright (c) FMS, Inc.
  ' Source  : Total Visual SourceBook
  ' Usage   : Permission granted to subscribers of the FMS Newsletter

  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

My Error

Translation: Error in the compilation of the functions. Syntax Error in a Visual Basic Module. Check the program, and then recompile it.


What do you think might have caused the problem?

3
  • It talks about some functions, and you are having a Sub. Is there some other code? In general, this is a compiling error, thus something is not written as it should be. I have tried the code above, it compiles. Take a look here - superuser.com/questions/1160671/… Commented May 23, 2018 at 9:12
  • If you are asking question on multinational forum, it would very kind of you to transalte your error to english, instead of posting it in your language. Commented May 23, 2018 at 9:29
  • 1
    I use the very same function and API declarations, and they work in 32bit Access 10. Do you perhaps use 64bit Office? If not, can you translate that error message for us? Commented May 23, 2018 at 9:29

1 Answer 1

3

The declarations aren't 64-bit compatible, and you're using longs instead of booleans for wRevert and the return of EnableMenuItem. You can try the following (needs VBA7 (Office 2010+) for LongPtr support):

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.