Using VBA Controls in a UserForm

business_analyst

Board Regular
Joined
Jun 5, 2009
Messages
99
Ok, so I have the following code that checks the textboxes within a Userform and if there is a blank text box, a message box pops up prompting the user to fill them out. The problem is, this code only seems to work in the userform from a top-down approach. In other words, if you don't fill anything in the first textbox, but enter something in the second, it allows the user to continue. Otherwise, it works fine.

Dim proceed As Integer

Private Sub cmd_1_next_Click()
check Me
If proceed = 0 Then
MsgBox ("Please fill out all textboxes")
Else
frm_00.Hide
frm_01.Show
End If
End Sub

Private Sub check(ByVal frmname As Object)
Dim ctl As Control
For Each ctl In frmname.Controls
If TypeName(ctl) = "TextBox" Then
If ctl.Value = "" Then
proceed = 0
Else
proceed = 1
End If
End If
Next ctl
End Sub

Any help would be greatly appreciated, thanks All.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
You need to exit the loop when you find a blank control:
Rich (BB code):
Private Sub check(ByVal frmname As Object)
Dim ctl As Control
For Each ctl In frmname.Controls
If TypeName(ctl) = "TextBox" Then
If ctl.Value = "" Then
proceed = 0
Exit For
Else
proceed = 1
End If
End If
Next ctl
End Sub
 
Upvote 0
You need to exit the loop when you find a blank control:
Rich (BB code):
Private Sub check(ByVal frmname As Object)
Dim ctl As Control
For Each ctl In frmname.Controls
If TypeName(ctl) = "TextBox" Then
If ctl.Value = "" Then
proceed = 0
Exit For
Else
proceed = 1
End If
End If
Next ctl
End Sub

Thanks rorya! it works perfectly now :laugh:. Much appreciated!!
 
Upvote 0

Forum statistics

Threads
1,223,931
Messages
6,175,465
Members
452,646
Latest member
tudou

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