Connecting vb6.0 and MS Access

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”

now let us proceed to the main part

Private Sub frmMain_Load()
db.ConnectionString = sConn
db.Open

with rs
.activeconnection = db
.locktype = adlockoptimistic
.cursortype = adopenkeyset
.open sSQL

‘ now let us loop through your records and show it to your label
‘ for example your table has two fields, the field called name and description

do while not .eof
label1.caption = .fields(“name”).value & vbCrLf & .fields(“description”).value & vbCrLf

.movenext
loop

‘let close the rs connection
rs.close
end with
set rs = nothing
db.close
set db = nothing
End Sub

Source: http://www.kdkeys.net/forums/thread/868.aspx

1 Comment

  1. Warren said,

    great code, it helps me alot..

Leave a comment