Top Banner
© 2009 VB Tutorial All Rights Reserved Contact Me: [email protected] Connect VB Application with MS Access through Coding Author D BESCHI ANTONY ([email protected])
19

VB Tutorial-Connect VB Application With MS Access Through Coding

Nov 12, 2014

Download

Documents

BeschiAntonyD
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: VB Tutorial-Connect VB Application With MS Access Through Coding

© 2009 VB Tutorial All Rights Reserved Contact Me: [email protected]

Connect VB Application with MS Access through Coding

Author D BESCHI ANTONY

([email protected])

Page 2: VB Tutorial-Connect VB Application With MS Access Through Coding

© 2009 VB Tutorial All Rights Reserved Contact Me: [email protected]

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

Page 3: VB Tutorial-Connect VB Application With MS Access Through Coding

© 2009 VB Tutorial All Rights Reserved Contact Me: [email protected]

2. Select Standard EXE template to create new project and click on open button

Page 4: VB Tutorial-Connect VB Application With MS Access Through Coding

© 2009 VB Tutorial All Rights Reserved Contact Me: [email protected]

3. VB IDE is opened

4. Right Click on the Toolbox and select components

Page 5: VB Tutorial-Connect VB Application With MS Access Through Coding

© 2009 VB Tutorial All Rights Reserved Contact Me: [email protected]

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

OK

The ADODC control is added to the Tool Box

Page 6: VB Tutorial-Connect VB Application With MS Access Through Coding

© 2009 VB Tutorial All Rights Reserved Contact Me: [email protected]

Page 7: VB Tutorial-Connect VB Application With MS Access Through Coding

© 2009 VB Tutorial All Rights Reserved Contact Me: [email protected]

6. Double click on Adodc control and draw it on your form

Page 8: VB Tutorial-Connect VB Application With MS Access Through Coding

© 2009 VB Tutorial All Rights Reserved Contact Me: [email protected]

7. Right Click on the ADOC control select properties to get Property pages Dialogue

Page 9: VB Tutorial-Connect VB Application With MS Access Through Coding

© 2009 VB Tutorial All Rights Reserved Contact Me: [email protected]

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

Page 10: VB Tutorial-Connect VB Application With MS Access Through Coding

© 2009 VB Tutorial All Rights Reserved Contact Me: [email protected]

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

Page 11: VB Tutorial-Connect VB Application With MS Access Through Coding

© 2009 VB Tutorial All Rights Reserved Contact Me: [email protected]

Page 12: VB Tutorial-Connect VB Application With MS Access Through Coding

© 2009 VB Tutorial All Rights Reserved Contact Me: [email protected]

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

Page 13: VB Tutorial-Connect VB Application With MS Access Through Coding

© 2009 VB Tutorial All Rights Reserved Contact Me: [email protected]

Creating Module

1. Right click on the project and select Add -> Module. Add Module Dialogue is

opened.

Page 14: VB Tutorial-Connect VB Application With MS Access Through Coding

© 2009 VB Tutorial All Rights Reserved Contact Me: [email protected]

Page 15: VB Tutorial-Connect VB Application With MS Access Through Coding

© 2009 VB Tutorial All Rights Reserved Contact Me: [email protected]

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

Page 16: VB Tutorial-Connect VB Application With MS Access Through Coding

© 2009 VB Tutorial All Rights Reserved Contact Me: [email protected]

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

Page 17: VB Tutorial-Connect VB Application With MS Access Through Coding

© 2009 VB Tutorial All Rights Reserved Contact Me: [email protected]

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.

Page 18: VB Tutorial-Connect VB Application With MS Access Through Coding

© 2009 VB Tutorial All Rights Reserved Contact Me: [email protected]

Page 19: VB Tutorial-Connect VB Application With MS Access Through Coding

© 2009 VB Tutorial All Rights Reserved Contact Me: [email protected]

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 recordsets

The End

(Your suggestions and corrections are welcomed…! You can reach me by mailing to me

to [email protected] or [email protected])