Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Access VBA SQL: Error Too few parameters. Expected 1

Posted on 2014-01-16
8
Medium Priority
?
3,471 Views
Last Modified: 2014-01-17
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!
0
Comment
Question by:Megin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39787108
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
0
 

Author Comment

by:Megin
ID: 39787191
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?
0
 
LVL 85
ID: 39787196
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.
0
Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

 

Author Comment

by:Megin
ID: 39787232
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.
0
 

Author Comment

by:Megin
ID: 39787290
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.
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 1000 total points
ID: 39787311
you have to include the NAMES of the FIELDS in your Insert query


Dim strSql As String
strSql = "INSERT INTO SubTaskOrders(NameOffield, StoNo, stoName)  Values (" & Me.cmbTaskOrder & ", " & Me.txtStoNo & ", '" & Me.txtStoName & "')"
CurrentDb.Execute strSql, dbFailOnError


change "NameOffield" with the actual name of field for the TaskOrder
0
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 1000 total points
ID: 39788100
And (again) if those fields are Text, you must enclose them in in single or double quotes:

strSql = "INSERT INTO SubTaskOrders(NameOffield, StoNo, stoName)  Values (" & Me.cmbTaskOrder & ", '  " & Me.txtStoNo & " '  , '   '" & Me.txtStoName & "  ' )"

I've added some whitespace so you can see what I'm talking about.

In your code, there is also an extra ) at the end ... cap fixed that for you.
0
 

Author Closing Comment

by:Megin
ID: 39788874
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!
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question