Toggle Buttons with Text

G

Guest

I have hunted around and figured out how to have a toggle button display a
text message on it when it is initially clicked on.

ie. The button is blank uppon opening a form
The button says "YES" on it when it is clicked once,
The button says "NO" on it when clicked again.

What I would like to figure out is how to have that text appear without
having to click the button.
For example, if a form was opened and a button clicked to say YES and then
the form was re-opened, I would like to button to aknowledge that it has
already been set, and have it Display its "clicked" message = "YES"

How would I go about doing this?
 
M

MtnWindow

If I understand you correctly, you want to save the caption of a button
as long as the database remains open. If that is the case, you can use
a Public variable in a Module.

For example:
Public GlobalVariable_btn1Status As Byte

Then on your form, test for the status of this variable in the On Load
event, like this:
Private Sub Form_Load()
Select Case GlobalVariable_btn1Status
Case 0
btn1.Caption = ""
Case 1
btn1.Caption = "Yes"
Case 2
btn1.Caption = "No"
End Select
End Sub

Similarly in the On Click event check the status and then change it,
like this:
Private Sub btn1_Click()
Select Case GlobalVariable_btn1Status
Case 0
btn1.Caption = "Yes"
GlobalVariable_btn1Status = 1
Case 1
btn1.Caption = "No"
GlobalVariable_btn1Status = 2
Case 2
btn1.Caption = "Yes"
GlobalVariable_btn1Status = 1
End Select
End Sub


The value of the Public variable will not be saved when the database
closes. If you want to retain the value, you could use a field in the
first record of a table instead of a Public variable.
 
G

Guest

It's easiest to use a global variable or set( ) and get( ) functions in a
standard module, but if your organization has an aversion to globals (as many
do), then you could use static variables.

In order to have the toggle button blank the first time the form is opened
and "remember" its previous state the next time the form is opened, even if
the state never changed, you'll need to add some code to the form's OnOpen( )
event, OnClose( ) event, a procedure in the form, and two public functions in
a standard module. In the standard module, try:

Public Function isNotFirstTime(fNotChanged As Boolean) As Boolean
Static fNotFirstTime As Boolean

If (Not (fNotFirstTime)) Then
isNotFirstTime = False
fNotFirstTime = True ' Set for next time.
Else
isNotFirstTime = True
End If

If (fNotChanged) Then ' Reset for next form opening if
fNotFirstTime = False ' caption wasn't changed.
End If
End Function

Public Function getBtnState(fUsePrev As Boolean) As Boolean
Static fState As Boolean

If (Not (fUsePrev)) Then
fState = Not (fState)
End If
getBtnState = fState
End Function

In the form's module, try:

Private Sub Form_Open(Cancel As Integer)
If (isNotFirstTime(False)) Then
Call toggle(True)
End If
End Sub

Private Sub ToggleBtn_Click()
Call toggle(False)
End Sub

Private Sub toggle(fUsePrev As Boolean)
If (getBtnState(fUsePrev)) Then
Me!ToggleBtn.Caption = "Yes"
Else
Me!ToggleBtn.Caption = "No"
End If
End Sub ' toggle( )

Private Sub Form_Close()
If (Len(Me!ToggleBtn.Caption) = 0) Then
Call isNotFirstTime(True)
End If
End Sub

.. . . where ToggleBtn is the name of the button.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 

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

Top