Display a simple Access Database onClick
Hey everyone. I just started my new job today and I desperately need some help. My boss wants a VBA function to display an Access database. Right now, I'm starting with a simple 3 line database.
I read through the "How to get the best help possible quickly" thread. I did a search and didn't see anything over this topic. I also, am using Excel 2007.
Here is what I have:
Code:
Sub Show()
'C:\Users\zachk\Desktop\Test.accdb
Dim cn As Object
Dim rs As Object
Dim strSql As String
Dim strConnection As String
Set cn = CreateObject("ADODB.Connection")
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _ "Data Source=C:\Users\zachk\Desktop\Test.accdb"
strSql = "SELECT ID FROM Test;"
cn.Open strConnection
Set rs = cn.Execute(strSql)
MsgBox rs.Fields(0) & " rows in MyTable"
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
It works. Connects correctly. When I click the button, it runs show correctly. But is just alerting "3 rows in MyTable". I need it to display all the rows. I'm new to VBA but know other programming languages pretty well. Please help me out here, I've been looking for hours.