VBA SQL Replace Into Syntax Error - mysql

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") & ");"

Related

Visual Basic Access - recording information from Textboxes

I'm trying to create a form that allows you to register users. I've looked up tutorials and none of them seem to work. This is the code I am stuck with currently:
CurrentDb.Execute "INSERT INTO tblUser(User ID, Fullname, Username, Password, Security) " _
& VALUES & " (" & Me.ID & ",'" & Me.Fullname & "','" & Me.Uname & "','" & _
Me.uPass & "','" & Me.Pri & "')"
When I run the code I get:
Runtime error '3134': Syntax error in INSERT INTO statement.
Two errors:
You have space in one of your field. Use brackets if you have space
or if you use reserved words.
VALUES must be included in the string, not as a varible.
Correction:
CurrentDb.Execute "INSERT INTO tblUser([User ID], Fullname, Username, Password, Security) " & _
"VALUES (" & Me.ID & ",'" & Me.Fullname & "','" & Me.Uname & "','" & _
Me.uPass & "','" & Me.Pri & "')"
From my comment, try below:
CurrentDb.Execute "INSERT INTO tblUser(User ID, Fullname, Username, Password, Security) " _
& "VALUES(" & Me.ID & ",'" & Me.Fullname & "','" & Me.Uname & "','" & _
Me.uPass & "','" & Me.Pri & "')"
Password is a reserved word, so:
CurrentDb.Execute "INSERT INTO tblUser(User ID, Fullname, Username, [Password], Security) " _

How to display values from multiple textbox(vb net) to mysql database in column form

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.

Add case statement in insert query to turn empty string into null

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 & "'") & ");"

Insert Into Statement VBA [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Hi everyone I am coding in VBA using Access 2013 I wrote my code two different ways and keep getting a syntax error. Here's my code.
Private Sub cmdAdd_Click()
CurrentDb.Execute "INSERT INTO GroupVolunteers(Group, Leader, Name,
phone, email, EmergencyContact, EmergencyContact) " & _
" VALUES(" & Me.txtGroup & "','" & Me.cboLeader & "','" & Me.txtName & "','"
& Me.txtEmail & "','" & Me.txtPhone & "','" & Me.txtEmergencyContact & "','"
& Me.EmergencyNumber & "','" & Me.txtRegNumber & "')"
'clear form
cmdClear_Click
'refresh data in list on form
frmStudentSub.Form.Requery
End Sub
OR
'when we click on button Add there are two options
'1. for insert
'2. for update
If Me.txtRegNumber & "" = "" Then
'this is for insert new
'add data to table
CurrentDb.Execute "INSERT INTO GroupVolunteers(Group, Leader, Name,
phone, email, EmergencyContact, EmergencyContact) " & _
" VALUES(" & Me.txtGroup & "','" & Me.cboLeader & "','" &
Me.txtName & "','" & _
Me.txtEmail & "','" & Me.txtPhone & "','" &
Me.txtEmergencyContact & "','" & Me.EmergencyNumber & "','" &_
Me.txtRegNumber & "')"
Else
'otherwise (Tag of txtID store the id of student to be modified)
CurrentDb.Execute "UPDATE GroupVolunteers " & _
" SET Group=" & Me.txtGroup & _
", leader='" & Me.cboLeader & "'" & _
", name='" & Me.txtName & "'" & _
", email='" & Me.txtEmail & "'" & _
", phone='" & Me.txtPhone & "'" & _
", EmergencyContact='" & Me.txtEmergencyContact & "'" & _
", EmergencyNumber='" & Me.txtEmergencyNumber & "'" & _
", NumberVolunteers ='" & Me.txtNumberVolunteers & "'" & _
" WHERE RegNumber = " & Me.txtRegNumber.Tag
" VALUES(" & Me.txtGroup & "', ...
Think of how that's going to end up in your statement:
VALUES(<Me.txtGroup>', ...
In other words, you're either missing the opening quote for a character-type column or you have too many for a numeric-type column. It should be one of:
" VALUES('" & Me.txtGroup & "', ... // for character-type column
" VALUES(" & Me.txtGroup & ", ... // for numeric-type column
That should fix your insert in both code blocks, you may also want to examine the update in the second code block as well. It has no quotes on the group column which is okay if it's numeric-type but probably not if it's character-type.

Listbox in Ms access

I can store records in the DB by using combobox with the following code.
Here single part number is selected and partnumber related data is stored in the DB table.
But I want the code for Listbox...When I select multiple partnumbers ..how can I store in the DB table?
Case "Pn ADDED to Wrapper", _
"Pn REMOVED from Wrapper"
If Me!PartNumber <> "All" And Me!PartNumber <> "Select" Then ' a proper part number has been selected in combo box
strNewSq5 = _
"INSERT INTO tblTmpEventLog (TrackingNumber,PartNumber,PartNumberChgLvl,EnteredBy,EventTypeSelected,EventDate)"
strNewSq5 = strNewSq5 & " VALUES ('" & tempTrackingNumber & "','" & _
tempPartNumber & "','" & _
tempPartNumberChgLvl & "','" & _
tempEnteredBy & "','" & _
tempEventTypeSelected & "'," & _
"#" & Forms!frmEventLog_Input.EventDate & "#)"
dbs.Execute strNewSq5, dbFailOnError
TrnsfTmpEventToEventLog
Else
displayMsgBox = MsgBox("A single part number must be specified. Please correct.", vbCritical, "System Error")
Exit Sub
End If
You need to iterate over the selected items and store them one by one:
MS Access 2007 - Cycling through values in a list box to grab id's for a SQL statement
EDIT re comment
You do not provide sufficient information for a detailed answer, but here are some notes that may help.
For Each itm In Me.NameOfListBox.ItemsSelected
If Instr("All,Select",Me.NameOfListBox.Column(0, itm) )=0 Then
'' a proper part number has been selected in list box
'' Me.NameOfListBox.Column(0, itm) is the column (zero in this example
'' and row (itm) of the selected item in the list box. If it is the
'' part number, then you might like to say:
'' tempPartNumber = Me.NameOfListBox.Column(0, itm)
strNewSq5 = "INSERT INTO tblTmpEventLog " & _
"(TrackingNumber,PartNumber,PartNumberChgLvl,EnteredBy," & _
"EventTypeSelected,EventDate)"
strNewSq5 = strNewSq5 & " VALUES ('" & tempTrackingNumber & "','" & _
tempPartNumber & "','" & _
tempPartNumberChgLvl & "','" & _
tempEnteredBy & "','" & _
tempEventTypeSelected & "'," & _
"#" & Forms!frmEventLog_Input.EventDate & "#)"
dbs.Execute strNewSq5, dbFailOnError
TrnsfTmpEventToEventLog
Else
''Do not insert
End If
Next