Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inserting data through a textbox to an access database

Status
Not open for further replies.

neroe

Technical User
Feb 27, 2003
54
GB
i have a form with a textbox on it and a button. the form has a test connection success to an access database consequently i have been through the motions of adding an oledbdataadapter etc. could anyone provide me with the correct code so that onclick of the button the data is (simply)written back to the database. i am more than willing to email the project to some kind soul as this is driving me nuts!!
thanks in advance . . .
 
dim strsql as string
dim myCOmmand as new oledb.oledbcommand


strsql = "insert into db values ('" & textbox.text & "')"

myConn.open

myCommand = new oledb.oledbcommand(strSQL)
mycommand.connection() = myConn
mycommand.executenonquery()

myconn.close


that should do it. did this help?

hayt



 
hayt he wants to write back to his db and probably he used the wizard to make his connection and adpters and so on.

so this should work aswell.

Code:
me.bindingcontext(datasetname).endcurrentedit
dataadaptername.update(datasetname)

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
hayt
thanks for your very prompt reply . . . i've included the code but i am obviously screwing it up somewhere.
would you mind if i mailed it to you? i have put the database in the bin folder so i beleive that should the correct code be inserted then it should run on your system as intended
thanks
 
i should've asked if you made your connection to your db in code or using the wizard. you can just post your code here. that way someone smarter than me (like crissie1) can answer you as well. don't worry if it is a lot of code.

hayt
 
yup, ok. i used the wizard to create the connection.
the code currently looks like this . . .

Private Sub btnRegister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegister.Click
Dim objTextBox As TextBox
objTextBox = txtFirstname

Dim drRows As DataRowCollection
Dim objNewRow As DataRow

drRows = objTextBox.Text

'Message Box fires 'Thankyou for registering'
MessageBox.Show("onClick this button will commit details to database and thank the user for registering")
End Sub

i am basing the code on an example where by there is a grid on the page with text boxes used to input the data. from there if insert is clicked then the data in the text boxes is written to the grid and consequently to the database. in my project i want to take the grid out. the code in the example looks like this . . .

'//This is the add student button event. When this button is clicked a new student is added to the
'//dataset.
Private Sub btnAddStudent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddStudent.Click
'add a new record to the database from the textboxes
'declare an object called objTable as a DataTable object
Dim objTable As DataTable
'connect it to the table "tblMarks" in the dataset DsMarks1
objTable = DataSet11.Tables("tblMarks")

'declare drRows as a collection of rows
Dim drRows As DataRowCollection
'declare an object "objNewRow" as an individual row object
Dim objNewRow As DataRow
'connect drRows to the rows in the table
drRows = objTable.Rows

'add values to objNewRow
objNewRow = objTable.NewRow()
objNewRow("Name") = txtName.Text
objNewRow("Module1") = txtModule1.Text
objNewRow("Module2") = txtModule2.Text
objNewRow("Module3") = txtModule3.Text

' add new row to dataset
drRows.Add(objNewRow)
End Sub
 
ok. i think i understand now. i haven't done connections with wizrds, but lemme try. if i am wrong, someone here will correct me.


here is what (i think) you need to do.
forget about the datarows and collections.


you can just do this:

Private sub (formLoadevent)

txtFirstname.databindings.add("Text", datasetname, "Table.Field")

end sub

Private Sub btnRegister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegister.Click

me.bindingcontext(datasetname).endcurrentedit
dataadaptername.update(datasetname)

End Sub


i think that should do it. try it and let me know if it works. i can't help but think that i am missing something, though. i'll keep looking for you.


hayt

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top