Good day.!

I want to check all the textbox and combo box if its null or not. Once they click on the button, if one txtbox or combo box is null it will give a message that incomplete entry. I have already posted this thread, but i noticed that when the txtbox or combo box is on the tabcontrol it will not check if its null or not. Here is my previous code.! Any suggestion or just revision of my code in order for this work.?My code below:

Sub clarifydata()
Dim mycontrol As Control
For Each mycontrol In newstudentry.Controls
  If TypeOf mycontrol Is TextBox Or TypeOf mycontrol Is ComboBox Then
    If Trim(mycontrol) = "" Then
      toclear = toclear + "1"
      'C.SetFocus
    Else
      toclear = toclear + "0"
    End If
    
   End If
Next

    If toclear Like "1" Then
        clarifyclear = True
    Else
        clarifyclear = False
    End If
End Sub

God bless on this mother earth.

Hi Blocker

This works for me...

I have made few changes in the code to show it as an example. Please make relevant amendments for your original code...

Private Sub Command1_Click()
    Dim mycontrol As Control
    '~~> What is "newstudentry" in your code???
    '~~> This works for me...
    '~~>Me.Controls will ensure all controls in that form
    '~~> are covered...
    For Each mycontrol In Me.Controls
        If TypeOf mycontrol Is TextBox Or _
        TypeOf mycontrol Is ComboBox Then
            '~~> combination of len and trim
            If Len(Trim(mycontrol)) = 0 Then
                MsgBox "empty"
            Else
                MsgBox "Not empty"
            End If
        End If
    Next
End Sub

Hi Blocker

This works for me...

I have made few changes in the code to show it as an example. Please make relevant amendments for your original code...

Private Sub Command1_Click()
    Dim mycontrol As Control
    '~~> What is "newstudentry" in your code???
    '~~> This works for me...
    '~~>Me.Controls will ensure all controls in that form
    '~~> are covered...
    For Each mycontrol In Me.Controls
        If TypeOf mycontrol Is TextBox Or _
        TypeOf mycontrol Is ComboBox Then
            '~~> combination of len and trim
            If Len(Trim(mycontrol)) = 0 Then
                MsgBox "empty"
            Else
                MsgBox "Not empty"
            End If
        End If
    Next
End Sub

This is my code when the btn save is click. But still it will not read any emtpy txtbox or combobox. The textbox and combox are inside a frame which is inside also in a tab control.below is the code. Im so stuck whats wring with this.

Dim mycontrol As Control

For Each mycontrol In Me.Controls
  If TypeOf mycontrol Is TextBox Or TypeOf mycontrol Is ComboBox Then
    If Len(Trim(mycontrol)) = 0 Then
      toclear = toclear + "1"
      'C.SetFocus
    Else
      toclear = toclear + "0"
    End If
    
   End If
Next

    If toclear Like "1" Then
        clarifyclear = True
    Else
        clarifyclear = False
    End If
    
If clarifyclear = False Then

God bless on this mother earth.

If toclear Like "1" Then

what if there is more than one control without a caption... you should...

if toclear>0 then

Good Luck

The problem starts here

toclear = toclear + "1"

and then continues to

toclear = toclear + "0"

and finally to

If toclear Like "1" Then

Do this...

Dim mycontrol As Control, toclear As Long
    
    toclear = 0
    
    For Each mycontrol In Me.Controls
        If TypeOf mycontrol Is TextBox Or TypeOf mycontrol Is ComboBox Then
            If Len(Trim(mycontrol)) = 0 Then toclear = toclear + 1
        End If
    Next

    If toclear > 0 Then
        clarifyclear = True
    Else
        clarifyclear = False
    End If
    
    If clarifyclear = False Then
    
    '~~> Your rest of the code....

The problem starts here

toclear = toclear + "1"

and then continues to

toclear = toclear + "0"

and finally to

If toclear Like "1" Then

Do this...

Dim mycontrol As Control, toclear As Long

toclear = 0

For Each mycontrol In Me.Controls
    If TypeOf mycontrol Is TextBox Or TypeOf mycontrol Is ComboBox Then
        If Len(Trim(mycontrol)) = 0 Then toclear = toclear + 1
    End If
Next

If toclear > 0 Then
    clarifyclear = True
Else
    clarifyclear = False
End If

If clarifyclear = False Then

'~~> Your rest of the code....

end quote.

Thank you.But even if there are some textboxes and comboxes empty, It will just read this line

If TypeOf mycontrol Is TextBox Or TypeOf mycontrol Is ComboBox Then

and will jump to end if. then continue the loop and still escape to pass this next code

If Len(Trim(mycontrol)) = 0 Then
      toclear = toclear + 1
    End If

I dont know whats is hapeening with this code.When i degug it mycontrol returns to zero(0).

Pls help

Give me a moment...

Edit:

Ok I have checked the code again, it works fine... Can I see your project?

Give me a moment...

Edit:

Ok I have checked the code again, it works fine... Can I see your project?

Thank you very much for this moment of time you have helped me. Ive done all debugging rules but it jump to second if statement again. After all, its just a logical error i think. Ive already corrected the code and it works for now. Maybe on the last if statement goes the error.heres the code:

toclear = 0

For Each frmcontrol In Me.Controls
  If TypeOf frmcontrol Is TextBox Or TypeOf frmcontrol Is ComboBox Then
    If Len(Trim(frmcontrol)) = 0 Then
      toclear = toclear + 1
    End If
  End If
Next

    If toclear > 0 Then
        clarifyclear = True
    Else
        clarifyclear = False
    End If
    
If clarifyclear = False And btnsave.Caption <> "Update" Then

Thank you very much for your time..God bless. see you there on next thread. This problem was solved.

Glad to have helped :)

Do remember to Mark your Thread "Solved", if the query is solved... ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.