Hide/Unhide database window with VBA code

inoxo

Registered User.
Local time
Today, 10:03
Joined
Sep 8, 2005
Messages
42
Hello,

I woul like to programatically (with VBA code) hide and unhide the database window.

In Access 97, this can be done with Tools > Startup > Display database window box (check/uncheck).

But I would like my program does that himself : when the user launches the application, the database window should be hidden and when he leaves, the database window should be shown again.

Thanks.
 
Paste the following code into a new module:
Code:
Option Explicit

Private Declare Function GetClassNameA Lib "user32" ( _
    ByVal hwnd As Long, _
    ByVal lpClassName As String, _
    ByVal nMaxCount As Long) _
    As Long
Private Declare Function GetWindow Lib "user32" ( _
    ByVal hwnd As Long, _
    ByVal wCmd As Long) _
    As Long
Private Declare Function ShowWindowAsync Lib "user32" ( _
    ByVal hwnd As Long, _
    ByVal nCmdShow As Long) _
    As Boolean

Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5

Private Const SW_HIDE = 0
Private Const SW_SHOW = 5

Private Function GetClassName( _
    ByVal hwnd As Long) _
    As String

    Dim lpClassName As String
    Dim lLen As Long

    lpClassName = String(255, 32)
    lLen = GetClassNameA(hwnd, lpClassName, 255)
    If lLen > 0 Then
        GetClassName = Left(lpClassName, lLen)
    End If

End Function

Public Sub ShowDbWindow(ByVal bCmdShow As Boolean)

Dim hWndApp As Long

hWndApp = GetWindow(Application.hWndAccessApp, GW_CHILD)
Do Until hWndApp = 0
    If GetClassName(hWndApp) = "MDIClient" Then
        Exit Do
    End If
    hWndApp = GetWindow(hWndApp, GW_HWNDNEXT)
Loop

If hWndApp > 0 Then
    hWndApp = GetWindow(hWndApp, GW_CHILD)
    Do Until hWndApp = 0
        If GetClassName(hWndApp) = "ODb" Then
            Exit Do
        End If
        hWndApp = GetWindow(hWndApp, GW_HWNDNEXT)
    Loop
End If

If hWndApp > 0 Then
    ShowWindowAsync hWndApp, IIf(bCmdShow, SW_SHOW, SW_HIDE)
End If

End Sub

In the launch code of your application execute the following line:
Code:
ShowDbWindow False

In the exit code of your application execute the following line:
Code:
ShowDbWindow True
 
Simple, elegant, but requires changing the active selected object. My code does not involve the SelectObject command and works as well. Both are excellent methods, depending on your needs.
 
I have tried things like unchecking the database window, and this does remove the view to the background elements (like tables, forms, etc...) for the users. However, they can still go in to the same menu option and reenable it if they want.

Is there a way to hide these elements, compile the file into some kind of executable, and distribute it to the other users? That way, all they see is the switchboard (which opens on execution). There is no menu options, no possible changes to the database possible, only USE?!?

Thank you for all your replies... I am still trying them all to see which best fits this issue. Thank you again.
 
Hello,

Thanks for the posting... but I was not successful.

1. I posted your codes in a new module (Called 'Module1')
2. I then opened the form (the one I wanted to be displayed) in design mode - clicked on "On Load" Event Procedure - and pasted the 'ShowDbWindow True' code.
3. There was no On Exit event found. So I am stuck.
4. I closed the db and re-run the db again - just to see - it opened with every objects on, with db window - just like it used to.

Where and what am I doing wrong? Thanks.
 
Hello, manojjain,

Paste the ShowDbWindow False statement in the Form_Load() event, and the ShowDbWindow True statement in the Form_Unload() event.
 
Thank you ByteMyzer, I found this useful. Ridders would tell me I should know enough to handle this but in my environment "Certain users" are allowed to use the MDB instead of the MDE so I have to leave the F11 option open to them.
 
Hi insane_ai

I doubt ByteMyzer will see your post as he/she hasn't posted for 5 years!

I use much simpler code by Dev Ashish to do this which I've modified for use in both 32-bit & 64-bit

Code:
Option Compare Database
Option Explicit

'************ Code Start **********
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Global Const SW_HIDE = 0
Global Const SW_SHOWNORMAL = 1
Global Const SW_SHOWMINIMIZED = 2
Global Const SW_SHOWMAXIMIZED = 3

'###############################################
#If VBA7 Then
    Private Declare PtrSafe Function apiShowWindow Lib "user32" Alias "ShowWindow" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
#ElseIf Win64 Then 'need datatype LongPtr
    Private Declare PtrSafe Function apiShowWindow Lib "user32" Alias "ShowWindow" (ByVal hWnd As LongPtr, ByVal nCmdShow As Long) As Long
#Else '32-bit Office
    Private Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
#End If
'###############################################

Function SetAccessWindow(nCmdShow As Long)

    'Usage Examples
    'Maximize window:
    ' ?SetAccessWindow(SW_SHOWMAXIMIZED)
    'Minimize window:
    ' ?SetAccessWindow(SW_SHOWMINIMIZED)
    'Hide window:
    ' ?SetAccessWindow(SW_HIDE)
    'Normal window:
    ' ?SetAccessWindow(SW_SHOWNORMAL)
    
    Dim loX As Long
   ' Dim loForm As Form
    On Error Resume Next
    
    loX = apiShowWindow(hWndAccessApp, nCmdShow)
    SetAccessWindow = (loX <> 0)

End Function

As it says above, just use this line in the Form_Load event:
Code:
SetAccessWindow (SW_SHOWMINIMIZED)

Have a look at this example app to see this and other related code in use:
Show/hide application window, navigation pane, ribbon, taskbar

Also not sure I understand why standard users need to have access to special keys like F11. Admin users could be allowed additional functionality e.g. have an Admin button that displays the app window/nav pane & allows access to the VBE (or similar)
 
@Insane_ai:

If you have "certain users" who are allowed to use the MDB instead of the MDE, could you not simply modify your code not to hide those objects from them when they are in the MDB as opposed to the MDE?

Even so, giving any end-user level of access to the MDB file is frowned upon, as a rule. What is the requirement for this?
 

Users who are viewing this thread

Back
Top Bottom