I am using MS Access 1997 version (.mdb file). On a daily basis, I need to insert values manually. In that file, there's a column Logical (Boolean data type). I am automate this template using SQL query instead of direct entry.
Below is my insert query:
Insert Into Data_CustomerTransmit_Tbl (Logical)
Values (" & Logicalnme & ")
Values:
Logicalnme - True
When I run this query in VBA in Excel, I get this error message
Syntax Error in Insert into Statement
Kindly confirm shall I use "Logical" as column name or this is the reserved keyword?
Thanks in advance.
There isn't a problem with your field name, you just need to enclose your INSERT column name in square brackets. You also need to choose a valid value in the VALUES clause:
INSERT INTO Data_CustomerTransmit_Tbl ( [Logical] )
VALUES (TRUE);
If you want to be prompted for the value to insert, you can use a parameter:
PARAMETERS [Please enter a Boolean value] YesNo;
INSERT INTO Data_CustomerTransmit_Tbl ( [Logical] )
VALUES ([Please enter a Boolean value]);
I presume you are trying to do this insert using VBA? If so, your syntax in building the SQL statement is correct, except you have some punctuation missing: double-quotes on each end.
"INSERT INTO Data_CustomerTransmit_Tbl (Logical) VALUES (" & Logicalnme & ")"
Further, as you have split the string over two lines (breaking before VALUES), you must also terminate the first line of the string with: ' " & _' (space,double-quote,space, ampersand, space, underscore) in order to indicate that the string continues to the next line. Then you begin the next line with double-quotes:
"INSERT INTO Data_CustomerTransmit_Tbl (Logical) " & _
"VALUES (" & Logicalnme & ")"
In VBA the code should look like this:
Docmd.RunSQL("INSERT INTO Data_CustomerTransmit_Tbl (Logical) VALUES (" & Logicalnme & ");"
The SQL query you alluded to - have you tried to execute it manually in the query editor using the same value(s) you are trying to pass from Excel? That would immediately provide more verbose feedback if there is an issue with the query or the data.
Regarding the Boolean field, make sure you are receiving a True/False that you are expecting, not a bit field, 0 and 1. I like to make quick log entries in a table or a file when troubleshooting to see the raw data.
Use Cbool function:
Insert Into Data_CustomerTransmit_Tbl (Logical)
Values (" & Cbool(Logicalnme) & ")
Add single quotes around values?
sql = "INSERT INTO Data_CustomerTransmit_Tbl (Logical) " & _
"VALUES ('" & Logicalnme & "')"
docmd.runsql sql
Related
I've built a function that creates a comma delimited string from multiple selections in my list box. When I try to use this as the Where clause in my sql (in VBA) I get a 'type mismatch' error - because the field I'm using with the criteria is numeric. How do I resolve this?
example:
sql = "INSERT INTO tblItemsLibrary ( Constr, Description) ...
"WHERE (((tblItemsLibrary1.ItemsLibID) In (" & strSelectedRecords & ")));"
In the above example strSelectedRecords is coming in as "17,11,28" where the field is a long.
You can use LIKE:
INSERT INTO tblItemsLibrary ( Constr, Description)
...
WHERE "," & tblItemsLibrary1.ItemsLibID) & "," LIKE "*," & strSelectedRecords & ",*"
Actually you might want to adjust strSelectedRecords to start and end with *, and ,*.
If you are using SQL Server 2016 or above, the function string_split can be used:
SELECT *
FROM tblItemsLibrary
WHERE tblItemsLibrary1.ItemsLibID IN (select * from STRING_SPLIT(#numlist, ','))
Replace #numlist with your string 17,11,28
Another method is to construct dynamic sql and pass that on top of base sql statement:
EXECUTE('SELECT * FROM tblItemsLibrary1 WHERE ItemsLibID IN
('+#numlist+')')
Your SQL expression will result in:
INSERT INTO tblItemsLibrary ( Constr, Description)
...
WHERE (((tblItemsLibrary1.ItemsLibID) In (17,11,28)));
which is correct if ItemsLibID is numeric.
Thus, your error is probably caused by the values you try to insert.
I need to use VBA excel macro to load Chinese character data from excel spreadsheet to DB2 using SQL Insert Statement. I know that this cannot work with normal Insert statement as below:
INSERT INTO table_name
VALUES (value1,value2,value3,...);
Since this is my first time dealing with double byte character, can anybody help to guide me on which method I can use to load DBCS data into DB2.
Thanks in advance.
A few things:
Not knowing your table and column names, enclose them with square brackets to be on the safe side
There must be no space between the N' prefix and the Unicode data, nor a space after the value itself
Your data1 value is not enclosed in quotation marks; this means it must be a numeric value. If it is not a numeric value, enclose it in quotation marks. If it is a Unicode string, enclose it in "N'..."'"
Try with this:
oStr = "INSERT INTO [" & tableName & "] ([" & field1 & "], [" & field2 & "]) Values(" & data1 "," & " N'" & data2 & "');"
I would like to insert a row with SQL (in Visual Basic) and receive something like: "Error: You have an errin in your SQL syntax near IF NOT EXISTS ... at line 1"
Code looks like:
...
Try
query = "IF NOT EXISTS (Select Movie_Name from MovieDB where Movie_Name=" & placeholder & ") INSERT INTO MovieDB (Movie_Name) VALUES ('" & placeholder & "')"
'PREPARE CONNECTION
MySQLCmd = New MySqlCommand(query, dbConnection)
'OPEN DB
dbConnection.Open()
'EXECUTE QUERY
MySQLCmd.ExecuteNonQuery()
'CLOSE DB
dbConnection.Close()
Catch ex As Exception
...
I really dont get it anymore. Some ideas?
At a minimum you need single quotes around the first string as well:
--V --V
query = "IF NOT EXISTS (Select Movie_Name from MovieDB where Movie_Name='" & placeholder & "'); INSERT INTO MovieDB (Movie_Name) VALUES ('" & placeholder & "')"
As #Cameron astutely noticed, you also need a semicolon between the EXISTS function and the INSERT command
But you should look into using parameters instead of concatenating SQL - not only does it remove the need to delimit strings, but it prevents SQL injection and avoids syntax error if the string contains an apostrophe.
As far as I know "IF NOT EXISTS INSERT" is no valid MySQL syntax. This should work:
query = "INSERT INTO MovieDB (Movie_Name) VALUES ('" & placeholder & "') ON DUPLICATE KEY UPDATE Movie_Name = Movie_Name";
This tries to insert the new movie name and if this fails it does virtually nothing. (For this to work there must be a unique index on Movie_Name.)
I am trying to add new records to a Access database through VB.NET. The table "Quantities" has three columns:
PartNumber (string), PadsPerStrip (integer), and Verified (boolean)
The format that we use for PartNumber is ###-####-### (ie 901-0656-000). When I run my code everything is added correctly but math is performed on Part number so that the - is treated as a minus sign even though it is a string. Here is my sql command:
cmdInsert.CommandText = "INSERT INTO Quantities (PartNumber, PadsPerStrip, Verified) VALUES ( " & partNum & ", " & updatingPPS.ToString() & ", No);"
When viewing the command in a MsgBox it shows up as:
INSERT INTO Quantities (PartNumber, PadsPerStrip, Verified) VALUES (901-0656-000, 3, No);
Is there a way to make it skip the math operator when exporting the part number?
Make sure to enclose the part number with quotes. Without that, the value is not considered as a string when run in the database.
I'm trying to run code that will copy fields into a new table, moving them from a _New table to the original table. The VBA code that does this works as such:
SQLStatement = CStr("INSERT INTO " & TName & " SELECT * FROM " & TName & "_New")
Log.WriteLine "Running query with string """ & SQLStatement & """ "
QueryTimer = Timer
DoCmd.RunSQL SQLStatement
Log.WriteLine "Query time: " & (Timer - QueryTimer)
The log is just a handful of procedures in a class module I threw together. Its output on the error is
#142921: Running query with string "INSERT INTO Records SELECT * FROM Records_New"
#142941: Error Date/Time: 7/21/2009 2:29:40 PM
#142941: Error # & Description: 3162, You tried to assign the Null value to a variable that is not a Variant data type.
I can confirm that TName and SQLStatement are both valid strings at the time the SQL operation is run, and that the sources (Records and Records_New) are both valid. Option Explicit is set elsewhere in the file to avoid any confusion from typos. The error is thrown on the DoCmd line.
Why would this have a Null value, even though DoCmd.RunSQL doesn't return a value?
Can you post the table descriptions for Records and Records_New tables?
I would wager that you are trying to insert a NULL value into one of the columns of the "Records" table (and the column description is NOT NULL).
Hope this helps.
I think it will help if you also change the insert statement to be more specific about which columns it is inserting/selecting. You are asking for bugs by being so non-specific.
This may seem like it is non-responsive to your answer, but I suspect that the columns in the select table and destination table are either not lined up, or there is a field in the destination table that disallows null.
Try this:
In a new Query (in SQL view) paste your query "INSERT INTO Records SELECT * FROM Records_New" in and try to run it manually. I bet you get a more specific error and can troubleshoot the query there before running it with the added complexity of the code around it.
INSERT INTO Statement (Microsoft Access SQL)
Your SQL INSERT statement is incorrect - it should be:
INSERT INTO Records SELECT * FROM [Records_New];
Here's what you need to use:
CStr("INSERT INTO " & TName & " SELECT * FROM [" & TName & "_New)"];")
Maybe Timer needs parens?
QueryTimer = Timer()