james_axton
asked on
Using ADO, loop through an Access database noting Table Names and Record Counts
I can do this using DAO but not ADO, and I prefer doing it with ADO. I want to go through an existing Access database, grab each table name and a count of its records, and then export that data to an Excel sheet.
I've included the incomplete code that I'm using. I'm having trouble with:
1) Actually looping through each table, the code there is very incomplete
2) Properly writing the data to Excel using ADO
3) I'm not sure I've even created the Excel sheet correctly using ADO
Any feedback, help, and/or corrections would be greatly appreciated.
I've included the incomplete code that I'm using. I'm having trouble with:
1) Actually looping through each table, the code there is very incomplete
2) Properly writing the data to Excel using ADO
3) I'm not sure I've even created the Excel sheet correctly using ADO
Any feedback, help, and/or corrections would be greatly appreciated.
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sSQL As String
Dim iTable as Integer
Excel Variables
Dim xlApp As Object
Dim xlWb As Object
Dim xlWs As Object
Create an instance of Excel and add a workbook
Set xlApp = CreateObject("Excel.Application")
Set xlWb = xlApp.Workbooks.Add
Set xlWs = xlWb.Worksheets("Sheet1")
Display Excel and give the user control of Excels lifetime
xlApp.Visible = True
xlApp.UserControl = True
Set cnn = CurrentProject.Connection
Set rst = New ADODB.Recordset
iTable = 1
For Each Table in the Database that doest start with "~" or "MSYS"
sSQL = "SELECT Count(*) As TotalRecords FROM " & DatabaseTable
rst.Open sSQL, cnn, adOpenStatic, adLockOptimistic
xlWs.Cells(iTable, 1).Value = the Table Name (in Column A)
xlWs.Cells(iTable, 2).Value = the the Number of Records (rst!TotalRecords, in Column B)
iTable = iTable + 1
Next Table
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
Set xlWs = Nothing
Set xlWb = Nothing
Set xlApp = Nothing
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I also failed to mention that I changed the GetTablesADO Function type from String to Variant.
Sorry for my late reply, what problem do you still have ?
ASKER
The same problems that were previously mentioned :) I haven't done anything with them yet.
can you post the code of your getTables and the place you call it !
dim tables as variant
tables = GetTablesADO("YourDatabase ")
dim tables as variant
tables = GetTablesADO("YourDatabase
ASKER
I left everything intact except for the changes mentioned above.
Where do you get an error and what is it telling ?
ASKER
Dhaest, if I did something wrong with respect to replying then I apologize. Did I not provide enough information in my previous posts?
Can you post the adjusted code .
The code where you call teh getTables-function and the getTables-function !
And also a description of the error you get !
The code where you call teh getTables-function and the getTables-function !
And also a description of the error you get !
ASKER
I ended up re-writing everything to use OpenSchema(adSchemaTables) to grab my table names and then running a "SELECT Count(*)" against each table - in the same vein as my original code but with correct nomenclature.
To verify that my Excel code was correct I just did a few web searches.
Thank you for your help.
To verify that my Excel code was correct I just did a few web searches.
Thank you for your help.
ASKER
At line 45 in the included Function it halts with an error 91 "Object Variable or With Variable not set". It highlights this portion of the line:
If RS.State = adStateOpen Then
I was able to troubleshoot my way past other small issues but I can't get this one. Any ideas? I've attached the changes to the function that I made. I also changed the tables variable from String to a Variant to accommodate the array.
Open in new window