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

Execute Append Query using VBA? 2

Status
Not open for further replies.

sap2

Technical User
Dec 20, 2007
12
US
Scenario:
Main Form: fMain
Sub Form: fsub1 and fsub2
2 tables: tbl1 and tbl2
Query: qryAppendtbl2

After 2 fields are chosen on fMain, fsub1 will display an ID that is indexed into tbl1. fsub2 needs that index number from fsub1 so the records can be synched in reports. NO record exists on tbl2 I am trying to create the record using the forms and an append query.

I can create an append query (qryAppendtbl2) to get the ID from fsub1 and append tbl2, however I cannot get the code correct using VBA. I am using the following code and I get an error that states "Too few parameters. Expected 1" and the compiler highlights the last line:


Private Sub SQE_AfterUpdate()
Dim strSql As String
strSql = "INSERT INTO tbl2 ( InfoID, PO_NUM, PO_ITM ) SELECT tbl1.InfoID, tbl1.PO_NUM, tbl1.PO_ITM From tbl1 GROUP BY tbl1.InfoID, tbl1.PO_NUM, tbl1.PO_ITM HAVING (((tbl1.InfoID)=[Forms]![fMain]![fsub1]![ID]));"
DBEngine(0)(0).Execute strSql, dbFailOnError
End Sub

Can anybody help me? I know only enough VBA to be dangerous. Thanks.
 
try
Code:
strSql = "INSERT INTO tbl2 ( InfoID, PO_NUM, PO_ITM ) SELECT tbl1.InfoID, tbl1.PO_NUM, tbl1.PO_ITM From tbl1 
where tbl1.InfoID=" & [Forms]![fMain]![fsub1]![ID] & "
GROUP BY tbl1.InfoID, tbl1.PO_NUM, tbl1.PO_ITM [COLOR=red][/color]
 
How are ya sap2 . . .

Perhaps this will do:
Code:
[blue]   Dim Sql As String
   Sql = "INSERT INTO tbl2 ( InfoID, " & _
                            "PO_NUM, " & _
                            "PO_ITM ) " & _
         "SELECT tbl1.InfoID, " & _
                "tbl1.PO_NUM, " & _
                "tbl1.PO_ITM " & _
         "From tbl1 " & _
         "GROUP BY tbl1.InfoID, " & _
                  "tbl1.PO_NUM, " & _
                  "tbl1.PO_ITM " & _
         "HAVING (tbl1.InfoID=" & Forms!fMain!fsub1[purple][b].Form[/b][/purple]!ID & ");"
   DBEngine(0)(0).Execute strSql, dbFailOnError[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
 
Thanks both were helpful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top