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

Run-Time error 3061: Too few parameters. Expected 1

Status
Not open for further replies.

rogerarce

Technical User
Jan 24, 2002
57
CR

Can someone help me and tell me what is wrong with this code?:


dbs.Execute " INSERT INTO Pagar ( Factura, Bodega, item, Cantidad, Monto )" _
& " SELECT Facturas.FacturaID, Factura2.Bodega, Factura2.item, Sum(Factura2.Cantidad) AS SumOfCantidad, Sum(Factura2.Monto) AS SumOfMonto " _
& " FROM Facturas INNER JOIN Factura2 ON Facturas.FacturaID = Factura2.Factura " _
& " GROUP BY Facturas.FacturaID, Factura2.Bodega, Factura2.item " _
& " HAVING (((Facturas.FacturaID) = Forms.Facturas.FacturaID ));"


Thanks in advance!
 
You must keep the reference to the form outside the quotes when running SQL in VBA:

Code:
dbs.Execute " INSERT INTO Pagar ( Factura, Bodega, item, Cantidad, Monto )" _
& " SELECT Facturas.FacturaID, Factura2.Bodega, Factura2.item, Sum(Factura2.Cantidad) AS SumOfCantidad, Sum(Factura2.Monto) AS SumOfMonto " _
& " FROM Facturas INNER JOIN Factura2 ON Facturas.FacturaID = Factura2.Factura " _
& " GROUP BY Facturas.FacturaID, Factura2.Bodega, Factura2.item " _
& " HAVING Facturas.FacturaID = " & _
Forms.Facturas.FacturaID, dbFailOnError

It can be useful to create the sql string (strSQL= "Select ...") and the execute, it is easier to debug. dbFailOnError is essential if you do not want SQL to fail silently.

 
I like to build a sql variable then run it as Remou suggests. It helps me debug later if needed.

Also the query will be a little more efficient if you move the criteria from the HAVING clause to the WHERE clause. The SQL assumes the FacturaID field is numeric.

Code:
Dim strSQL as String
strSQL = " INSERT INTO Pagar ( Factura, Bodega, item, Cantidad, Monto )" _
  & " SELECT Facturas.FacturaID, Factura2.Bodega, Factura2.item, Sum(Factura2.Cantidad), Sum(Factura2.Monto) " _
  & " FROM Facturas INNER JOIN Factura2 ON Facturas.FacturaID = Factura2.Factura " _
  & " WHERE Facturas.FacturaID = " & _
  Forms.Facturas.FacturaID & _
  " GROUP BY Facturas.FacturaID, Factura2.Bodega, Factura2.item "
  
dbs.Execute strSQL, dbFailOnError

Duane
Hook'D on Access
MS Access MVP
 
Hey! dhookom it worked great! Thank you for your help! Is there a web that you can recommend I should read to lean more about this?

Regards!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top