verifying data entry in text boxes in user forms

VBAmalta89

Board Regular
Joined
Mar 25, 2011
Messages
116
I have a series of check boxes in a frame in a user form. Adjacent to each check box is a text box which must be filled if the the check box is checked.

How can i create a sort of validation function to ensure that once the user click the done button then all required text boxes are filled, ie all checked boxes must have their adjacent text boxes filled? A message prompting the user about the error would ideally be given.

Thanks
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
You could put this in a Standard Module - Reference this procedure under your DONE command button

Sub Foo()
For Each ctrl In UserForm1.Controls
If TypeOf ctrl Is MSForms.TextBox Then
If ctrl.Value = "" Then
MsgBox "All text boxes are not Filled in."
Exit Sub
End If
End If
Next
End Sub
 
Upvote 0
This assumes the CheckBoxes in Frame1 are named CheckBox1, CheckBox2, etc. and their associated TextBoxes are named TextBox1, TextBox2, etc.

Change the 4 in the code to the number of CheckBoxes you have in Frame1

Code:
Private Sub cmbDone_Click()
    
    Dim i As Integer, bInvalid As Boolean

    With Me.Frame1
        For i = 1 To [COLOR="Red"]4[/COLOR]
            If .Controls("CheckBox" & i).Value And Len(.Controls("TextBox" & i)) = 0 Then
                .Controls("TextBox" & i).SetFocus
                bInvalid = True
                Exit For
            End If
        Next i
    End With
    
    If bInvalid Then
        MsgBox "The selected Textbox requires an entry or deselect its checkbox.", vbExclamation, "Missing Textbox Entry"
    Else
[COLOR="Green"]        ' All selected checkboxes have filled textboxes
        ' Your code here[/COLOR]
    End If
    
End Sub
 
Last edited:
Upvote 0
what if the checkl boxes had the following names:

process1_1
process2_1
process3_1

What i mean is that 'i' isnt at the end of the variable name but rather in the middle of the variable.

How does the code change for this?
 
Upvote 0
Thanks this works perfectly when having one frame. If i had to have multiple frames which are enabled depending on a value enetered in a previous text box then how would the coding change to validate only the text boxes in the active frames?

example i have 8 frames and the user has to choose how many of these to activate. how do i change the code to validate only the data in the enabled frames?
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,136
Members
452,890
Latest member
Nikhil Ramesh

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