Link to home
Start Free TrialLog in
Avatar of Megin
Megin

asked on

Access VBA SQL: Error Too few parameters. Expected 1

Hello!

I am using some code syntax that worked for one table, but is not working for another. I think it is because the table that I am trying to insert the data into has a primary key that is automatically incremental. I want to inforamtion to be placed at the end of the table with the primary key (stoID) automatically entered with the next number in the order. Does that make sense?

Anyway, here is the code I have. It is taking information put into a form and inserting it into a table.

Private Sub Command47_Click()
Dim strSQL As String
strSQL = "INSERT INTO SubTaskOrders(TOID, StoNo, [StoName]) VALUES (" & Me.cmbTaskOrder & ", " & Me.SubNO & ", " & Me.SubName & ")"
CurrentDb.Execute strSQL, dbFailOnError

End Sub

Open in new window


Thank you, in advance, for you help!
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

check the name of the table and the names of the fields, make sure they match the names of the fields in table "SubTaskOrders" .

if you have included the AutoNumber field in your insert sql, remove it
Avatar of Megin
Megin

ASKER

I have included an AutoNumber field, but that is the primary key for the table and is connected to other forms and tables. I don't want/can't to remove it. Is there any other way to handle this error?

However, is there some way to get that number to automatically add the next number other than using AutoNumber?
Also, if those are Text fields you need to enclose them in single quotes:

strSQL = "INSERT INTO SubTaskOrders(TOID, StoNo, [StoName]) VALUES (" & Me.cmbTaskOrder & ", '" & Me.SubNO & "', '" & Me.SubName & "')"

This assumes that StoNo and StoName are TEXT fields, and that TOID is a Numeric field.

What rey means is that if TOID is an AutoNumber field, you should not write to that but instead let Access handle that value.
Avatar of Megin

ASKER

My whole form just starting thowing errors, so I am creating it over again. It is going to take me a little bit to get back to this. I might not be able to try it until tomorrow.

So, don't worry. I will answer this.
Avatar of Megin

ASKER

I am getting a syntax error for the Insert statement now.

Here is my code:

Private Sub btnAddRecord_Click()
Dim strSql As String
strSql = ("INSERT INTO SubTaskOrders(" & Me.cmbTaskOrder & ", " & Me.txtStoNo & ", " & Me.txtStoName & ")")
CurrentDb.Execute strSql, dbFailOnError

End Sub

Open in new window


Some of the names have changed because I am using a new form now, but I want it to do the same thing.
SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Megin

ASKER

OMG! I am so embarrassed!  I can't believe I left that out of the code! I know better....I think I was just frustrated at the end of the day.

The code is working now.

Thank you for your help and your patience!