Access database using code

Im trying!!

New member
Joined
Mar 11, 2007
Messages
3
Programming Experience
Beginner
Hi

Im using VB.NET and trying to connect to a Access database using code does anyone know how to do this?
 
Last edited:
Ok Thankyou for the links i have managed to finally get the data to show up from my access table into my windows application datagridview the code is: (i have used a button to display the data but the code can be placed on form load as well.)

Imports System.Data.OleDb
Imports System.Data

'then write this as private: (change highlighted text to your location and table name)

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim connString As String = "Provider=Microsoft.Jet.OL

EDB.4.0;Data Source=C:\\db1.mdb"
Dim myConnection As OleDbConnection = New OleDbConnection
myConnection.ConnectionStr

ing = connString
' create a data adapter
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from st_timetable", myConnection)

Dim ds As DataSet = New DataSet
da.Fill(ds, "st_timetable")
DataGridView1.DataSource = ds.DefaultViewManager
End Sub
 
Read the DW2 link in my signature


I've read this till I'm blue in the face. I'm using OLEDB , trying to figure out where to place the Parameters I've created for my app. then call an update.

You're examples for DW are ADO which assumes you are binding a fixed location DB to your app. I am not. I allow my program to alter the location of where the DB is stored. I can read from it just fine but trying to update/edit the DB is becoming a real pain. I see the logic in using the Parameters but I am unable to get them to work.

I tried placing them in my module with the SQL statement but I don't know how to declare them.

I've tried placing them in my main as a sub I call once when the Main form first loads but then it says my Parameter names are undeclared. If I declare them as say Dim LastName1 as String, then there is no reason to do the ac.Parameters.Add(Lastname1, OleDbType.VarChar)

can you give me just a small example of how I might edit a row inside a dataset using your Parameters and assume I'm using OleDb?
 
Hi

Im using VB.NET and trying to connect to a Access database using code does anyone know how to do this?

Read the DW2 link in my signature, section on Creating A Simple Data APplication - once you have that working, I can tell you how to vary the location.. Not that I think this is a great idea, but it's doable
 
can you give me just a small example of how I might edit a row inside a dataset using your Parameters and assume I'm using OleDb?

Ps.. parameters have nothing to do with editing the contents of a dataset!
 
Ps.. parameters have nothing to do with editing the contents of a dataset!

ok I never said they did I need to edit some data in my database and I was wanting to setup my Parameters in advance so I can use the them in my edit routine.
 
Nevermind I'll continue writing my old code at least it's still backward compatable and it works.
 
I dont know what else to tell you; I can lead a donkey to water, but I cant make it drink..

Forum member Arg81 can tell you how to make a dataset connect to varying databases, not just the one specified at dataset-design time.. ;)
 
I allow my program to alter the location of where the DB is stored. I can read from it just fine but trying to update/edit the DB is becoming a real pain. I see the logic in using the Parameters but I am unable to get them to work.

Troy,

I don't understand why if you can read from it, you can't edit or save anything. can you post the error message you get?

I've altered my app, much to the help from Cjard, so that my application can connect to 3 different databases (1 for each location that uses the app) at startup.
I'm using ADO and I can SELECT, INSERT and UPDATE just fine, as it's the connection string I change, and this is stored throughout my project.

What's confused me on your post is the use of "parameters" and not knowing where to put them....
Excuse my ignorance, but exactly what parameters are you talking about??

Let us know (post exactly what you are trying to do and the errors you get) and someone should be able to help you.
 
Back
Top