Access 2007-2019
Access 2000-2003
Access Seminars
TechHelp Support
Tips & Tricks
Access Forum
Course Index
Topic Glossary
Insider Circle
 
Home   News   Tips   Glossary   Templates   Forums   Help   Logon   Order   Contact Us  
 
Tips & Tricks

New Tips Added Weekly!
Click here to get on our Mailing List

   
 
VB.NET

Connect to Access DB

How to connect to an Access Database.

Create a blank Form and add a DataGridView on to it.

Import the Oledb information.

	'Imports System.Data
	Imports System.Data.OleDb
	

Now in your code, create some variable to store the information you will be using later.

	Dim dbProvider As String
	Dim dbSource As String
	 
	Dim dbPathAndFilename As String
	Dim con As New OleDb.OleDbConnection
	Dim ds As New DataSet
	Dim da As OleDb.OleDbDataAdapter
	Dim sql As String
	

Next in the Load event set a few.

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;" ' MDB
		'dbProvider = "PROVIDER=Microsoft.Ace.OLEDB.12.0;" 'ACCDB
		dbSource = "Data Source="
	 
		LoadData()
	End Sub
	

Load the Data.

	Sub LoadData()
		'Connect to db
		'You could store the db path in the Settings of the App.
		'dbPathAndFilename = My.Settings.dbPath
		dbPathAndFilename = "C:\Users\%USER%\Documents\db.mdb"
		conAn.ConnectionString = dbProvider & dbSource & dbPathAndFilename
		 
		conAn.Open()
		sql = "SELECT * FROM table;"
		da = New OleDb.OleDbDataAdapter(sql, con)
		'Give it a name.
		da.Fill(ds, "TABLE")
	 
		con.Close()
		 
		'Populate the DataGridView
		DataGridView1.DataSource = ds.Tables("TABLE")
	End Sub
	

 

Alex Hedley (Avatar) By: Alex Hedley


Click here to sign up for more FREE tips

 

You may want to read these articles from the 599CD News:

 

Learn
 
Accessindex
Excelindex
Wordindex
Windowsindex
PowerPointindex
Photoshopindex
Visual Basicindex
ASPindex
Seminars
More...
Customers
 
Account Login
Online Theater
Downloads
Lost Password
Free Upgrades
Insider Circle
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Articles
Search Our Site
Waiting List
Production Schedule
Collapse Menus
Help
 
Live Chat
Customer Support
WalkThru Tutorials
Troubleshooting
FAQs
TechHelp
Consulting Services
About Us
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
Order
 
Video Tutorials
Handbooks
MYOLP Memberships
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Gift CDs
Contact
 
Live Chat
General Info
Support Policy
Contact Form
Email Us
Mailing Address
Phone Number
Fax Number
Course Survey
Facebook    Twitter

Google Plus    LinkedIn

Blog RSS Feed    YouTube Channel
Richard Rost Microsoft MVP