Run-time Error 438 When Data Entered in all TextBoxes

poopiebear

New Member
Joined
Nov 19, 2010
Messages
41
I keep getting Run-time error 438 and have no idea why. The premise of this code is to check to see if there are any empty textboxes before a new userform appears with the answer. It works when there is at least one textbox that doesn't have data it to display a message box, but when all the textboxes have data in there, the error pops up. What am I missing? :confused:

This coding is in UserForm1 and if all the fields are completed, I want UserForm2 to open.

Code:
'Validates if data is entered in all fields
Private Sub CommandButton1_Click()
    Dim cCont As Control
       For Each cCont In Me.Controls
        If TypeName(cCont) = "TextBox" And cCont.Value = "" And cCont.Visible <> False Then
         MsgBox "Please Complete All Fields"
         OK = True
         GoTo L1
         Exit For
         End If
        Next cCont
 
'Displays TCB amount in new window
    UserForm2.Show
L1:
End Sub

This is the line that gets highlighted when the error appears:

Code:
If TypeName(cCont) = "TextBox" And cCont.Value = "" And cCont.Visible <> False Then
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Move the check for cCont.Value and cCont.Visible after you've checked the control is a textbox.

Currently VBA will try and evaluate those 2 properties for every control, regardless if it's a textbox or not.

Some controls don't have a Value property, not 100% sure about Visible, so that's why you get the 438 Object doesn't support this property... error.

Something like this perhaps

Code:
Option Explicit
 
'Validates if data is entered in all fields
Private Sub CommandButton1_Click()
Dim cCont As Control
 
    For Each cCont In Me.Controls
       
        'check if control is a textbox
        If TypeName(cCont) = "TextBox" Then

           ' now check the 2 properties
            If cCont.Value = "" And cCont.Visible <> False Then
                MsgBox "Please Complete All Fields"
                OK = True
                GoTo L1
            End If
                        
        End If
                
    Next cCont
 
    'Displays TCB amount in new window
    UserForm2.Show
L1:
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,607
Messages
6,179,871
Members
452,949
Latest member
Dupuhini

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