0

Hi all How can I make a statement if label have text And TextBox have no text in a loop? I can't find anything about it I know with a textbox I can use the .TextLength but that don't work as .CaptionLength Hope you can help

For k = 2 To 41
   If Me.Controls("Label" & k).CaptionLength < 0 And Me.Controls("TextBox" & 
   k).TextLength = 0 Then
   MsgBox "You have a name without Hdc"

   Exit Sub
 End If

UPDATED With working Code

Dim k As Long
For k = 1 To 40
    If Len(Me.Controls("Label" & k).Caption) > 0 And Me.Controls("TextBox" & 
k).Text = vbNullString Then
    MsgBox "You have a name without Hdc"
    Exit Sub
    End If
Next

1 Answer 1

1

Try

If len(Me.Controls("Label" & k).caption) > 0 And ...

The len function returns the length of a string and the caption-property gives the text (caption) of a label

Sign up to request clarification or add additional context in comments.

4 Comments

Hmm I think its on the right track but it is bring up the msgbox even though there is no empty TextBox is it because I use the textbox wrong I tried using = and <> for the textbox also without closing with () If Len(Me.Controls("Label" & k).Caption) > 0 And (Me.Controls("TextBox" & k).TextLength <> 0) Then
let me try with using len on textbox too
For the textbox, you can use the property text (not caption), but TextLength should work also. But if you want to check if the textbox is empty, you have to check for TextLength = 0
Now its working EPIC thanks so much for your help much appreciated !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.