Connecting to an Access 98/2003 Database in vb6

This code shows how to connect to a Access 98 or Access 2003 database in visual basic.Before using this code you should add a reference to Microsoft Activex data object 2.x library (2.x means 2.5 or 2.6 or something like that)

Dim db_file As String
Dim statement As String
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset

 
‘Set the database Path
db_file = App.Path
If Right$(db_file, 1) <> “” Then db_file = db_file & “”
db_file = db_file & “DataFile.mdb”
 
‘Open a connection.
Set conn = New ADODB.Connection
conn.ConnectionString = _
    “Provider=Microsoft.Jet.OLEDB.4.0;” & _
    “Data Source=” & db_file & “;” & _
    “Persist Security Info=False”
conn.Open
Explanation:
Here db_file is the database path. You can change “DataFile.mdb” to your database file name.
Usage:
  • Create a table

statement=”create table table_name(fieldname1 char(10),fieldname2 integer,…)”

set rs=conn.execute(statement)

  • Get the recordset

statement=”select * from table_name”

set rs=conn.execute(statement)

 

  • Print a data in to a message box

msgbox rs(“field_name”)

 

  • Checking for null in a record item

if isnull(rs(“field_name”) )=true then msgbox “record item is null”

Leave a Reply

Your email address will not be published. Required fields are marked *