Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have an error in my vba code but I don't know where.. help me please

VB
Set MyTable2 = MyDB.OpenRecordset("SELECT * FROM [tblRoutingMain] " & _
                                "WHERE ((([tblRoutingMain].[QUOTE NUMBER])='" & [QUOTE NUMBER] & "') AND (([tblRoutingMain].COMPPN)='" & COMPPN & "')) " & _
                                "ORDER BY tblRoutingMain.ROUTING ASC;")
    
  If Not MyTable2.EOF Then MyTable2.MoveFirst
  
 
      strUpdateQuery = "UPDATE tblRoutingMain SET  tblRoutingMain.[SEQ] = '" & MyTable2.Fields("SEQ") & "', " & _
                       "WHERE ((([tblRoutingMain].[QUOTE NUMBER])='" & [QUOTE NUMBER] & "CONT1" & "') AND (([tblRoutingMain].COMPPN)='" & COMPPN & "') AND (tblHardwareCost.[ROUTING]='" & MyTable2.Fields("ROUTING") & "'))" & _
                       "ORDER BY tblRoutingMain.ROUTING ASC;"
      CurrentDb.Execute strUpdateQuery, dbFailOnError
Posted
Updated 26-Mar-13 2:55am
v3

You really should consider using SqlParameters. It's easier to write, easier to read/debug and more secure. Adding variables as a part of a sql-query will make it vulnerable for sql-injections.

But to debug this, the first thing I would do is to see the result of strUpdateQuery variable before it is executed. You will see what's wrong if you see what you are trying to execute.

And why are you doing ordering in an update query?
SQL
"ORDER BY tblRoutingMain.ROUTING ASC"
 
Share this answer
 
v2
Comments
juju5454 26-Mar-13 9:04am    
It is my first SQL code. About strUpdateQuery, it looks good with variable.
I put an order by to do like in MyTable2

How can I use a SqlParameters ?
StianSandberg 26-Mar-13 9:09am    
You can't do an order by with update. Remove your order by clause. And please post the resulting sql-query. What does your sql looks like before it's executed?
juju5454 26-Mar-13 14:31pm    
could you take a look ?
I obtained this code :
<pre lang="sql">Set MyTable2 = MyDB.OpenRecordset("SELECT * FROM [tblRoutingMain] " & _
                               "WHERE ((([tblRoutingMain].[QUOTE NUMBER])='" & [QUOTE NUMBER] & "') AND (([tblRoutingMain].COMPPN)='" & COMPPN & "')) " & _
                               "ORDER BY tblRoutingMain.ROUTING ASC;")

 If Not MyTable2.EOF Then MyTable2.MoveFirst


     strUpdateQuery = "UPDATE tblRoutingMain SET  tblRoutingMain.[SEQ] = '" & MyTable2.Fields("SEQ") & "' " & _
                      "WHERE ((([tblRoutingMain].[QUOTE NUMBER])='" & [QUOTE NUMBER] & "CONT1" & "') AND (([tblRoutingMain].COMPPN)='" & COMPPN & "') AND (tblHardwareCost.[ROUTING]='" & MyTable2.Fields("ROUTING") & "'))"

     CurrentDb.Execute strUpdateQuery, dbFailOnError


I have an other error.. to few parameters. expected 1
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900