troubleshooting Question

Capturing data during loop and performing "if" statement

Avatar of Ernest Grogg
Ernest Grogg asked on
Microsoft AccessVisual Basic ClassicVB ScriptVBA
8 Comments1 Solution130 ViewsLast Modified:
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

ASKER CERTIFIED SOLUTION
Abhigyan Srivastava

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 8 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 8 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004