This is an inventory software. it has the field skid number as the primary unique key. i am trying to make an entry if it doesn't exist or update the entry if it does. not working so well l. what am i doing wrong and how can i fix this? I'm using visual basic 2010. i have tired many alternatives and have failed. Thank You for any help.
Try
strQuery3 = "INSERT INTO inv_by_skid(skid_num, cat, descript, cond, pr_count, cs_count, location, vendor, gender, size_run) VALUES ('" & add_skid_num.Text & "','" & add_category.Text & "','" & add_description.Text & "','" & add_condition_box.Text & "','" & add_pair_count.Text & "','1','" & add_location.Text & "','" & add_vender_num.Text & "','" & add_gender.Text & "','" & add_sizerun_box.Text & "') ON DUPLICATE KEY UPDATE SET pr_count = '" & temp_prcount_box.Text & "', cs_count = '" & temp_cscount_box.Text & "'"
SQLCmd3 = New MySqlCommand(strQuery3, dbCon3)
dbCon3.Open()
SQLCmd3.ExecuteNonQuery()
dbCon3.Close()
MsgBox("Skid Added Successfully!")
Catch ex As Exception
MsgBox("Failure!", ex.Message)
End Try
ON DUPLICATE KEY UPDATE SET
...is wrong... it's just
ON DUPLICATE KEY UPDATE
You also need to learn about sql injection and why your code contains massive security vulnerabilities.
Related
CurrentDb.Execute "INSERT INTO convenati(conid, location, surname, firstname, middlename, phone, email, dob, sex, mstatus, status, violated, date)"&_
"VALUES(" & Me.txtid & ", '" & me txtlocation & "','" & Me.txtsurname & "','" & Me.txtmiddlename & "','" & Me.txtfirstname & "','" & Me.txtphone & "','" & Me.txtemail & "','" & Me. txtdate &"','" &_ Me.cbogender &"','" &_ Me.cbomstatus &"','" &_ Me.cboviolated & "','" & Me.txtdate & "')"
Please I need help with this code.
I have a table called convenati, a form called formconvenati and a subform called convenati subform.
In my table I have these fields:
conid
location
surname
firstname
middlename
phone
email
dob
sex
mstatus
status
violated
data.
help me with it
Some of your problems:
you're missing a space after the ).
If any of the fields in the table are field type Date/Time, then there SQL should have # around the date, instead of '
Double-check the number of fields you list in the first half of the query, with the data you're adding (in the last half of the query). One is missing somewhere.
Missing a . after a me..
Put Option Explicit at the top of your modules (first line) to help find & fix undeclared/mishandled variables, objects, etc.
assume you don't have it all squished together like it appears in your example, but if you do, remove all _'s except at the end of the line.
CurrentDb.Execute "INSERT INTO convenati(conid, location, surname, firstname, " & _
"middlename, phone, email, dob, sex, mstatus, status, violated, date) " & _
"VALUES(" & Me.txtid & ", '" & Me.txtlocation & "','" & Me.txtsurname & _
"','" & Me.txtmiddlename & "','" & Me.txtfirstname & "','" & Me.txtphone & _
"','" & Me.txtemail & "','" & Me.txtdate & "','" & Me.cbogender & "','" & _
Me.cbomstatus & "','" & Me.cboviolated & "','" & Me.txtdate & ") "
Also, is the last field you listed in your question date or data?
...the problem all these things have in common is lack of attention to detail. "Close" doesn't count in coding.
One way to troubleshoot problems with SQL being used in VBA is to test the actual SQL output of your statement, in an actual query.
Using the statement above as an example, change CurrentDb.Execute to Debug.Print, and make the next line say Stop. Run your code, and when it breaks at the Stop, hit Ctrl+G to open the Immediate Window, and take a look at the complete SQL.
If you still can't find a problem, copy and paste that output into a Blank Query (in SQL view) and try running the query. It it won't run there either, then you need to start removing parts to simplify the query, troubleshooting by the process of elimination.
I don't know what values each of your form controls hold, nor what data type is expected for each of the fields in the table, so I replaced them all with ___ and got:
INSERT INTO convenati (conid, location, surname, firstname, middlename,
phone, email, dob, sex, mstatus, status, violated, date) VALUES(___,
'___','___','___','___','___','___','___','___','___','___','___')
More debugging tips here.
I have the following query and do have a problem with the syntax.
The query code looks like this:
query = "REPLACE INTO valuation (`ticker`,`depot_id`,`src_id`,`valuation_date`,`value`) VALUES ('" & strTicker & "','" & intDepot & "','" & intSrc & "','" & dateValuationDate & "','" & Format(CDbl(dblMktValue), "000") & "');"
which gives me the following string:
REPLACE INTO valuation (`ticker`,`depot_id`,`src_id`,`valuation_date`,`value`) VALUES ('BK001EUR','1','2','09.08.2017','14999260');
The fields are:
Varchar, Int, Int, Date, double
i guess i do have some problems with the quotations, but I am not sure how to fix it.
Thank you
You are likely getting errors on your numeric data, so you need to remove the unneeded quotes. Try this:
query = "REPLACE INTO valuation (`ticker`,`depot_id`,`src_id`,`valuation_date`,`value`) VALUES ('" & strTicker & "'," & intDepot & "," & intSrc & ",'" & dateValuationDate & "'," & Format(CDbl(dblMktValue), "000") & ");"
I have a database in MySQL named database1 and a table named records. I am trying to add the contents on my textboxes(3 textboxes) to my MySQL database in column form, not in row form.
Is it possible with concat???
The output I want
Here is my code, I created a row for typeofservice2, and typeofservice3but I only want one, which is type of service, and it looks like this
What I did
Query = "insert into database1.records (dateoftrans,typeofservice,typeofservice2,typeofservice3) values('" & TextBox12.Text & "','" & tos & "','" & tos2 & "','" & tos3 & "')"
tos, tos2, and tos3 are my textboxes
Hope someone can help me
At first the question for me is quite tricky but then I get your point, I give it a shot.
your current query is this.
Query = "insert into database1.records (dateoftrans,typeofservice,typeofservice2,typeofservice3) values('" & TextBox12.Text & "','" & tos & "','" & tos2 & "','" & tos3 & "')"
The best way i can give to you is this..
Create 3 x Insert Command and each Query has there own tos(textbox name). do something like this.
Query1 = insert into database1.records(dateoftrans,typeofservice) values('" & TextBox12.Text & "','" & tos & "')
Query2 = insert into database1.records(dateoftrans,typeofservice) values('" & TextBox12.Text & "','" & tos2 & "')
Query2 = insert into database1.records(dateoftrans,typeofservice) values('" & TextBox12.Text & "','" & tos3 & "')
by that you can save all the values in same column.
I am trying to insert values from an Excel VBA userform to MySQLDBA.
The problem comes with empty text box values for date columns.
MySQL doesn't accept " but it allows Null.
Can I add a case statement in my insert query to check if the value for the column is empty string, and if it is, insert null for that column? Otherwise, it should insert the value of the textbox.
Here is my current query in VBA, where Date_of_Birth columns should be checked for empty string:
Str = "insert into sample.details (ID,Name,Age,Gender,Date_of_Birth) values ('" & txt1.Value & "', '" & txt2.Value & "','" & txt3.Value & "','" & txt4.Value & "','" & txt5.Value & "');"
P.S I tried setting a if condition for the txt5.value but that didn't work.
You can use the IIf function.
Str = "insert into sample.details (ID,Name,Age,Gender,Date_of_Birth) values ('" & txt1.Value & "', '" & txt2.Value & "','" & txt3.Value & "','" & txt4.Value & "'," & IIf(txt5.Value="","null","'" & txt5.Value & "'") & ");"
I have small application in vb.net
I have one table in msaccess, which is having around 20,000 records,
now, I want to insert these records to remote server of mysql,
I tried as below code, it takes around 3-4 hours to insert data,
Can anyone show me faster way to insert data, i will be very glad..
dst in code is having all records from ms Access
For i As Integer = 0 To dst.Tables(0).Rows.Count - 1
mycmd.Connection = mycn
str = "INSERT INTO tblstudentresults(department_desc, grade, roll_no, name, course_code, course_desc, examination_type, total_marks, obtained_marks)"
/*'str = str & " VALUES('" & cname & "','" & sdr("grade").ToString() & "','" & sdr("st_code").ToString() & "','" & sdr("stName").ToString() & "','" & sdr("Subject_code").ToString() & "','" & sdr("Subject").ToString() & "','" & sdr("ExamTitle").ToString() & "','" & sdr("Maxmark").ToString() & "','" & sdr("score").ToString() & "')" -- Added non-VB comment here to improve readability */
str = str & " VALUES('" & cname & "', '" & dst.Tables(0).Rows(i)("grade").ToString() & "', '" & dst.Tables(0).Rows(i)("st_code").ToString() & "', '" & dst.Tables(0).Rows(i)("stName").ToString() & "', '', '" & dst.Tables(0).Rows(i)("Subject").ToString() & "', '" & dst.Tables(0).Rows(i)("ExamTitle").ToString() & "', '" & dst.Tables(0).Rows(i)("Maxmark").ToString() & "', '" & dst.Tables(0).Rows(i)("score").ToString() & "')"
mycmd.CommandText = str
mycmd.ExecuteNonQuery()
next
It might be faster to construct a multiple-row insert in one large string (or maybe chunks of, say 500 rows), then run the entire insert statement in a single call, something roughly like the following:
Dim firstRow as Boolean = True
str = "INSERT INTO tblstudentresults(...) VALUES"
For i As Integer = 0 To dst.Tables(0).Rows.Count - 1
' Only insert comma after first row, so we don't have comma at the end.
If Not firstRow Then str = str & ","
str = str & "('" & cname & "','" ...
firstRow = False
Next
mycmd.Connection = mycn
mycmd.CommandText = str
mycmd.ExecuteNonQuery()