Question : VB 2010 - Connection to MS Access

We are trying to move some utility programs from VB6 to .NET - in VB6 doing SQL statements with Access was simple - but I do not see an easy way to open the database now - I am looking for sample code that will make this easy for inhouse ulitities that need to be put together quickly. This was all very easy in VB6

Answer : VB 2010 - Connection to MS Access

http://www.carlprothman.net/Default.aspx?tabid=90#ODBCDriverForAccess
oConn.Open "Driver={Microsoft Access Driver (*.mdb)};" & _
           "Dbq=c:\somepath\mydb.mdb;"

Or

http://www.eggheadcafe.com/community/aspnet/14/63462/how-to-connect-vb-with-ms.aspx
Dim MyDatabase As OleDb.OleDbConnection
Dim ConnectToMyDatabase As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
     "Data Source="
MyDatabase = New OleDb.OleDbConnection(ConnectToMyDatabase)
MyDatabase.Open()


http://www.kdkeys.net/forums/thread/868.aspx
To connect vb6.0 and MS access, I will make an example of connecting them using ADODB.

Dim sConn as string 'for the connection string

'now we declare the ADODB connection

'to be able to show this NEW ADODB.Connection line, you must do the following:

1. in the toolbox, you must add a component.
2. in the component box, find the Microsoft ActiveX ADO connection something like that.
3. then when this is done, drag the component into your form and then delete it
4. this way you may be able to have the NEW ADODB line in your intellisense

now lets proceed to connecting you db and vb6.0

Dim db as New ADODB.Connection
dim rs as new ADODB.Recordset
dim sPath as string ' here we put the path of your database
dim sSQL as string 'your SQL Statement

'like for example your vb application is on C:\MyVB
' and within that your DB is under another folder called DB
' we must get the path C:\MyVB\DB\yourdbname.mdb
' we shall be using app.path for getting the path of your application that is the C:\MyVB
' then we must concatenate the folder and the name of your database.

sPath = app.path & "\DB\yourdbname.mdb;"

'initialize the connection string

sConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sPath

sSQL = "SELECT * FROM yourtablename"
...


;-)

JeffCoachman
Random Solutions  
 
programming4us programming4us