playing with commandbars

gebuh

Registered User.
Local time
Today, 14:29
Joined
Jun 16, 2006
Messages
21
Hi all, can anyone tell me what I'm doing wrong? I'm testing the code below that's supposed to enable all commandbars and make them visible if appropriate- but it is causing access to shutdown whenever I run it. If I can get it to work I want to be able to reset a users toolbars after my app has changed them-not perfect, I think this still dumps any customization the user had previously.

Code:
Public Function enableTools()
On Error GoTo Err_enableTools
'enable all toolbars
    Dim i As Integer
    For i = 1 To CommandBars.Count
        CommandBars(i).Enabled = True
        Debug.Print CommandBars(i).Name
        DoCmd.ShowToolbar CommandBars(i).Name, acToolbarWhereApprop
    Next i
Exit_enableTools:
    Exit Function
Err_enableTools:
     MsgBox Err.Number & " - " & Err.Description
    Resume Exit_gottaGo
End Function
 
Is the first cammand bar actually referenced with a '0' instead of a '1'. So if you had 3 command bars they would be 0, 1, & 2 as opposed to 1, 2 & 3.

If so this may be the problem - ?
 
could be- but then '0' commandbar would just not be enabled, this code is causing an error that shuts down the db.

KenHigg said:
Is the first cammand bar actually referenced with a '0' instead of a '1'. So if you had 3 command bars they would be 0, 1, & 2 as opposed to 1, 2 & 3.

If so this may be the problem - ?
 
If this is what is happening then your CommandBars.Count is returning say 7. Then if you don't subtract one from it, you would be referencing a command bar 7 which does not exist...?!?
 
If all your doing is turning the command bars on and off, do you even need the two lines below?

Debug.Print CommandBars(i).Name
DoCmd.ShowToolbar CommandBars(i).Name, acToolbarWhereApprop

I've turned the command bars 'off' on one of my db's but I only used:


Dim i As Integer
For i = 1 To CommandBars.Count
CommandBars(i).Enabled = False
Next i

May try taking those two lines out and see what happens.

HTH,
Shane
 
the debug... I put in for troubleshooting. the showToolbar, to turn toolbars on. enable/disable will actually remove the toolbars from the db- but it also changes any other db the user enters. If I just enable the commandbars, it won't return the toolbars to anything close to a standard configuration(it hasn't for me anyway). I was trying to reset the toolbars after my db closed so that when the user opened another db they would have regular toolbar settings.

ShaneMan said:
If all your doing is turning the command bars on and off, do you even need the two lines below?

Debug.Print CommandBars(i).Name
DoCmd.ShowToolbar CommandBars(i).Name, acToolbarWhereApprop

I've turned the command bars 'off' on one of my db's but I only used:


Dim i As Integer
For i = 1 To CommandBars.Count
CommandBars(i).Enabled = False
Next i

May try taking those two lines out and see what happens.

HTH,
Shane
 
gebuh said:
the debug... I put in for troubleshooting. the showToolbar, to turn toolbars on. enable/disable will actually remove the toolbars from the db- but it also changes any other db the user enters. If I just enable the commandbars, it won't return the toolbars to anything close to a standard configuration(it hasn't for me anyway). I was trying to reset the toolbars after my db closed so that when the user opened another db they would have regular toolbar settings.

Ok, I understand better now, but will also have to confess to it being over my head. I did do a quick Google search using "CommandBars(i).Enabled" and came up with a good number of hits, so maybe you can do some searching and come up with a post of someone, somewhere, who's tried do something similar to this.

Sorry I couldn't be of more help,
Shane
 
The only way I know do do this is to implicitly turn the m all off and back on...

He's a cut a paste from a sub I run in one of my db"s....

Code:
DoCmd.ShowToolbar "Menu Bar", acToolbarNo
DoCmd.ShowToolbar "Database", acToolbarNo
DoCmd.ShowToolbar "Relationship", acToolbarNo
DoCmd.ShowToolbar "Table Design", acToolbarNo
DoCmd.ShowToolbar "Table Datasheet", acToolbarNo
DoCmd.ShowToolbar "Query Design", acToolbarNo
DoCmd.ShowToolbar "Query Datasheet", acToolbarNo
DoCmd.ShowToolbar "Form Design", acToolbarNo
DoCmd.ShowToolbar "Form View", acToolbarNo
DoCmd.ShowToolbar "Filter/Sort", acToolbarNo
DoCmd.ShowToolbar "Report Design", acToolbarNo
DoCmd.ShowToolbar "Print Preview", acToolbarNo
DoCmd.ShowToolbar "Toolbox", acToolbarNo
DoCmd.ShowToolbar "Formatting (Form/Report)", acToolbarNo
DoCmd.ShowToolbar "Formatting (Datasheet)", acToolbarNo
DoCmd.ShowToolbar "Macro Design", acToolbarNo
DoCmd.ShowToolbar "Utility 1", acToolbarNo
DoCmd.ShowToolbar "Utility 2", acToolbarNo
DoCmd.ShowToolbar "Web", acToolbarNo
DoCmd.ShowToolbar "Source Code Control", acToolbarNo
 
thanx for the info, I was doing that, but I was experimenting with trying to find a way to return the user's settings to what they were before they used my db. Guess I'll have to keep looking.
 
gHudson, Pat, SJ or Doc or one of the other more experienced developers around here should know how to do this - :):)
 
GHudson posted a redone db a while back [AnthemNPI] sorry can't remember name of the thread, but it had a button to restore default toolbars.

AnthemNPI-Code to Hide/Restore Toolbars - GHudson

======================
Private Sub btnRestoreToolbars_Click()

'ghudson 5/12/2006
'unhide all menu bars and tool bars
Dim i As Integer
For i = 1 To CommandBars.Count
CommandBars(i).Enabled = True
Next i

End Sub
==========================
Private Sub Form_Open(Cancel As Integer)
' Minimize the database window and initialize the form.
' Move to the switchboard page that is marked as the default.
Me.Filter = "[ItemNumber] = 0 AND [Argument] = 'Default' "
Me.FilterOn = True

'ghudson 5/12/2006
'hide the database window
DoCmd.SelectObject acTable, , True
DoCmd.RunCommand acCmdWindowHide

'ghudson 5/12/2006
'hide all menu bars and tool bars
Dim i As Integer
For i = 1 To CommandBars.Count
CommandBars(i).Enabled = False
Next i

End Sub
========================
hope i've reproduced it accurately - worked well for me. Not sure how useful it will be for you out of context like that.

Edit: think you can find the gist of it here:
http://www.access-programmers.co.uk/forums/showthread.php?t=97578
 
Last edited:
I think I have a solution to the commandBars problem- this module captures the users toolbars, turns them off, turns on my custom toolbars and when my application closes resets the toolbars to their original settings. Any comments welcome

Code:
Option Compare Database
'array to hold users current toolbar settings
    Global myGlobalCBarArray() As String
Public Function createMyArray()
'create array to store users toolbar settings
'place a ref to this function in onload of main form of app-but make sure
'it only runs once when your db opens-otherwise you'll lose the original settings
'   ex:
'Private Sub Form_Load()
'   createMyArray
'End Sub
'this will collect users current settings in a global array

On Error GoTo Err_createMyArray

    Dim barCnt As Integer
    Dim cnt As Integer
'get number of visible commandBars, will dim array size
    For Each cBar In CommandBars
        If cBar.Visible = True Then
            barCnt = barCnt + 1
        End If
    Next

'store commandBar names in array
    barCnt = barCnt - 1  'watch fencepost errors- array starts at 0
    ReDim myGlobalCBarArray(barCnt)
    For Each cBar In CommandBars
            If cBar.Visible = True Then
                myGlobalCBarArray(cnt) = cBar.Name
                cnt = cnt + 1
            End If
    Next
    
Exit_createMyArray:
    Exit Function
Err_createMyArray:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_createMyArray
End Function
Public Function allOff()
'
'turns everything but my toolbars off
'call this from switchboard open or load event

On Error GoTo Err_allOff

    Dim i As Integer
'disable all commandBars
    For i = 1 To CommandBars.Count
        CommandBars(i).Enabled = False
    Next i
'enable my toolbars, your custom toolbars go here
    CommandBars("onBar").Enabled = True
    DoCmd.ShowToolbar "onBar", acToolbarYes
    CommandBars("offBar").Enabled = True
    DoCmd.ShowToolbar "offBar", acToolbarYes
    
Exit_allOff:
    Exit Function
    
Err_allOff:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_allOff
End Function
Public Function allOn()
'put this function at close of application- restores user's original settings

On Error GoTo Err_allOn

    Dim i As Integer
'disable all commandBars
    For i = 1 To CommandBars.Count
        CommandBars(i).Enabled = False
    Next i
    
'enable and show previously set toolbars
    For cnt = 0 To UBound(myGlobalCBarArray)
        CommandBars(myGlobalCBarArray(cnt)).Enabled = True
        DoCmd.ShowToolbar myGlobalCBarArray(cnt), acToolbarYes
    Next

Exit_allOn:
    Exit Function
    
Err_allOn:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_allOn
End Function
 

Users who are viewing this thread

Back
Top Bottom