Inserting SQL DATA into VB Database - mysql

Sorry for the NOOB question I just started with VB. I finally managed to connect my program with my phpmyadmin account, but now I have this little problem.
I am trying to enter the Data the user entered into the form into the db table.
My syntax is wrong. Im doing the following:
Dim SQLStatment As String = "INSERT INTO students(Title, Initial, Surname, Address, City, Country, Postcode) VALUES('" & vtital & vinital & surname & vstreet & vcity & vcountry & vpcode '")"
If anyone could be so kind to give me a brief explanation on how to enter multiple variable data into a db using vb.net it would be much appreciated.

You forgot the separator
It should be like this ..
Dim SQLStatment As String = "INSERT INTO students(Title, Initial, Surname, Address, City, Country, Postcode) VALUES('" & vtital & "','" & vinital & "','" & surname & "','" & vstreet & "','" & vcity & "','" & vcountry & "','" & vpcode & "')"
Later .. use parameterized..

This should work:
Dim SQLStatment As String = String.Format("INSERT INTO students(Title, Initial, Surname, Address, City, Country, Postcode) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",
vtital,
vinital,
surname,
vstreet,
vcity,
vcountry,
vpcode)

Related

code to add data from a form to a table in access

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.

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.

VBA coding on access

I am trying to see where my issue lies with the following code.
'add data to table
CurrentDb.Execute "INSERT INTO IB Students(Student Name, Gender, IB) " & _
"VALUES (" & Me.Text6 & ",'" & Me.Combo13 & "','" & Me.Text9 & "')"
'refresh data in list on form
stdfrm.Form.Requery
Whenever I try to use the add command I get Runtime Error -3134 and it says there is a syntax error with INSERT INTO statement.
May be the name of table needs to be enclosed by brackets, and text values must be quoted:
'add data to table
sSQL = "INSERT INTO [IB Students] ([Student Name], Gender, IB) " & _
"VALUES ('" & Me.Text6 & "','" & Me.Combo13 & "','" & Me.Text9 & "')"
'print SQL for debug
Debug.print sSQL
'Run query
CurrentDb.Execute sSQL
'refresh data in list on form
stdfrm.Form.Requery
Note: if Me.Text6 (and others) is not bind to data source then you must use it as Me.Text6.Value
Edit: Maybe is a good idea check the length before run the query
When i open the immediate window it shows
INSERT INTO [IB Students] (Student Name, Gender, IB) VALUES (Micheal,'Male','2')
INSERT INTO [IB Students] (Student Name, Gender, IB) VALUES (John,'Male','1')
INSERT INTO [IB Students] ([Student Name], Gender, IB) VALUES (,'','')
INSERT INTO [IB Students] ([Student Name], Gender, IB) VALUES (,'','')

Classic ASP mysql INSERT getting result Operation is not allowed when the object is closed

I have this code:
Set oConnection = Server.CreateObject("ADODB.Connection")
Set oRecordset = Server.CreateObject("ADODB.Recordset")
oConnection.Open "DRIVER={MySQL ODBC 5.1 Driver};SERVER=localhost;UID=xxxx;PWD=xxxx;DATABASE=xxxx; OPTION=3;"
Sqltemp = "INSERT INTO rsvptable (fname, lname, fbid, rsvp, streetaddress, city, state, zip, streetaddress2, cellphone, acode) " & _
"VALUES ('" & firstName & "', '" & lastName & "', " & fb & ", 0, '" & address1 & "', '" & city & "', '" & strState & "', " & zip & ", '" & address2 & "', " & cell & ", " & returnedNums & ")"
set newAdd = oConnection.execute(Sqltemp)
if newAdd.EOF then
response.write "end"
else
response.write "not end"
end if
And it keeps telling me this:
ADODB.Recordset error '800a0e78'
Operation is not allowed when the object is closed.
/add.asp, line 136
line 136 is this:
if newAdd.EOF then
What would i be overlooking here?
UPDATE
found my answer here! :o)
How to tell if a db update was successful?
two things:
you define oRecordset as the recordset, but then use & check eof on newAdd
Why are you trying to populate a recordet from an insert query?
http://www.w3schools.com/ado/met_conn_execute.asp
The results are stored in a new Recordset object if it is a
row-returning query. A closed Recordset object will be returned if it
is not a row-returning query.
INSERT is not a row-returning query, thus it returns a closed recordset, thus you can't .EOF it. Check the Rows Affected bu passing a variable as the second argument to Execute. If RowsAffected is 1, then you're set.

DoCmd.RunSQL in Access not providing expected results

I've done this a zillion times, and I don't know what's changed.
Private Sub Foo()
dim rst as DAO.Recordset
set rst = CurrentDB.OpenRecordset("Select * From Table1;", dbOpenDynaset)
rst.movefirst
Do Until rst.EOF
DoCmd.RunSQL INSERT INTO Table2 (Last, First, Gender) VALUES (" & "' & rst!Last & '" & ", " & "' & rst!First & '" & ", " & "' & rst!Gender & '" & ");"
rst.MoveNext
Loop
End Sub
If I do this, the values inserted into Last, First and Gender are '" & rst!ColumnName & "' versus the actual data from the column.
If I omit the single quotes it works until it comes across a last name with a hypen or aposttrophe and then it throws a syntax error.
But I've done this same thing in code a zillion times the last 6 years and it's always worked and for whatever reason on this new project it's not proving the results I am used to.
Any ideas?
You seem to have that a bit mixed up:
DoCmd.RunSQL "INSERT INTO Table2 (Last, First, Gender) VALUES ('" & _
& rst!Last & "', '" & rst!First & "', '" & rst!Gender & "');"
Note that if you have names that contain apostrophes, the above will fail, so it is a good idea to escape them like so:
DoCmd.RunSQL "INSERT INTO Table2 (Last, First, Gender) VALUES ('" & _
& Replace(rst!Last,"'","''") & "', '" & Replace(rst!First,"'","''") _
& "', '" & rst!Gender & "');"
The other point that I wonder about is why are you doing this in steps? Why not:
strSQL = "INSERT INTO Table2 (Last, First, Gender) " _
& "SELECT Last, First, Gender FROM Table1 "
CurrentDB.Execute strSQL, dbFailOnError