Again, I'm a beginner and am not claiming this to be the most efficient or best way to do this. The following code will query for the data in table one and organize it as shown in table two. Just two or three loops.
Code:
Public Sub test()
On Error GoTo error_proc
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim x As Integer
Dim y As Integer
Dim csum As Double
Dim row_labels() As Double
Dim col_labels() As String
Dim data() As Double
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT investmentName, multiple, pct FROM tbl;")
'csum = 0# 'not necessary
If Not rs.BOF And Not rs.EOF Then
rs.MoveLast 'to get rowcount
rs.MoveFirst
ReDim row_labels(rs.RecordCount * 3 - 2)
ReDim col_labels(rs.RecordCount - 1)
ReDim data(rs.RecordCount * 3 - 2, rs.RecordCount - 1) 'data(rows, cols)
row_labels(0) = 0#
For y = 1 To UBound(row_labels) - 3
csum = csum + rs!pct
row_labels(y) = csum
row_labels(y + 1) = csum
row_labels(y + 2) = csum
rs.MoveNext
y = y + 2
Next y
row_labels(UBound(row_labels)) = csum + rs!pct
rs.MoveFirst
For x = 0 To UBound(data, 2)
'For y = 0 To UBound(data, 1) '<--- EDIT: not necessary
' data(y, x) = 0# '<--- is this necessary or do arrays ALWAYS init to zero??? idk...
'Next y
col_labels(x) = rs!investmentName
data(x * 3, x) = rs!multiple
data(x * 3 + 1, x) = rs!multiple
rs.MoveNext
Next x
'At this point we now have THREE arrays populated with the requested data
' col_labels() = the column headings
' row_labels() = the running sum of pct
' data(,) = the data organized in (rows, cols)
' DO WITH WHAT YOU WILL
End If
rs.Close
db.Close
exit_proc:
Set rs = Nothing
Set db = Nothing
Exit Sub
error_proc:
Debug.Print "Dude! Like, there was an error, bro."
Resume exit_proc
End Sub
edit:
according to my super extensive research vba numeric types will default initialize to zero, so that will make my sub a little bit simpler. Everything processed in just 2 loops.
https://stackoverflow.com/questions/...default-values