You are on page 1of 19

Connect VB Application with MS Access through Coding

Author
D BESCHI ANTONY
(beschi.agent@gmail.com)
Introduction
This tutorial helps to connect the visual basic applications with the MS-Access database
using coding. Once the connection is made with the database in this way that can be
used within the application scope. This tutorial is made with the visual representations of
each step to make connection.

There are three sections in this tutorial


1. Getting Connection String
2. Creating Module
3. Creating an Example
We shall start….

Getting Connection String


1. Open Microsoft Visual Basic 6.0
Start -> All Programs -> Microsoft Visual Studio 6.0 -> Microsoft Visual
Basic 6.0
2. Select Standard EXE template to create new project and click on open button
3. VB IDE is opened

4. Right Click on the Toolbox and select components


5. Select Microsoft ADO Data Control 6.0 (OLEDB) and then select Apply and
OK

The ADODC control is added to the Tool Box


6. Double click on Adodc control and draw it on your for
7. Right Click on the ADOC control select properties to get Property pages Dialogue
8. Select the option Use Connection String option and click on Build Button. Now
Data Link Properties Dialogue will be shown. Select the Provider Tab
9. Select Microsoft Jet 4.0 OLE DB Provider or Microsoft Jet 3.5.1 OLE DB
Provider(best). And click on Next button. It goes to the Connection Tab of the
Data link properties window. Select the particular MS Database that you want to
connect by cliking on the button which is near to the database name fied and
Test the connection by clicking on the Test Connection Button. Then click on
OK button
10. Finally you have got the connection String Copy that String to the Module code

Congrats…! You have got connection String… Your connection String is

“Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=E:\workspace\Learning\dbfolder \test.mdb;Persist Security Info=False “

Now we shall move on to the second part of the tutorial


Creating Module
1. Right click on the project and select Add -> Module. Add Module Dialogue
is opened.
2. Select New Tab -> Module and click on Open button. The Module window is opened
Where your going to include your connection string in coding
3. Start Enter your Coding
//Declaring a ADODB Connection. Globlal to specify the global scope (whole application)
Global CnnStr As ADODB.Connection

Sub Main()
// Defining the connection variable
Set CnnStr = New ADODB.Connection
CnnStr.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=E:\workspace\Learning\dbfolder\test.mdb;Persist Security
Info=False"

//To open the Main form. In this case splash screen


frmSplash.Show
End Sub

Note : In case, f your application is going to be shifted to the different computer, the
path in the connection string will not refer to the database so you need you use relative
path instead of absolute path. The connection string will look like this
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & App.Path &
"\dbfolder\test.mdb;Persist Security Info=False"

In case if you use password protected database. The connection string will be
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & App.Path &
"\dbfolder\test.mdb;Persist Security Info=True;Jet OLEDB:Database
Password=yourpassword"

// To Retrive the records from database


Public Sub OpenRecordSet(ByRef rsSend As ADODB.Recordset, Optional sSQL As
String)
Set rsSend = New ADODB.Recordset
rsSend.ActiveConnection = CnnStr
rsSend.CursorLocation = adUseClient
rsSend.CursorType = adOpenKeyset
rsSend.LockType = adLockPessimistic

If sSQL <> "" Then


rsSend.Open sSQL
End If
End Sub
After Entering the code the Module window will be

4. Finally you have to set up the startup function for your application that can be
achieved by Right clicking on the project and select the your application name
properties. And select the module main sub routine as your startup function

With these settings you have reached your goal. Hence forth you can easily
communicate with your backend storage (MS-Access) without any difficulties.
Creating an Example
This example shows just the methods or the way in which the database connection can
be used.
To Retrive records from database
Public Function isValidUser(sUserName As String, sPassword As String)
As Boolean

Dim rsUser As New ADODB.RecordSet //Declare the recordset


Dim sSQL As String //Declare the string for query variable
Dim bReturnValue As Boolean
sSQL = "SELECT * FROM tbl_Login WHERE user_name ='" & sUserName &
"' AND user_password = '" & sPassword & "'"
OpenRecordSet rsUser, sSQL //Calling the function which you have
//declared in the module
If rsUser.RecordCount > 0 Then
bReturnValue = True
Else
bReturnValue = False
End If
isValidUser = bReturnValue
End Function

To Delete, Edit or Update records


Dim sSQL As String
sSQL =”your sql statement”
CnnStr.Execute (sSQL)

Further reading is needed to acquire the knowledge about the cursors and record sets
Your suggestions and corrections are welcomed…! You can reach me by mailing to me
to beschi.agent@gmail.com

You might also like