Populating a textbox with results from multiple option buttons

ryeire

New Member
Joined
Jan 31, 2022
Messages
31
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I'm currently creating a database for doing checks and logging wether a station has passed or failed the check.

I have a userform with a pass/fail option button for each station and I'm looking to populate a textbox in another userform with the station numbers that have failed.
However, I am struggling to find a way to populate one textbox with results from multiple option button groups. I have tried elseif statements and a number of techniques but I have had no luck and my excel vba knowledge is still limited.

1651758419422.png
1651758469043.png


So you select what stations have passed/failed then when you press continue the second userform appears. I would like the failed stations to be populated automatically into the Failed Station Numbers text box. I have it coded so that the date and area automatically populated.

This is an example of the code I was trying:

VBA Code:
Private Sub cmdSTcheck_Click()

DiodeForm.Hide

frmWeeklyForm.txtArea.Value = "Diode"

If Me.opt1fail.Value = True Then
frmWeeklyForm.txtFails.Value = "1"
End If

If Me.opt2fail.Value = True Then
frmWeeklyForm.txtFails.Value = "2"
End If

frmWeeklyForm.Show

End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi,
untested but assuming your naming convention for the "Fail" optionbuttons remains consistent then try following & see if does what you want

VBA Code:
Private Sub cmdSTcheck_Click()
    Dim Fails   As String
    Dim i       As Long
    
    For i = 1 To 42
        With Me.Controls("Opt" & i & "Fail")
            If .Value Then Fails = Fails & i & ","
        End With
    Next i
    
    Fails = Mid(Fails, 1, Len(Fails) - 1)
    frmWeeklyForm.txtFails.Value = Fails
End Sub

Dave
 
Upvote 0
Solution
@dmt32 I now have one small problem with the code. If all of the stations pass and I submit I get an error within the code. Do you know of a way to fix this?

1651823697323.png
1651823741021.png
 
Upvote 0
Try

VBA Code:
Private Sub cmdSTcheck_Click()
    Dim Fails   As String
    Dim i       As Long
    
    For i = 1 To 42
        If Me.Controls("Opt" & i & "Fail").Value Then _
        Fails = Fails & i & ","
    Next i
    
    If Len(Fails) > 0 Then
        Fails = Mid(Fails, 1, Len(Fails) - 1)
        frmWeeklyForm.txtFails.Value = Fails
    End If
End Sub

Note: When posting code, use code tags from then menu bar - Press VBA which will produce the tags & place you code between them

Dave
 
Upvote 0
Dave you really are amazing! Worked perfectly again thank you very much.

Sorry about the code I just wanted to show where the error was - which is why I used a screenshot
 
Upvote 0
most welcome - appreciate the feedback

Dave
 
Upvote 0

Forum statistics

Threads
1,215,756
Messages
6,126,692
Members
449,330
Latest member
ThatGuyCap

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top