changing toggle button caption

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like to change the caption on a toggle button to reflect its current
state.

I suspect I will put something like:

Me!myButton.caption = "inny"
and
Me!myButton.caption = "outy"

behind some events, but I don't know which ones.
 
Use the AfterUpdate event of the control to alter its Caption.
Use the Current event of the form to make that happen as each record is
loaded.

This example assumes a toggle button named "Inactive". Because the code is
run for every record, it changes the caption only if it needs to be
changeds. (Reading a property is much less expensive than writing it.)

Private Sub Inactive_AfterUpdate()
'Purpose: Set the Caption of the Inactive toggle button.
Dim strCaption As String

With Me.Inactive
If .Value Then
strCaption = "Closed"
Else
strCaption = "Open"
End If
If .Caption <> strCaption Then
.Caption = strCaption
End If
End With
End Sub

Private Sub Form_Current()
Call Inactive_AfterUpdate
End Sub
 
Thank you very much for your reply Allen. I have not seen the "With"
construct before; I presume that it allows you to not fully qualify every
object reference(?).

I would like to ask how to access the properties associated with the form
itself. For example, I would like to put a toggle button on the form that
will toggle the "Allow Edits" property of the form.
 
Yes, you have understood With correctly. Not essential; just handy.

In form design view, you can see the properties of the form in the
Properties box when its title says Form. AllowEdits is on the Data tab.

In the event procedure, you just write:
Me.AllowEdits = True
 
Thanks again Allen. I think there is a problem with this, however, at least
when using it in the AfterUpdate event. If the form's AllowEdits property is
set to No, the AfterUpdate event will never occur; is that correct?

FYI: I have found that it just too easy to accidently change data in a form
(fat finger syndrome I think it's called<g>), so I wanted to set the
AllowEdits property to No, but have a button to enable edits if that is
really what I want.
 
Okay, if that's what you are after, one of the problems is that setting the
AllowEdits affects the unbound controls as well as the bound ones.

That usually works out to be a problem for me, so I use the function below
with a command button that toggles between Lock and Unlock the form. To lock
the form, call:
Call LockBoundControls(Me, True)
and to unlock it, use False for the last argument.

The code loops through all controls on the form, and identifies those bound
to a field (not unbound, nor bound to an expression, nor without a Control
Source), and it toggles the Locked property of those controls. If it finds a
subform, the function calls itself recursively so that it handles any level
of subforms as well.

If there are some controls that should not be locked, you can pass their
names as optional arguments, e.g.:
Call LockBoundControls(Me, True, "Text1", "Sub1")
If the exception is a subform, then no processing occurs for lower levels of
that subform either.

Re your question about AllowEdits, a Form_AfterUpdate won't occur when there
are no updates, *unless* the form was already dirty at the time when you set
AllowEdits to No. In that case, MS decided it was best to let you finish
editing and save before applying the property--probably a good choice. The
code below therefore explicitly saves before allowing the toggle to
AllowEdits, to avoid that confusion.

As a visual clue for the user, I also place thin-boundary red rectangle
around the edge of the form, and set its Visible property to True when the
form is locked. I've found this helps the user remember why "the stupid
respond won't respond" (i.e. it's currently locked.)

Hope it helps. Watch the line-wraps.

----------------------------code starts------------------
Public Function LockBoundControls(frm As Form, _
bLock As Boolean, ParamArray avarExceptionList())
On Error GoTo Err_Handler
'Purpose: Lock the bound controls and prevent deletes on the form any
its subforms.
'Arguments frm = the form to be locked
' bLock = True to lock, False to unlock.
' avarExceptionList: Names of the controls NOT to lock
(variant array of strings).
'Usage: Call LockBoundControls(Me. True)
Dim ctl As Control 'Each control on the form
Dim lngI As Long 'Loop controller.
Dim bSkip As Boolean

'Save any edits.
If frm.Dirty Then
frm.Dirty = False
End If
'Block deletions.
frm.AllowDeletions = Not bLock

For Each ctl In frm.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acOptionGroup, _
acCheckBox, acOptionButton, acToggleButton
'Lock/unlock these controls if bound to fields.
bSkip = False
For lngI = LBound(avarExceptionList) To
UBound(avarExceptionList)
If avarExceptionList(lngI) = ctl.Name Then
bSkip = True
Exit For
End If
Next
If Not bSkip Then
If HasProperty(ctl, "ControlSource") Then
If Len(ctl.ControlSource) > 0& And Not ctl.ControlSource
Like "=*" Then
If ctl.Locked <> bLock Then
ctl.Locked = bLock
End If
End If
End If
End If

Case acSubform
'Recursive call to handle all subforms.
bSkip = False
For lngI = LBound(avarExceptionList) To
UBound(avarExceptionList)
If avarExceptionList(lngI) = ctl.Name Then
bSkip = True
Exit For
End If
Next
If Not bSkip Then
If Len(Nz(ctl.SourceObject, vbNullString)) > 0& Then
ctl.Form.AllowDeletions = Not bLock
ctl.Form.AllowAdditions = Not bLock
Call LockBoundControls(ctl.Form, bLock)
End If
End If

Case acLabel, acLine, acRectangle, acCommandButton, _
acTabCtl, acPage, acPageBreak, acImage, acObjectFrame
'Do nothing

Case Else
'Includes acBoundObjectFrame, acCustomControl
Debug.Print ctl.Name & " not handled on " & conMod & " at " &
Now()
End Select
Next

Exit_Handler:
Set ctl = Nothing
Exit Function

Err_Handler:
MsgBox "Error " & Err.Number & " - " & Err.Description
Resume Exit_Handler
End Function
----------------------------code starts------------------
 
Back
Top