Susan
Susan

Reputation: 11

Access loop through continuous form

I am attempting to add a VBA to a command button to look at a text box and if not null, then look at a combo box. If that combo box is null, have a pop up message box. Since it is a continuous form and there are several records showing, it works fine on the first record, however it does not for the following records. I believe this is because I need to loop through each record. Below is the VBA code I have so far. I would greatly appreciate any help regarding the loop, as I am new to VBA.

If Not IsNull(Me.Text33.Value) Then
    If IsNull(Me.Combo24.Value) Then
        MsgBox "You must state if the rationale is aligned with procedures for each disputed subcategory."
    Else
     'do nothing
    End If
End If

DoCmd.Save
DoCmd.Close

Thank you in advance,

Susan

Upvotes: 1

Views: 9396

Answers (1)

Kostas K.
Kostas K.

Reputation: 8508

Since this is a continuous form, you can Clone the Form's recordset and loop in each record.

Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
    rs.MoveLast
    rs.MoveFirst

Dim idx As Integer
For idx = 1 To rs.RecordCount
    If Not IsNull(rs![TextBoxFieldName]) Then If IsNull(rs![ComboBoxFieldName]) Then MsgBox "..."
    rs.MoveNext
Next idx

Set rs = Nothing

With DoCmd
    .Save
    .Close
End With

Note in case this is intended for validation purposes, the DoCmd actions will always execute regardless of the message-box error/warning.

You can change that by adding an Exit Sub after showing the message box.

Edit:


Sub Example()

    Dim rs As DAO.Recordset
    Set rs = Me.RecordsetClone
        rs.MoveLast
        rs.MoveFirst

    Dim idx As Integer
    For idx = 1 To rs.RecordCount
        Select Case True
            Case Not IsNull(rs![ComboFieldName]) And IsNull(rs![TextBoxFieldName]):
                MsgBox "You must enter dispute comments for each disputed subcategory."
                GoTo Leave

            Case IsNull(rs![ComboFieldName]) And Not IsNull(rs![TextBoxFieldName]):
                MsgBox "You must state if the rationale is aligned with procedures for each disputed subcategory."
                GoTo Leave

            Case Else:
                'do nothing
        End Select
        rs.MoveNext
    Next idx

    Set rs = Nothing
    With DoCmd
        .Save
        .Close
    End With
    Exit Sub

Leave:
    Set rs = Nothing
End Sub

Upvotes: 4

Related Questions