Avatar of Ernest Grogg
Ernest Grogg

asked on 

Capturing data during loop and performing "if" statement

I got some help to get this code below to help loop through a "from" and "to" to enter all into the table... so now I want to capture the data as it loops through to check to see if this is already entered into the table

Dim strSQL As String
Dim Result As String


For iI = 0 To iTo - iFrom
         strSQL = "Insert into Table(Field1,Field2,Field3, Field4, Field5) values(""" & Me.iFrom + iI & """,""" & _
            Me.Field2 & """,""" & Me.Field3 & """,""" & Me.Field4 & """,""" & Me.Field 5 & """ )"
            CurrentDb.Execute strSQL, dbFailOnError

Next

Open in new window


I entered this below but if I put it in between, I can capture the first loop only...how can I capture and loop through all and show the MSGBox showing each one that is already in use?  Using the 1 msgbox?

Dim strSQL As String
Dim Result As String

For iI = 0 To iTo - iFrom
Result = Me.iFrom + iI
    If Result = DLookup("Field1", "Table", "Field1=" & Result) Then
        'Debug.Print Result
        MsgBox "this is already in use" & " " & Result
        Cancel = True
        Exit Sub
    Else

         strSQL = "Insert into Table(Field1,Field2,Field3, Field4, Field5) values(""" & Me.iFrom + iI & """,""" & _
            Me.Field2 & """,""" & Me.Field3 & """,""" & Me.Field4 & """,""" & Me.Field5 & """ )"
            CurrentDb.Execute strSQL, dbFailOnError
       
    End If
Next

Open in new window

Microsoft AccessVisual Basic ClassicVB ScriptVBA

Avatar of undefined
Last Comment
Abhigyan Srivastava

8/22/2022 - Mon