I'm working on a program in MS Access, and I'm just trying to enter some data into a test table to see how it works. I can't for the life of me see what is wrong with the syntax.
I'm getting a 3134 error code.
Maybe there is a reserved word I'm using?
All of the data are strings (even things that probably should be something else). I changed them to strings to try and figure out what the problem is).
CurrentDb.Execute " INSERT INTO TempReg " _
& "(Timestamp, LName, FName, Grade, InventoryNumber, SerialNumber, MacAddress, PaidIn, CheckNum) VALUES " _
& "('test', 'test2', 'test', 'test', 'test', 'test', 'test', 'test', 'test');"
Just for fun, when I run the following code it works fine. I don't see what is fundamentally different.
CurrentDb.Execute " INSERT INTO TestTable " _
& "(SampleText, MoreText) VALUES " _
& "('test', 'test2');"
Thank you!
TimeStamp is a reserved word, as per https://support.office.com/en-us/article/learn-about-access-reserved-words-and-symbols-ae9d9ada-3255-4b12-91a9-f855bdd9c5a2
You should change that field name, or try putting the name within [brackets] in your statement.
Try to put field names in square brackets:
CurrentDb.Execute "INSERT INTO TempReg " _
& "([Timestamp], [LName], [FName], [Grade], [InventoryNumber], [SerialNumber], [MacAddress], [PaidIn], [CheckNum]) VALUES " _
& "('test', 'test2', 'test', 'test', 'test', 'test', 'test', 'test', 'test');", dbFailOnError
Related
So I'm trying to take data from a table, set that piece of data to a variable, and add that variable into a new table.
This is the code to access the last name of the person I'm searching for. I'm almost 100% sure this part works.
Dim db As Database
Dim Lrs As DAO.Recordset
Dim LSQL As String
Set db = CurrentDb()
Set Lrs = db.OpenRecordset("Select [LastName]" & _
"From ['Chromebook Registration Form]" & _
"Where [InventoryNumber] = 1")
dbLastName = Lrs("LastName")
In debug mode, it shows that the variable "dbLastName" contains the string that I want.
However, when I run the following code (to add the information into a new table), I get a 3061 Run-time error code. Too few parameters: expected 1.
The debugger says the problem is in the last line. I assume it is a problem with "dbLastName". The timestamp thing works fine.
CurrentDb.Execute " INSERT INTO TempReg " _
& "([Timestamp], LName, FName, Grade, InventoryNumber, SerialNumber, MacAddress, PaidIn, CheckNum) VALUES " _
& "(Now, dbLastName, 'test', 'test', 'test', 'test', 'test', 'test', 'test');"
Thank you!
You can't just embed a string variable into SQL. Instead of
(Now, dbLastName, 'test ...
You need
(Now(), '" & dbLastName & "', 'test' …
In addition you need to make sure your variable will never include the single quote character, otherwise you have to also take that into account by doubling it.
I am trying something that has been done hundreds of times - except my code does not compile. Using this as a reference How to do INSERT into a table records extracted from another table I came up with the following sub, that is supposed to clear a table and refill it from another linked table (The idea is to liberate the linked table so it doesn't have a .laccdg file in its name for long)
Private Sub Form_Open(Cancel As Integer)
Dim rsDocs As Recordset
Dim sqlQuery As String
DoCmd.RunSQL "DELETE * FROM [Docs]"
sqlQuery = "INSERT INTO [Docs] (Numero, Description, [ID Symix], Groupe, [ID Sami])" & _
" SELECT [Unité] & "" "" & [Numéro Document] AS Numero, Description, [ID Symix], [Groupe Source], [ID Doc Sami]" & _
" FROM [Documents]"
Debug.Print sqlQuery
Set rsDocs = CurrentDb.OpenRecordset(sqlQuery)
CurrentDb.Execute sqlQuery
rsDocs.Close
Set rsDocs = Nothing
End Sub
I get an error on the execute line. The sql statement is wrong. Can someone see where it falters? Is the use of "" "" to join two fields together acceptable in this situation?
Also, clearing the table prompts a message asking if i am sure i want to do this. Will setting DisplayAlerts to False have negative consequences? Or should it be harmless if I put it back to True right after?
A general hint for debugging queries you design in VBA:
Do a Debug.Print of the SQL (you already have that)
Create a new query, close "Add tables"
switch to SQL view and paste your sql from the debug window
either execute the sql, or switch to design view.
Usually you will get a more specific error message from the query editor than what you get from VBA.
DisplayAlerts in Access is: DoCmd.SetWarnings True/False
But it is better to avoid it by using DB.Execute instead of DoCmd.RunSQL
No need for extra set of double quotes
"INSERT INTO [Docs] (Numero, Description, [ID Symix], Groupe, [ID Sami])" & _
" SELECT [Unité] & " " & [Numéro Document] AS Numero, Description, [ID Symix], [Groupe Source], [ID Doc Sami]" & _
" FROM [Documents]"
Instead of 2 sets of double quotes, which didn't work, or 1 set of double quotes, which don't produce a valid string, use 1 set of single quotes:
sqlQuery = "INSERT INTO [Docs] (Numero, Description, [ID Symix], Groupe, [ID Sami])" & _
" SELECT [Unité] & ' ' & [Numéro Document] AS Numero, Description, [ID Symix], [Groupe Source], [ID Doc Sami]" & _
" FROM [Documents]"
I got this error everytime I try to save the text in this field to the database. The code is this:
Dim strInsert As String
strInsert = "INSERT INTO Dbase (EB3_30) VALUES ('" & Me.EB3_30 & "')"
CurrentDb.Execute strInsert
The debugger stops at the last line and I get this error (I'm going to traduce it because it's in spanish):
run time error '3075':
Syntax Error (Missing Operator) in query expression "value of EB3_30"
The value of EB3_30 is a string of aproximate length 250. As it's in spanish some letters have accents. I also use dots, apostrophes and slashes. Maybe some of this characters cause problems? Any ideas?
Apostrophes probably cause problem.
Try this:
strInsert = "INSERT INTO Dbase (EB3_30) VALUES ('" & Replace(Me.EB3_30, "'", "''") & "')"
I have a form, and I want to fill it, and then save some of the fields into an existing table called Order.
I'm trying to do this with this line of code:
CurrentDb.Execute "INSERT INTO Order (OrderNumber)" & " VALUES (' " & Me.order & " ')"
I have also tried it like this
CurrentDb.Execute "INSERT INTO Order (OrderNumber)" & " VALUES ( " & Me.order & " )"
but it doesn't seem to make a difference. I keep getting the following error:
run-time error '3134': syntax error in INSERT INTO statement.
what am I doing wrong?
Order is a reserved word. If you must keep that as the table name, bracket it to avoid confusing the db engine.
Dim strInsert As String
strInsert = "INSERT INTO [Order] (OrderNumber) VALUES ('" & Me.order & "')"
Debug.Print strInsert
CurrentDb.Execute strInsert, dbFailOnError
If OrderNumber is numeric data type instead of text, discard those single quotes from the INSERT statement.
Store your statement in a string variable. Then use Debug.Print to examine the completed statement you're asking the engine to execute. You can view the Debug.Print output in the Immediate window. Go there with Ctrl+g Copy the statement and paste it into SQL View of a new Access query for troubleshooting.
running a vb application with the following code. I keep getting an error on my 'INSERT INTO' sql query, can anyone see what im doing wrong? This is the error - Syntax error in INSERT INTO statement.
connetionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = C:\Users\Dave\Documents\joblist.mdb;"
connection = New OleDb.OleDbConnection(connetionString)
Sql = "INSERT INTO jobList (StaffID, staffName, staffLastName, note, fault, section, techID, jobcomplete) VALUES ('" & staffid & "','" & staffFN & "','" & staffLN & "','" & staffNotes & "','" & staffFault & "', '" & staffSection & "', '" & techId & "','" & ava & "')"
connection.Open()
oledbAdapter.UpdateCommand = connection.CreateCommand
oledbAdapter.UpdateCommand.CommandText = Sql
oledbAdapter.UpdateCommand.ExecuteNonQuery()
connection.Close()
Me.JobListTableAdapter.Fill(Me.JoblistDataSet2.jobList)
Note and Section are reserved words in Jet SQL
You need to encapsulate them with square brackets
Sql = "INSERT INTO jobList (StaffID, staffName, staffLastName, [note], fault, " +
"[section], techID, jobcomplete) VALUES (......)"
This is the source of you syntax error, but .....
aside from that, you have many problems here:
As pointed out by Tim Schmeiter, you use the update command instead of
insert command.
you concatenate input text from your user to form an sql string. This
leads to sql injection attacks and problems in correct parsing of text
(apostrophes, invalid dates, invalid numbers, etc)
staffID and techID seems to be numeric fields in the database, but
you put their values inside single quotes like strings. If they are
numerics you will get another possible error there.
I assume you should use the InsertCommand instead of the UpdateCommand property since you are inserting.
oledbAdapter.InsertCommand.CommandText = "INSERT INTO jobList (StaffID, staffName, ....
Note that you're open for SQL-Injection and should use Parameters instead. You should also use Using statement to ensure that the connection gets closed even on error.