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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Insert SQL

Status
Not open for further replies.

webopdoug

Technical User
Jun 27, 2006
5
US
I was using the following syntax in a Private Function when Access complained "Invalid SQL Statement":

Dim strSQL As String
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Set conn = CurrentProject.Connection
Set rs = New ADODB.Recordset
strSQL = "tblCommissionsPaid"
rs.Open strSQL, conn, , adLockOptimistic
rs.AddNew <-- Error at this point
rs!ContractNumber = Me.ContractNumber
rs!RepID = Me.RepID
rs!CommTypeID = 2
rs!CommAmt = Me.PurchAmt * 0.15
rs!CommDate = Me.ContractDate
rs.Update
rs.Close
conn.Close

I tried an alternate Syntax with no luck
strSQL = "INSERT INTO tblCommissionsPaid (ContractNumber, RepID, CommAmt, CommDate, CommTypeID) " & _
"VALUES '" & Me.ContractNumber & "', ' & Me.RepID & ', ' & Me.PurchAmt * 0.15 & '," & _
"' #" & Me.ContractDate & "#', '2'"
DoCmd.RunSQL strSQL

I have used the rs.AddNew before with success (same syntax with a different table). Can you tell me what I'm missing here? Or is there a better way?
 
I was able to get this to work

strSQL = "INSERT INTO tblCommissionsPaid ([ContractNumber], [RepID], [CommAmt], [CommDate], [CommTypeID]) " & _
"VALUES ('" & Me.ContractNumber & "', " & Me.RepID & ", " & Me.PurchAmt * 0.15 & ", " & _
" #" & Me.ContractDate & "#, 2)"

I'm still not sure if this is the best way to do this.
 
Yup, you need brackets around the parameters of the VALUES clause.

And yes, it's better to use an INSERT statement than having the overhead of opening a recordset (especially since you were opening up the entire table - if you do use recordsets to add records, at least make sure your SQL statement doesn't retrieve records you don't need).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top