Trouble with SQL Query SELECT Count...

ChrisBrook

New Member
Joined
Jan 15, 2016
Messages
12
Hi,

I've built a report in MS Excel which contains multiple tabs and extracts information from a MS Access database.

As part of this I'm trying to select a specific table and count the number of rows where a certain criteria is met before then moving on to do the same on another table.

In my spreadsheet I have a tab called 'Completed', on here I have a list of the table names that I'm refrencing in the query.

The code I'm currently using is:
Code:
Public Sub ImportCompleted(StartDate As Date, EndDate As Date)
Dim strTableName As String, sSQL As String, lngRow As Long

'This calls a seperate sub which contains the DB path & password etc.
OpenDataBase MIDataBasePath, MIDataBasePassword


sSQL = "SELECT * FROM tbl_Reports ORDER BY QueueType, Description"


RetrieveDataBaseData (sSQL)
If RecordSet.EOF Or RecordSet.BOF Then
    MsgBox ("No Records Found")
    End
End If


'Generates Header on Worksheet
With Sheets("Completed").Cells(1, 1)
    .Value = "QueueView Daily Figures Report for " & VBA.Format(StartDate, "dd/mm/yyyy")
    .Font.Bold = True
    .Font.Italic = True
    .Font.Size = 24
End With


'Generate Column Headers
Sheets("Completed").Cells(2, 2).Value = "Code"
Sheets("Completed").Cells(2, 3).Value = "Description"
Sheets("Completed").Cells(2, 4).Value = "Completed (-1 Day)"


'Add Queue Names
lngRow = 3
Do Until RecordSet.EOF
    strTableName = RecordSet.Fields("TableName").Value
    strQueueType = RecordSet.Fields("QueueType").Value
    Sheets("Completed").Cells(lngRow, 1).Value = RecordSet.Fields("TableName").Value
    Sheets("Completed").Cells(lngRow, 2).Value = RecordSet.Fields("Code").Value
    Sheets("Completed").Cells(lngRow, 3).Value = RecordSet.Fields("Description").Value


    RecordSet.MoveNext
    lngRow = lngRow + 1
Loop


'Add Completed Values
lngRow = 3


For i = 3 To Sheets("Completed").Range("A1").End(xlDown).Row
    strTableName = Sheets("Completed").Cells(lngRow, 1).Value
    
    sSQL = "SELECT Count(*) FROM (" & strTableName & ") WHERE (((" & strTableName & ".Completed_TimeStamp) >= #" & VBA.Format(StartDate, "mm/dd/yyyy") & "#) And (" & strTableName & ".Completed_TimeStamp) < #" & VBA.Format(EndDate + 1, "mm/dd/yyyy") & "#)"
    
    Sheets("Completed").Cells(2, 5).Value = RecordSet.Fields("CountOfRecord_ID").Value
    
Next i


RecordSet.Close
DataBaseConnection.Close


End Sub

The last sSQL presents me with the following error:
"Item cannot be found in the collection corresponding to the requested name or ordinal."

I've checked and double checked and each table is formatted the same with the following columns:
Record_ID
Checkout_User
Completed_User
Completed_TimeStamp
Instance_ID

Any ideas?

Thanks
Chris
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You don't appear to actually do anything with that SQL string that I can see.
 
Upvote 0
What a fool! Yeah I missed out the following line:
Code:
RetrieveDataBaseData (sSQL)

After adding this in and using the string I get the same error though...
 
Upvote 0
Do any of your table names have spaces in? What is the SQL that produces the first error?
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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