Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning value and status of toggle buttons, Any better idea? 1

Status
Not open for further replies.

GKIL67

Technical User
Dec 1, 2009
44
Hello all, I'm trying to simulate a numeric pad where values
range from 1 to anything, no gaps - via an ms access form.
My issue is how to avoid repeating the subs for each
toggle button, as for i.e. 100 buttons I have to add
and modify 100 subs.

Also, how to empty the string from the respective value
(see HELP! remark below).

Any good ideas? Kindly check my code below.
Thank you!

================================================
Code:
Option Compare Database

Private Sub ToggleButton01_Updated(Code As Integer)
My_Result = GetToggle(ToggleButton01)
End Sub

Function GetToggle(AllToggleFields As Control)
'Used by all toggle buttons

Dim strActiveCtl As String
Dim ToggleVal As Integer

Me!SumValue = 0

strActiveCtl = Screen.ActiveControl.Name

If AllToggleFields.Value = False Then
            AllToggleFields.BackColor = -2147483633  'gray
            AllToggleFields.BorderColor = 0
            Me!SelectedValues = Me!SelectedValues & "," & Right$(strActiveCtl, 2) 'HELP!
            Me!SumValue = Me!SumValue - Val(Right$(strActiveCtl, 2))
                If Me!SumValue < 0 Then Me!SumValue = 0
Else
            AllToggleFields.BackColor = 255 'vbRed
            AllToggleFields.BorderColor = 255
            
            Me!SelectedValues = Me!SelectedValues & "," & Right$(strActiveCtl, 2)
            Me!SumValue = Me!SumValue + Val(Right$(strActiveCtl, 2))
End If

Set AllToggleFields = Nothing
End Function
 
I do not understand what you are saying.
"where values range from 1 to anything"
How do values range to anything? Why do you need 100 subs? I would think you can do it with just a single function. Are you using Access toggle buttons? AFAIK their is no bordercolor or backcolor on a toggle button. Here is my guess of what this thing looks like and does.

1) Build your 100 toggle buttons
2) For each toggle button put a value in the Tag property
3) Build a function like this
Code:
Public Function GetToggleValue()
  Dim actCtl As Access.ToggleButton
  Dim ctl As Access.Control
  Dim sumValue As Integer
  Set actCtl = Screen.ActiveControl

  For Each ctl In Me.Controls
    If ctl.ControlType = acToggleButton Then
      If ctl = True Then
        ctl.ForeColor = 0
        ctl.FontBold = True
        sumValue = sumValue + Val(ctl.Tag)
      Else
        ctl.ForeColor = vbRed
        ctl.FontBold = False
      End If
    End If
  Next ctl
  Me.txtSumValue = sumValue
End Function
4) Select all toggles. Then Make the control source of the toggle = GetToggleValue()

Here is a demo with 25 toggles. Took less than 10 minutes to build. So if I select toggle 6, 10, 24 I get a value of 40. Is that what you are trying to do.
 
You understood all right - it works.

However, adding and removing the tag value(s) from a string,
values separated by commas, is not resolved.
Maybe you could shed some light??

Regards and thank you!

P.S. It is used to simulate the pickings of
a lottery slip.
 
Public Function GetToggleValue()
Dim actCtl As Access.ToggleButton
Dim ctl As Access.Control
Dim sumValue As Integer
Dim strSelected As String
Set actCtl = Screen.ActiveControl

For Each ctl In Me.Controls
If ctl.ControlType = acToggleButton Then
If ctl = True Then
ctl.ForeColor = 0
ctl.FontBold = True
If strSelected = "" Then
strSelected = ctl.Tag
Else
strSelected = strSelected & "; " & ctl.Tag
End If
sumValue = sumValue + Val(ctl.Tag)
Else
ctl.ForeColor = vbRed
ctl.FontBold = False
End If
End If
Next ctl
Me.txtSumValue = sumValue
strSelected = Trim(strSelected)
If Right(strSelected, 1) = ";" Then
strSelected = Left(strSelected, Len(strSelected) - 1)
End If
Me.txtSelectedValues = strSelected
End Function
 

To make this easy to do. Put 100 toggles on a form then through code, name the control, set the caption, and set the tag. Makes it extremely fast to build the form with 100 controls.

Public Sub formatToggle()
Dim frm As Access.Form
Dim ctl As Access.Control
Dim counter As Integer
DoCmd.OpenForm "frmToggleDemo", acDesign
Set frm = Forms("frmToggleDemo")
For Each ctl In frm.Controls
If ctl.ControlType = acToggleButton Then
counter = counter + 1
ctl.Name = "tgl" & counter
ctl.Tag = CStr(counter)
ctl.Caption = CStr(counter)
End If
Next ctl
DoCmd.Close acForm, frm.Name, acSaveYes
End Sub
 
Job is done, fast and efficiently. Awesome work!
I believe at the same time you've covered
issues like mass object/control creation and manipulation, Form looping thru controls, works of toggle buttons etc.

My first inquiry has been a pleasure.

Thank you! MajP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top