donwiss
Board Regular
- Joined
- Jul 5, 2020
- Messages
- 63
- Platform
- Windows
I have this macro, which produces exactly the result that I want. But I would think this could be retrieved in one SQL string.
VBA Code:
Private Sub bnDatabaseNames_Click()
' creates global data variable
Dim i As Integer
Dim temp As Variant
If OpenDB Then Exit Sub
' get list of unique names
sql = "SELECT DISTINCT FldrName,Name FROM SecurityPrices ORDER BY FldrName,Name;"
Set rs = db.OpenRecordset(sql)
If Not rs.EOF Then
data = FlipData(rs.GetRows(10000))
ReDim Preserve data(UBound(data), 4)
' loop for our unique rows
For i = 0 To UBound(data)
sql = "SELECT FIRST(SettleDate),LAST(SettleDate),Count(*) FROM SecurityPrices WHERE FldrName='" & Replace(data(i, 0), "'", "''") & "' AND Name='" & Replace(data(i, 1), "'", "''") & "';"
Set rs = db.OpenRecordset(sql)
temp = rs.GetRows(10000)
data(i, 2) = temp(0, 0)
data(i, 3) = temp(1, 0)
data(i, 4) = temp(2, 0)
Next i
End If
CloseDB
End Sub