Module, VB, inherted nightmare!

Elphaba31

Registered User.
Local time
Yesterday, 19:09
Joined
Mar 24, 2010
Messages
17
HI Everyone,

I have an inherited Access database, that my boss converted to Access 2007. I am not a user or developer of Access or VB, but have inherited these nightmares to convert and repair.So can anyone tell me why this code does work (red line):
Code:
Option Compare Database
Option Explicit
Private Declare Function GetSystemMenu Lib "User32" (ByVal hWnd As Long, _
                                                     ByVal bRevert As Long) As Long
Private Declare Function EnableMenuItem Lib "User32" (ByVal hMenu As _
                                                      Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
Const MF_GRAYED = &H1&
Const MF_BYCOMMAND = &H0&
Const SC_CLOSE = &HF060&
Public Function SetEnabledState(blnState As Boolean)
    Call CloseButtonState(blnState)
    Call ExitMenuState(blnState)
End Function
'Disable the Menu Option
Sub ExitMenuState(blnExitState As Boolean)
    [COLOR=red] Application.CommandBars("File").Controls("Exit").Enabled = blnExitState
    [/COLOR]End Sub
    'Disable the Close Button Option
Sub CloseButtonState(boolClose As Boolean)
    Dim hWnd As Long
    Dim wFlags As Long
    Dim hMenu As Long
    Dim result As Long

    hWnd = Application.hWndAccessApp
    hMenu = GetSystemMenu(hWnd, 0)
    If Not boolClose Then
        wFlags = MF_BYCOMMAND Or MF_GRAYED
    Else
        wFlags = MF_BYCOMMAND And Not MF_GRAYED
    End If

    result = EnableMenuItem(hMenu, SC_CLOSE, wFlags)
End Sub
Thanks!!

Elphaba
 
Last edited by a moderator:
Do you really have a space in the word "Enabled"?
.E nabled

I'd start by getting rid of the space, but that seems too obvious.
I don't have Acc2007.
 
Actually no; had to look though, lol, was almost ready to accept the "Yes, today I'm that Stupid" award, lol. It seems to be a weird glitch, but the word is "Enabled" does not have a space in it....
 
Actually no; had to look though, lol, was almost ready to accept the "Yes, today I'm that Stupid" award, lol. It seems to be a weird glitch, but the word is "Enabled" does not have a space in it....
It is a problem with the vBulletin software for the forum. But if you use CODE TAGS (like you should anyway) then there is no problem with that.

See how to add code tags for the future (I went in and edited the original message to add them):

codetag001.png
 
I am "guessing" that the problem with the command you high lighted in red is Acces 2007 does not have menu and command bars like older versions of Access 2003 did. Access 2007 and newer uses the Ribbon so your code is choking on something that does not exist in Access 2007. Your code is trying to disable the red X in the upper right corner of the Access application window to prevent a user from closing Access.

The code I used to use with Access 2003 to disable the application close X button does not work well with Access 2007. My work around was to use the below code in the Form_Unload event for each form.

Code:
Dim booCloseForm As Boolean

Code:
Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Err_Form_Unload

    If booCloseForm = False Then
        Cancel = True
        MsgBox "You must use the Close Database button to exit the database!", vbCritical, "Invalid Close Database Attempt"
    End If

Exit_Form_Unload:
    Exit Sub

Err_Form_Unload:
        MsgBox Err.Number & " - " & Err.Description, vbCritical, "Form_Unload()"
        Resume Exit_Form_Unload

End Sub
To allow the database to be closed, I set the booCloseForm to True in my custom Close database button.

Code:
booCloseForm = True
DoCmd.RunCommand acCmdExit

HTH
 
That's a good point ghudson. Unless they have a customised menu-bar which should still work with the code. But I doubt it can be called it "File".
 

Users who are viewing this thread

Back
Top Bottom