64-bit Issue when Compiling

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DJRhino1175
    New Member
    • Aug 2017
    • 221

    #1

    64-bit Issue when Compiling

    Found this code off a google search to remove the close "X" from the title bar. But when I go to compile it throws an error.

    Compile error: The code in this project must be updated for use on 64-bit systems. Please review and update Declare statements and then mark them PtrSafe attribute.

    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 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
    
    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
    What do I need to do to fix this? I get it on all 3 Private Declare Functions.
    Last edited by NeoPa; Jun 9 '19, 07:18 PM.
  • twinnyfo
    Recognized Expert Moderator Specialist
    • Nov 2011
    • 3665

    #2
    DJ - Try this instead. It simply disables the button (or enables it).

    Much easier to use. Put it in a standalone module and call it from anywhere else in your project.

    Code:
    '******************************************************************************
    '* This Procedure enables or disables the 'Close' button on the Access Window *
    '* This prevents Users from accidentally closing the DB when they really      *
    '* only want to close a Report in Preview Mode.                               *
    '******************************************************************************
    Public Sub AccessCloseButtonEnabled(fEnabled 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 fEnabled Then
            lngFlags = clngMF_ByCommand And Not clngMF_Grayed
        Else
            lngFlags = clngMF_ByCommand Or clngMF_Grayed
        End If
    
        Call EnableMenuItem(hMenu:=lngMenu, _
                            wIDEnableItem:=clngSC_Close, _
                            wEnable:=lngFlags)
    
    End Sub
    I don't think it requires any additional declarations. Let me know if this works.

    Comment

    • DJRhino1175
      New Member
      • Aug 2017
      • 221

      #3
      The code itself compiles, but my callout does not.

      Code:
      Private Sub Form_Load()
      
          Call AccessCloseButtonEnabled
      
      End Sub
      Compile error: Argument not optional.

      Comment

      • twinnyfo
        Recognized Expert Moderator Specialist
        • Nov 2011
        • 3665

        #4
        Argument again! I’ll ask the mods to let you figure this one out for now.

        Easy solution. Just read some of your previous posts.

        Comment

        • DJRhino1175
          New Member
          • Aug 2017
          • 221

          #5
          Your right it was an easy solution, so I got that fixed, still doesn't compile, but do to another spot in the public function

          Code:
          lngMenu = GetSystemMenu(lngWindow, 0)
          Throws an error of:

          Compile error:
          Sub or function not defined

          Any Idea on why this is doing this?

          If I comment this out I get an error here:

          Code:
          Call EnableMenuItem(hMenu:=lngMenu, _
                                  wIDEnableItem:=clngSC_Close, _
                                  wEnable:=lngFlags)
          Same error as above.

          Comment

          • twinnyfo
            Recognized Expert Moderator Specialist
            • Nov 2011
            • 3665

            #6
            This should do it. I couldn’t remember the other declaration associated with it. But, you did a good job of troubleshooting to identify where the problem was. All part of the process.

            Code:
            Option Compare Database
            Option Explicit
            
            Private Declare PtrSafe Function GetSystemMenu Lib "user32" ( _
                ByVal hwnd As Long, _
                ByVal bRevert As Long) _
                As Long
            
            '******************************************************************************
            '* This Procedure enables or disables the 'Close' button on the Access Window *
            '* This prevents Users from accidentally closing the DB when they really      *
            '* only want to close a Report in Preview Mode.                               *
            '******************************************************************************
            Public Sub AccessCloseButtonEnabled(fEnabled 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 fEnabled Then
                    lngFlags = clngMF_ByCommand And Not clngMF_Grayed
                Else
                    lngFlags = clngMF_ByCommand Or clngMF_Grayed
                End If
             
                Call EnableMenuItem(hMenu:=lngMenu, _
                                    wIDEnableItem:=clngSC_Close, _
                                    wEnable:=lngFlags)
             
            End Sub
            Last edited by twinnyfo; Jun 8 '19, 03:04 PM. Reason: Added key word "PtrSafe" fir 64 bit systems

            Comment

            • twinnyfo
              Recognized Expert Moderator Specialist
              • Nov 2011
              • 3665

              #7
              I will have to get back to this on Monday. I am missing a function that I have at work.

              Comment

              • twinnyfo
                Recognized Expert Moderator Specialist
                • Nov 2011
                • 3665

                #8
                I found it!

                Here is the entire code:

                Code:
                Option Compare Database
                Option Explicit
                
                Public Declare PtrSafe Function _
                    GetSystemMenu _
                    Lib "user32" ( _
                    ByVal hwnd As Long, _
                    ByVal bRevert As Long) _
                    As Long
                
                Public Declare PtrSafe Function _
                    EnableMenuItem _
                    Lib "user32" ( _
                    ByVal hMenu As Long, _
                    ByVal wIDEnableItem As Long, _
                    ByVal wEnable As Long) _
                    As Long
                
                '******************************************************************************
                '* This Procedure enables or disables the 'Close' button on the Access Window *
                '* This prevents Users from accidentally closing the DB when they really      *
                '* only want to close a Report in Preview Mode.                               *
                '******************************************************************************
                Public Sub AccessCloseButtonEnabled(fEnabled 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 fEnabled Then
                        lngFlags = clngMF_ByCommand And Not clngMF_Grayed
                    Else
                        lngFlags = clngMF_ByCommand Or clngMF_Grayed
                    End If
                 
                    Call EnableMenuItem(hMenu:=lngMenu, _
                                        wIDEnableItem:=clngSC_Close, _
                                        wEnable:=lngFlags)
                
                End Sub

                Comment

                • ADezii
                  Recognized Expert Expert
                  • Apr 2006
                  • 8834

                  #9
                  Code:
                  Public Declare PtrSafe Function GetSystemMenu Lib "user32" (ByVal hwnd As LongPtr, ByVal bRevert As Long) As LongPtr
                  Public Declare PtrSafe Function EnableMenuItem Lib "user32" (ByVal hMenu As LongPtr, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
                  Code:
                  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
                  Code:
                  Call AccessCloseButtonEnabled(True)
                  Code:
                  Call AccessCloseButtonEnabled(False)

                  Comment

                  • DJRhino1175
                    New Member
                    • Aug 2017
                    • 221

                    #10
                    Thanks everyone, works perfectly with no issues. You guys are the best. Hope everyone was a great week.

                    Comment

                    Working...