0% found this document useful (0 votes)
9 views

VB.beter Database Connection Using Own Code

The document provides instructions on how to connect to an Access database using the OLE DB connection object in a programming environment. It outlines the necessary coding steps, including setting up the connection string and handling the connection open and close process. The final code example demonstrates how to implement these steps within a button click event in a form.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

VB.beter Database Connection Using Own Code

The document provides instructions on how to connect to an Access database using the OLE DB connection object in a programming environment. It outlines the necessary coding steps, including setting up the connection string and handling the connection open and close process. The final code example demonstrates how to implement these steps within a button click event in a form.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

DATABASE

CONNECTION
USING OWN CODE
THE CONNECTION
OBJECT
• The connection object is what you need to
connect to a database.
• There are different connection objects and it
depends on the type of database you are
connecting to.
• Since we are connecting to an access
database we are going to use the OLE DB
connection object.
Continued…
• OLEDB means Object Linking and
Embedding Database.
• There are different OLEDB
objects, the one we will use is
called “Jet”
• Others are SQL Server and Oracle.
HOW TO CONNECT
• Open a new project
• Add a button
• Change the name property to
btnLoad
• go to code window and type the
following codes
CONNECTION STEPS
• Dim (name of db) As String
Private Sub………
• Dim con As New OleDb.OleDbConnection
• Dim dbProvider As String
• Dim dbSource As String
• Dim MyDocumentsFolder As String
• Dim TheDatabase As String
• Dim FullDatabasePath As String
CODING CONT…
• dbProvider = "Provider=Microsoft.ACE.OLEDB.12.0;"
• TheDatabase = “(Name of db).accdb"
• MyDocumentsFolder =
Environment.GetFolderPath(Environment.SpecialFolder.
MyDocuments)
• FullDatabasePath = MyDocumentsFolder &(name
of db)
• dbSource =
"PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=C:\
Users\happymusi\documents\3.2\Lodge1.accdb"
CODING CONT
• con.ConnectionString = dbProvider & dbSource
• con.Open()

• MsgBox("A Connection to the Database is


now open")
• con.Close()
• MsgBox("The Connection to the Database is
now Closed")
FINAL CODE
• Public Class Form1
• Dim Lodge1 As String

• Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click


• Dim con As New OleDb.OleDbConnection
• Dim dbProvider As String
• Dim dbSource As String
• Dim MyDocumentsFolder As String
• Dim TheDatabase As String
• Dim FullDatabasePath As String
• dbProvider = "Provider=Microsoft.ACE.OLEDB.12.0;"
• TheDatabase = "Lodge1.accdb"
• MyDocumentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
• FullDatabasePath = MyDocumentsFolder & Lodge1
• dbSource = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\happymusi\documents\3.2\Lodge1.accdb"

• con.ConnectionString = dbProvider & dbSource


• con.Open()

• MsgBox("A Connection to the Database is now open")


• con.Close()
• MsgBox("The Connection to the Database is now Closed")

• End Sub
• End Class

You might also like