Toggle button on the toolbar

J

John

Hi

How can I create a toggle button on a toolbar that would enable/disable
editing on the current form?

Thanks

Regards
 
A

Al Camp

John,
After you place any button on the menu bar, in Customize mode, you can
right click it and customize it. A button can call a Macro, or a Function
in the form's module, by using the Properties/OnAction entry for the button.
If you don't want a button Enable and another for Disable, you'll need to
add a "trap" to your allow your enable/disable function to toggle.
If Me.AllowEdits = True Then...
Me.AllowEdits = False
Else
Me.AllowEdits = True
End If
I didn't test the code, but it should fly...
 
J

John

Thanks. How does one "toggle" the single toggle button i.e. the button
clicks in on first click and clicks out on next click and so on?

Regards
 
A

Al Camp

John,
Menu buttons don't toggle "visually" like a real toggle button control on
a form.
Menu buttons can be text only, (no icon) so you could also alternate the
Name on the button from Edit to NoEdit to Edit.... as an indication as to
what state the AllowEdits is set to.
This should clearly indicate the "toggle" to the user... without the
up/down button visiual.
 
A

Al Camp

John,
I tested my Name toggling, and am having a problem figuring out how to
programatically address the Name property of a button on the menu bar. I'll
try to figure it out and get back to you.
In the meantime, an small unbound text control on your form with...
= IIF(AllowEdits = True, "Enabled", "Disabled)
would give the user a good "visual" toggle value.
I always have a similar field on my forms that says "Filtered" or "All
Records"
Give me some time to "finger" this one out...
hth
Al Camp
Candia Computer Consulting - Candia NH
http://home.comcast.net/~cccsolutions
 
P

pietlinden

Toggle the AllowEdits property in the Open event of the form.
Me.AllowEdits = Not(Me.AllowEdits)

or something along those lines.
 
M

Marshall Barton

Al said:
I tested my Name toggling, and am having a problem figuring out how to
programatically address the Name property of a button on the menu bar. I'll
try to figure it out and get back to you.
In the meantime, an small unbound text control on your form with...
= IIF(AllowEdits = True, "Enabled", "Disabled)
would give the user a good "visual" toggle value.
I always have a similar field on my forms that says "Filtered" or "All
Records"
Give me some time to "finger" this one out...


Al, It's the Caption property, not the Name property

To set the up/down appearance, use:

With Application.CommandBars("MyBar").Controls!MyPrint]
.State =True 'Down
End With
 
T

Tim Marshall

John said:
How can I create a toggle button on a toolbar that would enable/disable
editing on the current form?

I'll assume you know how to create a custom menu and menu item. If not
write back and I'll write the "prequel" to the following! 8)

Write a function and save in a standard module. The function is as
written in air code following my comments below.

Make the function the on action item for the menu item, ie, for the
following, you need to enter:

=fToggleEditNamedForm(Forms!MyForm)

The Menu item can be called "Edit Form" or something and will be
ticked/depressed when in edit mode and unticked/not depressed when not
in edit mode. The "State" property use in my code below will add a tick
beside the menu item if this is a tool bar (WARNING, if you have a
picture for the item, the tick will NOT show) or depressed/not depressed
if this is a menu bar.

Alternately, instead of Forms!MyForm as an argument, you may choose to
use Screen.ActiveForm, but make certain when this menu item is used,
that the correct form is displayed!

Here's the function (air code). Note that the item in the commandbars
reference is the number of the control from the left. You won't need
any references set up for the items in the function, as everything runs
fom vba just fine:

Function fToggleEditMyForm(f as Access.Form)

'This function toggles the allowedit property of the form
'passed in the argument, f.
'
'f is the form being toggled.

'I realize the following could be achieve in less lines
'using the NOT operator, but I like this sort of thing as
'I find it easier to figure out WTF I was doing when
'I look at the code later

'Assume menu control is number 3 (arbitrary - Your
'mileage may vary)

if commandbars("mnuCustomMenuName").controls(3).State = true then

'Control is currently ticked/depressed (state property), so
'turn it OFF and turn allowedits OFF.

commandbars("mnuCustomMenuName").controls(3).State = False

f.Allowedits = false

else

'Control is currently not ticked/depressed (state property), so
'turn it ON and turn allowedits ON.

commandbars("mnuCustomMenuName").controls(3).State = True

f.Allowedits = True

end if

Exit_Proc:

Exit function

Err_Proc:

'This is pretty fool proof, so I don't include any error handling

End Function
 
J

John

Thanks Guys. I have found the following code to work fine....so far.

Public Function MainEdit()
Form_Main.Dirty = False
If Form_Main.AllowEdits = True Then
Form_Main.AllowEdits = False
Else
Form_Main.AllowEdits = True
End If

If Form_Main.AllowEdits = True Then
With Application.CommandBars("MyToolbar").Controls!Edit
.State = True
End With
Else
With Application.CommandBars("MyToolbar").Controls!Edit
.State = False
End With
End If
End Function

Marshall Barton said:
Al said:
I tested my Name toggling, and am having a problem figuring out how to
programatically address the Name property of a button on the menu bar.
I'll
try to figure it out and get back to you.
In the meantime, an small unbound text control on your form with...
= IIF(AllowEdits = True, "Enabled", "Disabled)
would give the user a good "visual" toggle value.
I always have a similar field on my forms that says "Filtered" or "All
Records"
Give me some time to "finger" this one out...


Al, It's the Caption property, not the Name property

To set the up/down appearance, use:

With Application.CommandBars("MyBar").Controls!MyPrint]
.State =True 'Down
End With
 
A

Al Camp

Thanks Marshall,
I had to go out most of the day today, so I didn't have a chance to spend
any time with this problem.
However, I was trying and failing with...
CommandBars.ActionControl.Caption = "Enable"
(without calling Application.) so your code was very helpful.

Same with the button up/down "appearance" code!

Thanks a lot,
Al Camp
Candia Computer Consulting - Candia NH
http://home.comcast.net/~cccsolutions


Marshall Barton said:
Al said:
I tested my Name toggling, and am having a problem figuring out how to
programatically address the Name property of a button on the menu bar.
I'll
try to figure it out and get back to you.
In the meantime, an small unbound text control on your form with...
= IIF(AllowEdits = True, "Enabled", "Disabled)
would give the user a good "visual" toggle value.
I always have a similar field on my forms that says "Filtered" or "All
Records"
Give me some time to "finger" this one out...


Al, It's the Caption property, not the Name property

To set the up/down appearance, use:

With Application.CommandBars("MyBar").Controls!MyPrint]
.State =True 'Down
End With
 
M

Marshall Barton

Glad you got it working, but, to be precise, you should
**not** be using the Form_Main type of reference. It may
work ok for you now, but since it refers to the "default"
instance of the form, you could be referring to the wrong
instance when/if you ever have the form open more than once.

The safe reference is Forms!Main for the form you opened
using DoCmd.OpenForm and a frmobject variable for any
additional instances.

On another note, you can make the second part of the code
sequence shorter by using:

With Application.CommandBars("MyToolbar").Controls!Edit
.State = Forms!Main.AllowEdits
End With
--
Marsh
MVP [MS Access]

Thanks Guys. I have found the following code to work fine....so far.

Public Function MainEdit()
Form_Main.Dirty = False
If Form_Main.AllowEdits = True Then
Form_Main.AllowEdits = False
Else
Form_Main.AllowEdits = True
End If

If Form_Main.AllowEdits = True Then
With Application.CommandBars("MyToolbar").Controls!Edit
.State = True
End With
Else
With Application.CommandBars("MyToolbar").Controls!Edit
.State = False
End With
End If
End Function
Al said:
I tested my Name toggling, and am having a problem figuring out how to
programatically address the Name property of a button on the menu bar.
I'll
try to figure it out and get back to you.
In the meantime, an small unbound text control on your form with...
= IIF(AllowEdits = True, "Enabled", "Disabled)
would give the user a good "visual" toggle value.
I always have a similar field on my forms that says "Filtered" or "All
Records"
Give me some time to "finger" this one out...

"Marshall Barton" wrote
Al, It's the Caption property, not the Name property

To set the up/down appearance, use:

With Application.CommandBars("MyBar").Controls!MyPrint]
.State =True 'Down
End With
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top