i want to concatenate sql query that is not in single line but new line .
Conn.Execute "insert into users("
"FirstName,"
"LastName,"
"Address1,"
"Address2,"
")values("
" UCase(txtfirstname.Text) &" ,
"& UCase(txtlastname.Text) &",
" & UCase(txtaddress1.Text) & ",
" & UCase(txtaddress2.Text) & "
")"
how to concatenate them into single one?
Thanks ,
Yugal
In the VB6 to do a line continuation you end a line with [space][underscore]. You also need to use the & to concatenate the text segments on each line together. So the line would look something like this:
Conn.Execute "insert into users(" _
& "FirstName," _
& "LastName," _
& "Address1," _
& "Address2" _
& ")values(" _
& UCase(txtfirstname.Text) & "," _
& UCase(txtlastname.Text) & "," _
& UCase(txtaddress1.Text) & "," _
& UCase(txtaddress2.Text) _
& ")"
Something like this perhaps:
Conn.Execute "insert into users(FirstName, LastName, Address1, Address2) values('" & UCase(txtfirstname.Text) & "', '" & UCase(txtlastname.Text) & "', '" & UCase(txtaddress1.Text) & "', '" & UCase(txtaddress2.Text) & "'")"
Related
I got Syntax error in number using query expression? error while I run below code
CurrentDb.Execute "UPDATE StateBudget " & _
" Set S_ID='" & Me.cbState & "'" & _
", C_ID=" & Me.cbCategory & _
", Year='" & Me.cbYear & "'" & _
", 1=" & Me.Ctl1 & _
", 2=" & Me.Ctl2 & _
", 3=" & Me.Ctl3 & _
" WHERE S_ID = """ & _
DLookup("ID", "States", "State='" & _
Me.subformStateBudget.Form.Recordset.Fields("State") & "'") & """"
S_ID and Year are Text fields, C_ID and the rest 1,2,3 are Number fields.
If there anything I missed? I am a beginner in programming so...
Probably one of number Me.* fields is null, so SQL will have syntax error. Use Nz function for such kind arguments. For debugging create SQL in string variable before executing and dump content of this variable to Immediate window using Debug.Print. You will see the problem easily.
Dim strSQL
strSQL = "UPDATE StateBudget " & _
" Set S_ID='" & Me.cbState & "'" & _
", C_ID=" & Nz(Me.cbCategory, 0) & _
", Year='" & Me.cbYear & "'" & _
", 1=" & Nz(Me.Ctl1, 0) & _
", 2=" & Nz(Me.Ctl2, 0) & _
", 3=" & Nz(Me.Ctl3, 0) & _
" WHERE S_ID = """ & _
DLookup("ID", "States", "State='" & _
Me.subformStateBudget.Form.Recordset.Fields("State") & "'") & """"
Debug.Print strSQL
CurrentDb.Execute strSQL, dbFailOnError
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.
So I'm trying to teach myself VBA again and I'm having a cople of troubles. I'm trying to add new users to a table but keep getting the above error when I click my "Update" button. The text field will be in the form of 2 letters and 5 numbers. XX11111 for example.
Private Sub cmdAdd_Click()
'when we click on button Add there are two options
'1. for insert
'2. for update
If Me.txtLoginName.Tag & "" = "" Then
'add data to table
CurrentDb.Execute "INSERT INTO tblUsers(LoginName,UserName,Rank) " & _
" VALUES('" & Me.txtLoginName & "','" & Me.txtUsername & "','" & Me.cboRank & "')"
Else
CurrentDb.Execute "UPDATE tblUsers " & _
"set LoginName=" & Me.txtLoginName & "'" & _
", UserName='" & Me.txtUsername & "'" & _
", Rank='" & Me.cboRank & "'" & _
" WHERE LoginName=" & Me.txtLoginName.Tag
End If
'clear form
cmdClear_Click
'refresh data in list on form
frmModifyUsersSub.Form.Requery
End Sub
you are missing a ' in this line:
" set LoginName=" & Me.txtLoginName & "'" & _
change it to
" set LoginName='" & Me.txtLoginName & "'" & _
and :
" WHERE LoginName=" & Me.txtLoginName.Tag
to
" WHERE (LoginName='" & Me.txtLoginName.Tag & "')" ' and I don't know if this your intended where condition.
The error pretty much gives you the answer. You need 2 parameters for the function it is failing on. One thing to try is to change
CurrentDb.Execute "INSERT INTO tblUsers(LoginName,UserName,Rank) " & _
" VALUES('" & Me.txtLoginName & "','" & Me.txtUsername & "','" & Me.cboRank & "')"
and
CurrentDb.Execute "UPDATE tblUsers " & _
"set LoginName=" & Me.txtLoginName & "'" & _
", UserName='" & Me.txtUsername & "'" & _
", Rank='" & Me.cboRank & "'" & _
" WHERE LoginName=" & Me.txtLoginName.Tag
To
CurrentDb.Execute "INSERT INTO tblUsers(LoginName,UserName,Rank) " & _
" VALUES('" & Me.txtLoginName & "','" & Me.txtUsername & "','" & Me.cboRank & "')",dbFailOnError
and
CurrentDb.Execute "UPDATE tblUsers " & _
"set LoginName='" & Me.txtLoginName & "'" & _
", UserName='" & Me.txtUsername & "'" & _
", Rank='" & Me.cboRank & "'" & _
" WHERE LoginName='" & Me.txtLoginName.Tag & "'", dbFailOnError
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
this is my code and im having the problems for 2 hours now
Dim Query As String
con.Open()
Query = "INSERT INTO `" & finalTableName & "` VALUES" & _
"(" & _
" '" & varDateTime & "'," & _
" '" & varComputer & "'," & _
" '" & vard & "'," & _
" '" & varll & "' ," & _
" '" & varPp & "'," & _
" '" & varVv & "' ," & _
" '" & varIi & "' ," & _
" '" & varIc & "'," & _
" '" & varPc & "'," & _
" '" & varSs & "'," & _
" '" & varRd & "'," & _
" '" & varIpd & "'," & _
" '" & varMg & "'," & _
" '', " & _
" '" & varRuleId & "', " & _
" '" & varDateUploaded & "' " & _
")"
Dim cmd As MySqlCommand = New MySqlCommand(Query, con)
If (cmd.ExecuteNonQuery()) Then
End If
con.Close()
This is the problem that im having
You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near 'dd', 'Deleted',
'344', '', '4', '2014-03-05 15:29:37' )' at line 1
Use command parameters
Dim Query As String
Query = "INSERT INTO `" & finalTableName & _
"` VALUES(#1, #2, #3, #4, #5, #6, #7, #8, #9, #10, #11, #12, #13, '', #14, #15)"
Dim cmd As MySqlCommand = New MySqlCommand(Query, con)
cmd.AddWithValue("#1", varDateTime);
cmd.AddWithValue("#2", varComputer);
cmd.AddWithValue("#3", vard);
cmd.AddWithValue("#4", varll);
cmd.AddWithValue("#5", varPp);
cmd.AddWithValue("#6", varVv);
cmd.AddWithValue("#7", varIi);
cmd.AddWithValue("#8", varIc);
cmd.AddWithValue("#9", varPc);
cmd.AddWithValue("#10", varSs);
cmd.AddWithValue("#11", varRd);
cmd.AddWithValue("#12", varIpd);
cmd.AddWithValue("#13", varMg);
cmd.AddWithValue("#14", varRuleId);
cmd.AddWithValue("#15", varDateUploaded);
con.Open()
If cmd.ExecuteNonQuery() = 1 Then
End If
con.Close()
The parameters will automatically be add the right way correponding the column type (text as text, numbers as numbers, dates as dates).
Also I recommend to add the column names part. This makes the command safer. If you later add or remove columns and the column order is changed, it will still work or at least throw an exception and not silently insert a value into a wrong column.
INSERT INTO table (col1, col2, col3) VALUES (val1, val2, val3)
Get rid of the ` and use ' instead.
As in:
`" & finalTableName & "`
should be
'" & finalTableName & "'
This is what I have for the Add Event.
CurrentDb.Execute "INSERT INTO tblMatchBook ([Patron], [Staff], [TimeReceived], [SurveyLink], [DateFinished], [BookTitles]) " & _
" Values('" & Me.ddBarcode & "," & Me.ddStaff & "," & Me.txtRDate & "," & Me.txtLink & "," & "" & "," & "" & "')"
I cannot figure out what is wrong.
insert into Main values (28494,1,False,'Buto-asma Sirop' , 'Buto-asma Sirop', 3.99 , 'Syrup', 'ispani', ' ', ' ',0, '1',4988 )