Connecting to Access using macro

  • Thread starter Thread starter George
  • Start date Start date
G

George

Hi there, I am using this code to connect to the access
database but its not opening the tables I want to open:
Sub Auto_Open()
Dim cn As ADODB.Connection
ans = InputBox("What Table you want to analyze?")
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=C:\FolderName\DataBaseName.mdb;"
' open a recordset
End Sub
Pls if you have any information that I can use to fix this
problem let me know
 
Your code merely creates a connection to a database, it doesn't open any tables.

Replace your line:

' open a recordset

With these lines:

Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
With rs
.Source = "SELECT * FROM " & ans
.ActiveConnection = cn
.Open
End With
' Do something with recordset
rs.Close
cn.Close
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top