Open a database instead of a workbook

teebruin

New Member
Joined
Mar 28, 2002
Messages
38
Hopefully this will be the last bit of help I'll need for this form. I know how to open a workbook, but how do you open an Access database? What do I use instead of Workbooks.open?

Thanks in advance for your help.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
This retrieves data from an access database budget.mdb, this DB has one table: "budget" and seven fields. This code retrieves data in the Item field containing the text "lease" and the division field which contains the text " N. America". The qualifying data is stored in a Recordset object, the data is then transferred to the worksheet.

Sub ADO_Demo()
' This demo requires a reference to
' the Microsoft ActiveX Data Objects 2.x Library

Dim DBFullName As String
Dim Cnct As String, Src As String
Dim Connection As ADODB.Connection
Dim Recordset As ADODB.Recordset
Dim Col As Integer

Cells.Clear
MsgBox "This demo retrieves the data for the records in which ITEM = LEASE and DIVISION = N. AMERICA."

' Database information
DBFullName = ThisWorkbook.Path & "budget.mdb"

' Open the connection
Set Connection = New ADODB.Connection
Cnct = "Provider=Microsoft.Jet.OLEDB.4.0; "
Cnct = Cnct & "Data Source=" & DBFullName & ";"
Connection.Open ConnectionString:=Cnct

' Create RecordSet
Set Recordset = New ADODB.Recordset
With Recordset
' Filter
Src = "SELECT * FROM Budget WHERE Item = 'Lease' "
Src = Src & "and Division = 'N. America'"
.Open Source:=Src, ActiveConnection:=Connection

' Write the field names
For Col = 0 To Recordset.Fields.Count - 1
Range("A1").Offset(0, Col).Value = Recordset.Fields(Col).Name
Next

' Write the recordset
Range("A1").Offset(1, 0).CopyFromRecordset Recordset
End With
Set Recordset = Nothing
Connection.Close
Set Connection = Nothing
End Sub
 
Upvote 0
Sorry, but I got a little lost. I only need to open an Access Database file, not necessarily open a form. I just want to have a link that let's the user open the database with a click.
 
Upvote 0
Here is the code that I have tried using, but it's not working for me and not real proficient in VBA:

Dim AccApp As New Access.Application
Dim strDB As String
strDB = "database filename"
AccApp.OpenCurrentDatabase (strDB)

Thanks for any help
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top