Click here to Skip to main content
15,905,683 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
can someone please guide me on how i can update certain records in my ms access database...
i have a table called contacts and the fields are:

contact id, first name, last name, phone number, address, e-mail etc

now on my vb.net form i have a button called update and when that is pressed i want the text enetered into the textbox to be update for that record.

for e.g i wish to update the address of contact id 1, how can i do this??

i have spent over 4 hours trying to achieve this but i keep getting errors..

please help!

thanks
Posted
Comments
Sergey Alexandrovich Kryukov 20-Feb-12 21:27pm    
How can we know where did you fail? How about a code sample, sharing your specific problems?
--SA
alom_93 21-Feb-12 14:58pm    
Private Sub search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles search.Click

dim con as new oledb.oledbconnection
dim da as new oledb.oledbdataadapter
dim fn as string
Dim c As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source = " & fn
dim ds as new dataset

con.connectionstring=(c)
con.open
da.fill(ds,"s")
con.close
inc=-1



Dim ca As New OleDb.OleDbCommandBuilder(da)

ds.Tables("s").Rows(inc).Item(1) = TextBox2.Text

ds.Tables("s").Rows(inc).Item(2) = TextBox3.Text

ds.Tables("s").Rows(inc).Item(3) = TextBox4.Text

ds.Tables("s").Rows(inc).Item(4) = TextBox5.Text

ds.Tables("s").Rows(inc).Item(5) = TextBox6.Text

ds.Tables("s").Rows(inc).Item(6) = TextBox7.Text

ds.Tables("s").Rows(inc).Item(7) = TextBox8.Text

ds.Tables("s").Rows(inc).Item(8) = TextBox9.Text



da.Update(ds, "s")
MsgBox("updated")
Varun Sareen 20-Feb-12 22:51pm    
Please share some sample code.
alom_93 21-Feb-12 11:41am    
Private Sub search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles search.Click

dim con as new oledb.oledbconnection
dim da as new oledb.oledbdataadapter
dim fn as string
Dim c As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source = " & fn
dim ds as new dataset
con.connectionstring=(c)
con.open
da.fill(ds,"s")
con.close
inc=-1
Dim ca As New OleDb.OleDbCommandBuilder(da)

ds.Tables("s").Rows(inc).Item(1) = TextBox2.Text

ds.Tables("s").Rows(inc).Item(2) = TextBox3.Text

ds.Tables("s").Rows(inc).Item(3) = TextBox4.Text

ds.Tables("s").Rows(inc).Item(4) = TextBox5.Text

ds.Tables("s").Rows(inc).Item(5) = TextBox6.Text

ds.Tables("s").Rows(inc).Item(6) = TextBox7.Text

ds.Tables("s").Rows(inc).Item(7) = TextBox8.Text

ds.Tables("s").Rows(inc).Item(8) = TextBox9.Text

da.Update(ds, "s")
MsgBox("updated")
Ariel Riyo 22-Feb-12 3:07am    
just update your question instead of posting it as a comment.

1 solution

first you should put this code on the upper part of the codes just before the class of the form that you are using
VB
Imports System.Data.OleDb 'this would be the declaration of OLEDB namespace, this might be the reason why you are having blue line for OleDBConnection and OleDbDataAdapter because the namespace that handles this codes is not declared


VB
Dim con as new OleDBConnection
Dim ds As New DataSet
Dim da As New OleDbDataAdapter
con.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" &  My.Application.Info.DirectoryPath & "\dbase.mdb" 'dbase.mdb is the database file that you are using

da = New OleDbDataAdapter("select * from contacts where [contact id] = '" & Textbox1.text & "'", reservationConn) 'get the record based on the textbox containing the ID of the person that you want to edit

da.Fill(ds, "Contacts")
inc=-1 

ds.Tables("s").Rows(inc).Item(1) = TextBox2.Text 
ds.Tables("s").Rows(inc).Item(2) = TextBox3.Text 
ds.Tables("s").Rows(inc).Item(3) = TextBox4.Text 
ds.Tables("s").Rows(inc).Item(4) = TextBox5.Text 
ds.Tables("s").Rows(inc).Item(5) = TextBox6.Text 
ds.Tables("s").Rows(inc).Item(6) = TextBox7.Text 
ds.Tables("s").Rows(inc).Item(7) = TextBox8.Text 
ds.Tables("s").Rows(inc).Item(8) = TextBox9.Text 
da.Update(ds, "s")

MsgBox("updated")


*Note
You can try this code. Hope it helps!
 
Share this answer
 
v2
Comments
alom_93 22-Feb-12 18:13pm    
i get blue squiggly lines around "OleDBconnection" "OleDbdataadapter" and "viewresds"

whats the problem and what is viewresds.

thanks
chuan0308 22-Feb-12 21:04pm    
i have edited the code you can try it out.. if you still having some problem tell me. i'll be checking this thread from time to time..
alom_93 24-Feb-12 17:49pm    
thanks but still get blue line on "reservationConn"
chuan0308 24-Feb-12 19:19pm    
sorry for that 1 you just have to replace it with con
alom_93 28-Feb-12 18:02pm    
thanks got a slight problem where it says
da.fill(ds,"contacts") i get error when i press the update button. It says "No value given for one or more required parameters."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900