VB6 Access update query not updating table - sql

I am trying to add a number sequentially to a field for a job number in a do loop. My code is not breaking but it is not adding the records to the table. The code is written in a function that is being called when the access form button is pressed to update the records. Below is my code:
Dim NumofBatches As Integer
NumofBatches = [Batches]
Dim startnum As Integer
startnum = 1
Dim jobnum As String
jobnum = [JobNumber]
Do While startnum <= NumofBatches
Dim mynumString As String
mynumString = startnum
DoCmd.RunSQL "INSERT INTO Production (CardCode,JobItemNo,JobIndex,DrawingRef,DRDescription,[CreationDate],Quantity,FinishDate,LastLocation,DateLastMoved) VALUES ('" & jobnum & mynumString & "', ItemNumber, JobNumber, DrawingRef, DRDescription, [CreationDate], Quantity, FinishDate, LastLocation, DateLastMoved)"
startnum = startnum + 1
Loop
I realize I will have duplicated rows of fields except for the cardcode field which is what I am trying to achieve. The cardcode fields should be sequential in each row for the jobnumber. An example, 100011, 100012, where 10001 is the job number and it is adding 1, 2, etc. sequentially.

You have a couple of problems. The first is in the field list in the INSERT part of your SQL statement. Unless you have a column in your Production table named "CardCode + mynumString" (and if you did it would have to have square brackets around it because it has a space and a symbol in it) it's not going to work. Inside the parentheses after INSERT INTO <TableName> you have to list column names from the table you are inserting into. Fix your target column names first.
Next you have a SELECT statement as the source of values you're inserting. Unless the values you need are found in a table, you should probably use a VALUES list:
INSERT INTO <TableName> (Col1, Col2, ...) VALUES (Val1, Val2, ...)
The final issue is that you have to concatenate your variables into the SQL string. I'm going to assume that [Text90] is a control with your CardCode value in it and you're wanting to concatenate it with mynumString. That would look something like this:
DoCmd.RunSQL "INSERT INTO Production (Col1, Col2, ...) VALUES ('" & [Text90] & mynumString & "', Val2, ...)"
Note that you have to resolve your value outside of the quoted SQL string and concatenate that value into the string and set all of that off with single-tick quotes inside the SQL string.
I find it handy to build dynamic SQL strings like this using a variable and then execute it as:
DoCmd.RunSQL mySQLVariable
Having the SQL string in a variable makes it a little easier to spot errors in the concatenation if you inspect the variable during debugging or if you just dump the value to the immediate window with Debug.Print.

Related

how to insert multiple row in single query in PostgreSQL

I have a variable that holds multiple data, and I want to insert this multiple data into PostgreSQL database using a single query.
the field on my table is this: stud_tbl(id,fname,lname)
and this is the variable that holds multiple data;
variable = (123,ron,lum),(234,nald,bay),(345,rol,lumz)
my query:
str = "insert into stud_tbl values ('" & variable & "')"
when I execute my query their was an error and I can't identify the error.
To expand on #Patrick's comment:
variable = "(123,'ron','lum'),(234,'nald','bay'),(345,'rol','lumz')"
the query:
str = "insert into stud_tbl values " & variable
Though the usual warnings (How can I prevent SQL injection in PHP?) about this not being best practice apply.
Value of your variable should be enclosed with quotation marks
variable = "(123,ron,lum),(234,nald,bay),(345,rol,lumz)"

Insert multiple rows into a single table from a textbox form - Access 2010

I have a table with a single field (defined as Number) in which I would like to insert multiple rows from a textbox in a form. The code is:
Private Sub CmdImportID_Click()
Dim strSQL As String
strSQL = "DELETE * FROM [Con_ID futcanc BAMB]"
DoCmd.RunSQL strSQL
strSQL = "INSERT INTO [Con_ID futcanc BAMB] (con_id) VALUES (" & Me!conID & ");"
DoCmd.RunSQL strSQL
End Sub
Problem is: it works with a single number in textbox, but when I try to insert more than one I receive "Run-time error '3075': Syntax error (missing operator) in query expression".
How can I modify the code to insert more data? Sorry, I'm not really skilled in coding vba :(
Thank you in advance.
The code you have written is for a single row insert and it will run only for one time. If a seperator is present for the numbers in Textbox, then loop through each number by splitting the Text with seperator and insert as seperate rows in DB.
Generally
INSERT INTO [Con_ID futcanc BAMB] (con_id) VALUES (1)
will inserts only one row and
INSERT INTO [Con_ID futcanc BAMB] (con_id) VALUES (1,2) is Invalid statement.
EDIT:
Refer the below link for Split and Loop code Logic
Split and Loop

Adding ' at the start of each row of a table through sql

I have a column called "product-code". These are all populated. I am wanting to do a query that will insert a ' at the start of each field and then another query to add a ' at the end of the field.
So for example at the moment a product code might be fmx-2, after the query I would want it to look like 'fmx-22'
I am looking to do this for all the data sets within the table. I am using Microsoft Access
Thanks
In Microsoft Access you can use & char to concatenate string, and your query could be something similar:
update my_table set product_code = "'" & product_code & "'";

Writing to multiple columns simulatenously with SQL

I am having trouble writing a VBA macro within Microsoft Access. What I am trying to do is use SQL to create an output table, but I want to write to multiple columns simultaneously.
This gets me all the values I need for one column:
Docmd.RunSQL “INSERT INTO Output (TargetCol1) SELECT [Field1] FROM [Table1] WHERE [Criteria1] = ‘Value’ GROUP BY Field1”
When I try to run this multiple times to get the values I need for other columns. INSERT INTO writes the data as new records, so I end up with blank spaces, like this:
Field1----Field2
Value----<Null>
Value----<Null>
Value----<Null>
<Null>----Value
<Null>----Value
What I want is:
Field1----Field2
Value---- Value
Value---- Value
Value----<Null>
I tried to create variables and create kind of a nested statement but I receive a ‘Compile error, object required’ on my first line when I try to run what I have written:
Set x = Docmd.RunSQL “INSERT INTO Output (TargetCol1) SELECT [Field1] FROM [Table1] WHERE [Criteria1] = ‘Value’ GROUP BY Field1”
Set y = Docmd.RunSQL “INSERT INTO Output (TargetCol2) SELECT [Field2] FROM [Table1] WHERE [Criteria2] = ‘Value’ GROUP BY Field2”
Docmd.runsql “INSERT INTO Output (TargetCol1, TargetCol2) Values (x,y)”
Why not:
INSERT INTO Output (TargetCol1,TargetCol2) SELECT [Field1,Field2] FROM [Table1] [Criteria1] = 'Value'"
Set is used for objects, and you do not have one in Set x = Docmd.RunSQL. Order by is irrelevant for a table.
The easiest way to set up queries is using the query design window. it will guide you through creating the query and you can then switch to SQL view to get SQL.
I suggest you do not use RunSQL : What's the difference between DoCmd.SetWarnings and CurrentDB.Execute

getting error on while inserting a single quotation values

Table1
EmpName1
Raja
Rav'i
Ramu'i
Rajes'ih
Table2
EmpName2
....
When I inserting table2.empname2 from table1.empname1, It inserted only the raja, remaining rows were not inserted it showing error in '(single quotation)
"Showing error as incorrect syntax near rav"
vb6 code.
INSERT INTO table2 (EmpName) VALUES('" & EmpName & "')
How can I insert a name with a single quotation also?
You can either escape your single quotation with something like
empName = replace(empName, "'", "''")
or paramertize your query...
It is highly recommended to use a parametrized query (see e.g. this article), instead of building your SQL query as a string. This protects you from many forms of SQL injection, and is also often faster.
Or directly use some kind of persistence framework, if you have a lot of DB interaction.
Your query will not work because in string SQL queries, the ' must be escaped (as ''). And BTW, if you want to insert values from one table into another, it is usually not a good idea to do a SELECT, then an INSERT in a loop, like you do. Just do a
INSERT INTO table2 SELECT ... FROM table1