PDA

View Full Version : Solved: insert into



mpearce
05-08-2009, 01:38 PM
In the sub below i get an error:


Run-time error '3061':

too few parameters, expected 2.

Private Sub RUN_Click()
Dim filename As Variant
Dim daterecd As Variant
Dim strSQL As String

filename = txtFileName.Value
daterecd = txtDateRecd.Value

Set db = CurrentDb()

strSQL = "INSERT into gwexport (filename, daterecd) Values (" & [filename] & ", " & [daterecd] & ")"
Debug.Print strSQL

db.Execute strSQL



End Sub
Table gwexport:
filename is text
daterecd is text

Where am i going wrong?

Let me know if you need more information.

Thanks

orange
05-08-2009, 05:35 PM
Just a guess but you could try
filename = Me.txtFileName.Value
daterecd = Me.txtDateRecd.Value

I think it hasn't resolved these 2 fields from the Form.

mpearce
05-08-2009, 07:16 PM
Just a guess but you could try
filename = Me.txtFileName.Value
daterecd = Me.txtDateRecd.Value

I think it hasn't resolved these 2 fields from the Form.

Still the same result

orange
05-09-2009, 06:33 AM
I have set up a small test of your issue. The problem is with the quotes.
You need quotes to suuround the text values in the INSERT statement.
Adjust as follows -- It works.

strSQL = "INSERT into gwexport (filename, daterecd) Values ( '" & [filename] & "', '" & [daterecd] & "');"

CreganTur
05-11-2009, 05:40 AM
The quotes that orange is referring to are called data type qualifiers. Whenever you want to use VBA variables or object value references in a hard-coded SQL string, you have to wrap them with data qualifiers. For string values, you use single quotes, as illustrated above. For Dates, you use the pound sign (#), and for numbers you do not use any qualifier.

Your code was failing because your lack of data qualifiers told SQL to expect numeric values for the parameters, but you are passing it string values.

HTH:thumb