Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert statment inside for loop in VBA

Status
Not open for further replies.

avenuw

Programmer
Jun 21, 2007
45
US
Hello,

I have spent the whole day trying to figure this out and I am not having any luck! Any help you provide will be GREATLY APPRECIATED. Ok the problem is quite simple, I am trying to insert 340 records automatically, so here is my code:

Public Sub insert_row_sub()

DoCmd.SetWarnings False

Dim num As Long
Dim sql As String

sql = "INSERT INTO new_tbl(code,s_id,fee_class) VALUES " & "(" & num & ",688,'Expensive');"

For num = 92259 To 92599
num = num + 1
DoCmd.RunSQL (sql)
'Print to immediate window
Debug.Print sql
'Halt execution for testing purposes
Stop
DoCmd.RunSQL sql

Next num
DoCmd.SetWarnings True
End Sub

And this is what prints in my immediate window:

INSERT INTO new_tbl(code,s_id,fee_class) VALUES (0,688,'Expensive');

instead of entering records with code column= 92259 To 92599
I get 340 records all with code column=0.

What am I doing wrong?

Thanks,
Judy
 
Red on blue is very difficult to read.

Code:
Public Sub insert_row_sub()

    DoCmd.SetWarnings False
    
    Dim num As Long
    Dim sql As String

For num = 92259 To 92599
sql = "INSERT INTO new_tbl(code,s_id,fee_class) VALUES " & "(" & num & ",688,'Expensive');"
 
DoCmd.RunSQL sql

num = num + 1
        'Print to immediate window
'Debug.Print sql
'Halt execution for  testing purposes
Stop
'DoCmd.RunSQL sql

    Next num     
    DoCmd.SetWarnings True
End Sub
 
In Remou's code don't forget to get rid of this line:
num = num + 1
 
THANK YOU!
THANK YOU!
THANK YOU!
THANK YOU!

Really Appreciate it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top