Problem with SQL code in VB

G

Guest

In the VB code for a Command Button I want to execute two SQL commands on the
database. The first one is an insert and runs just fine. However, the
second one is an update and is not working. Do I need to declare another
variable for the second SQL command? See below:

Dim loDB As DAO.Database
Set loDB = CurrentDb

Dim lsSQL As String
lsSQL = "INSERT INTO 5_Def_Item ([ITEM_ID],[Eval_Frmwk_Chart]) " & _
"VALUES('" & Me![InvisibleID1] & "',2)"

Dim loQD As DAO.QueryDef
Set loQD = loDB.CreateQueryDef("", lsSQL)

Call loQD.Execute

lsSQL = "UPDATE [1_All Data] " & _
"INNER JOIN 5_Def_Item ON [1_All Data].ID = [5_Def_Item].Item_ID " & _
"SET [1_All Data].[Def Item ID] = [5_Def_Item].Def_Item_ID, " & _
"[1_All Data].[Def Item?] = True " & _
"WHERE [5_Def_Item].Item_ID = ('" & Me![InvisibleID1] & "')"

Call loQD.Execute

Is the lsSQL variable not getting updated with the second SQL command?

Thanks!
 
G

Guest

You assigned loQD for the insert statement but you forgot to do the same with
update statement.

So the loQD still refers to insert instead of Update
Add
Set loQD = loDB.CreateQueryDef("", lsSQL) again before you execute update

Madhuri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top