Error in For loop code

Tupacmoche

Registered User.
Local time
Today, 19:02
Joined
Apr 28, 2008
Messages
291
VBA Masters,

I have this simple for loop that gives me an error but, I can't see what's wrong any help is appreciated. Error msg is: Next without for

Code:
For Each ctl In Me.Detail.Controls
    With ctl
        If .ControlsType = acTextBox Then
            If IsNull(.Value) Then
                If .Tag = "v" Then
                   If MsgBox(ctl.Name & " is empty, this is a required field", vbOKCancel) = vbCancel Then
                    Cancel = True: ctl.SetFocus: Exit Sub
                End If
            End If
        End If
    
Next ctl
 
Last edited by a moderator:
Try putting it between code tags with indentation and then it should be easy to see the problem.?
 
I do have it formatted but the formatting is not kept when you attach the code into this editor. Please see attached screen shot. It still eludes me.:o
 

Attachments

  • For_Loop_Code.JPG
    For_Loop_Code.JPG
    30 KB · Views: 49
I added code tags to the original post. Using the "#" icon in the reply area adds code tags which preserves indenting.
 
Oh, and you didn't end the With block.
 
I still get the same error.:o


Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
 
Dim bCheck As Boolean
Dim ctl As Control

For Each ctl In Me.Detail.Controls
    With ctl
        If .ControlsType = acTextBox Then
            If IsNull(.Value) Then
                If .Tag = "v" Then
                   If MsgBox(ctl.Name & " is empty, this is a required field", vbOKCancel) = vbCancel Then
                    Cancel = True: ctl.SetFocus: Exit Sub
                End If
            End If
        End If
Next ctl
    End With
    
End Sub
 
How about?

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
 
Dim bCheck As Boolean
Dim ctl As Control

For Each ctl In Me.Detail.Controls
    With ctl
        If .ControlsType = acTextBox Then
            If IsNull(.Value) Then
                If .Tag = "v" Then
                   If MsgBox(ctl.Name & " is empty, this is a required field", vbOKCancel) = vbCancel Then
                    Cancel = True: ctl.SetFocus: Exit Sub
                End If
            End If
        End If
    End With
Next ctl
    
    
End Sub
 
I do have it formatted but the formatting is not kept when you attach the code into this editor. Please see attached screen shot. It still eludes me.:o

That is why there are code tags?

Are you saying you have made over 200 posts and never used the code tags?
 
Gasman,

As long as I have been on this site, I have never even considered the formatting tools that are on top of the editing form. But, I will use it going forward. I dropped your suggested code in and got the error: End With without With.:(
 
I believe you are still missing an End If.?

Pair them up, I think the one for MSGBOX is missing?
 
Gasman et. al


Thanks, I saw that but I'm so tired that, I did not close it. :D
 
I have added an End If re the msgBox, but I think the property is ControlType not ControlsType.
What is the purpose of bCheck?

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
 
Dim bCheck As Boolean
Dim ctl As Control

For Each ctl In Me.Detail.Controls
    With ctl
        If .ControlsType = acTextBox Then
            If IsNull(.Value) Then
                If .Tag = "v" Then
                   If MsgBox(ctl.Name & " is empty, this is a required field", vbOKCancel) = vbCancel Then
                    Cancel = True: ctl.SetFocus: Exit Sub
                   [COLOR="Blue"]End if 'for the msgbox *********[/COLOR]
                End If
            End If
        End If
    End With
Next ctl
    
    
End Sub
 
So, is it working now?
 
Jdraw,


It may be that you could see that the code was going to crap out as it did. I changed it to the following:


Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
 
Dim bCheck As Boolean
Dim ctl As Control

For Each ctl In Me.Detail.Controls
    With ctl
            If .Tag = "v" Then
                If IsNull(.Value) Then
                    MsgBox ctl.Name & " is empty, this is a required field!", vbOKOnly, "Required Field"
                    ctl.SetFocus
                    Cancel = True
                    Exit Sub
                End If
            End If
    End With
Next ctl
    
End Sub
The code in the thread was showing the message but nothing else. Did you know that?
 
No. I was looking at syntax.
 

Users who are viewing this thread

Back
Top Bottom