hi everyone,

im very new to vb.net.

i have a textbox and a save button on my form.

i have already connected the database to my application...

pls can any1 tell m how do i save the data in the textbox to my ms access databse when i click on btnsave...

can sum1 pls guide m by giving me the code

Here's a sample

Dim oCon As OdbcConnection
Dim oCmd As OdbcCommand
Dim ConnStr As String
Dim strSQL As String

ConnStr = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=<path to mdb file>"
oCon = New OdbcConnection(ConnStr)
oCon.Open()
oCmd = oCon.CreateCommand()
strSQL = "INSERT INTO <tablename> (<fieldname>) VALUES ('" & TextBox1.Text.Replace("'", "''") & "')"
oCmd.CommandText = strSQL
oCmd.ExecuteNonQuery()
oCon.Close()

Replace correct strings to connection string, table name and field name, and add some error handling code.

If you have trouble with connection string see Connection strings for Access.

hi, thx vey much for the help... as i tld you im very new to vb.net 05...

i have pasted the code in my click method...

well i knw that i have to edit it frst :)

i have attached a preview of how my code is...

but why is "OdbcConnection" highlighted saying it is not declared. pls check the coding and guide me pls....

write this on the top of codes

Imports System.Data.Odbc
commented: jaauh +2

hi,

im still having trouble to save data to access..

i have attached my code in JPG format...

My little app is supposed to save data from a textbox to a field in access...

i dont get any errors while running the application.. but it just wont save the data.. cantr understand why...

Pls help... i really need to understand this..

Thank You...

Hi, i managed to make it work.. now my another prob is, how do i amend the code is i had 2 textbox and to save the data into ms access...

i tried this but its not working:

mycommand.CommandText = "INSERT INTO TBLSuppliers (Company, Contact Person) VALUES ('" & txtCompany.Text.Replace , & txtContactPerson.text.replace ("'", "''") & "')"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.