code for pivoting
Dim adoc1 As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub Command1_Click()
Dim cp1 As String
Dim txt As String
Dim countx As Integer
Dim county As Long
Dim xcord As Integer
Dim ycord As Long
Dim locidmatrix(500) As Integer
Dim prodidmatrix(10000) As Long ' space is only limited to virtual ram >=array size * 4bytes
Dim locprodmatrix(500, 10000) As Long
' change with necessary access connection string..
cp1 = "Provider=sqloledb;uid=sa;pwd=yasak;server=IULIA-PC;database=testdb;"
Set adoc1 = New ADODB.Connection
adoc1.CommandTimeout = 2000
adoc1.ConnectionTimeout = 10
adoc1.Open cp1
grid1.ColWidth(0) = 2000 ' product names column with in twips scale
'now prepare x axis for grid or table
txt = "select slid,locationDescription from " ' check the double quotes there must be one extra space on the right side
txt = txt & " (select LocationID,LocationDescription from Location) as loc, "
txt = txt & " (select distinct s.slid from "
txt = txt & " (select pid=ProductID,ProductDescription from Products) as p left outer join "
txt = txt & " (select spid=ProductID,slid=LocationID,NumberInLocation from Stock) as s "
txt = txt & " on p.pid=s.spid) as DisLocID "
txt = txt & " where loc.LocationID=DisLocID.slid order by LocationDescription"
countx = 0 ' x coordinate items count initialized
grid1.Cols = 1 ' arrange grid columns accordingly
Set rs = New ADODB.Recordset
rs.Open txt, adoc1
While rs.EOF = False
countx = countx + 1
grid1.Cols = grid1.Cols + 1
grid1.TextMatrix(0, countx) = Trim(rs!LocationDescription)
locidmatrix(countx) = rs!slid 'id s will be necessary as we use a different order then id
rs.MoveNext
Wend
'now prepare y axis for grid or table
txt = "select ProductID,ProductDescription from "
txt = txt & " (select ProductID,ProductDescription from Products) as p, "
txt = txt & " (select distinct ProdID=ProductID from Stock) as s "
txt = txt & " where p.ProductID=s.ProdID order by ProductDescription "
county = 0 ' y coordinate items count initialized
grid1.Rows = 1 ' arrange grid rows accordingly
Set rs = New ADODB.Recordset
rs.Open txt, adoc1
While rs.EOF = False
county = county + 1
grid1.Rows = grid1.Rows + 1
grid1.TextMatrix(county, 0) = Trim(rs!ProductDescription)
prodidmatrix(county) = rs!ProductID
rs.MoveNext
Wend
'prepare actual data table
txt = "select p.pid,s.slid,s.NumberInlocation from "
txt = txt & "(select pid=ProductID,ProductDescription from Products) as p left outer join "
txt = txt & "(select spid=ProductID,slid=LocationID,NumberInLocation from Stock) as s "
txt = txt & " on p.pid=s.spid order by p.pid,s.slid"
Set rs = New ADODB.Recordset
rs.Open txt, adoc1
While rs.EOF = False
locprodmatrix(rs!slid, rs!pid) = rs!NumberInlocation ' location and product ids are used as indices
rs.MoveNext
Wend
' now pivot the table with the help of matrix and loop
For ycord = 1 To county
For xcord = 1 To countx
grid1.TextMatrix(ycord, xcord) = locprodmatrix(locidmatrix(xcord), prodidmatrix(ycord))
Next xcord
Next ycord
End Sub