am getting a undefined error on the 'EntryTable' but i didnt think that i had to define the table names.
Option Explicit
Public RowsCount As Integer
Public ColsCount As Integer
Public gColNameArr() As Variant
Public gRowNameArr() As Variant
Public gItemNumsArr() As Integer
Public gWhereStrArr() As String
Public fields As Collection
Public Sub Run()
Initialize
Import
End Sub
Public Sub Initialize()
Dim sht As Worksheet
Dim i As Integer
Dim j As Integer
Dim str As String
Dim where_str As String
ColsCount = 88
RowsCount = 42
ReDim gColNameArr(1 To ColsCount)
ReDim gRowNameArr(1 To RowsCount)
ReDim gItemNumsArr(1 To RowsCount, 1 To 7)
ReDim gWhereStrArr(1 To RowsCount)
'set columns array
Set sht = Worksheets("detail"
With sht
For i = 1 To 88
gColNameArr(i) = .Cells(5, i + 1)
Next i
For i = 1 To 42
gRowNameArr(i) = .Cells(i + 8, 1)
Next i
End With
Set sht = Nothing
Set sht = Worksheets("data"
With sht
For i = 1 To RowsCount
where_str = "WHERE ("
For j = 1 To 7
str = .Cells(i, j + 1)
If str = "" Then
gItemNumsArr(i, j) = 0
Else
gItemNumsArr(i, j) = CInt(str)
where_str = where_str & "EntryTable.[Item Number] =" & str & " OR "
End If
Next j
gWhereStrArr(i) = Left$(where_str, Len(where_str) - 4) & " "
Next i
End With
End Sub
Public Sub Import()
Dim sht As Worksheet
Dim conn_str As String
Dim sql_str As String
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim i As Integer
Dim j As Integer
conn_str = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Documents and Settings\rhinson\My Documents\reporting.mde"
Set cn = New Connection
cn.Open conn_str
Set sht = Worksheets("Detail"
For i = 1 To RowsCount
For j = 1 To ColsCount
sql_str = "SELECT EntryTable(amount) as m " & _
"FROM EntryTable INNER JOIN HeaderTable ON EntryTable.[Reference #] = HeaderTable.[REference #] " & _
gWhereStrArr(i) & "And ItemTable.[Item Number] = " & gColNameArr(j) & " "
Set rs = New Recordset
Debug.Print sql_str
rs.Open sql_str, cn, adOpenStatic, adLockReadOnly
sht.Cells(i + 7, j + 1) = rs.fields("m"
rs.Close
Set rs = Nothing
Next j
Next i
cn.Close
Set cn = Nothing
End Sub
Option Explicit
Public RowsCount As Integer
Public ColsCount As Integer
Public gColNameArr() As Variant
Public gRowNameArr() As Variant
Public gItemNumsArr() As Integer
Public gWhereStrArr() As String
Public fields As Collection
Public Sub Run()
Initialize
Import
End Sub
Public Sub Initialize()
Dim sht As Worksheet
Dim i As Integer
Dim j As Integer
Dim str As String
Dim where_str As String
ColsCount = 88
RowsCount = 42
ReDim gColNameArr(1 To ColsCount)
ReDim gRowNameArr(1 To RowsCount)
ReDim gItemNumsArr(1 To RowsCount, 1 To 7)
ReDim gWhereStrArr(1 To RowsCount)
'set columns array
Set sht = Worksheets("detail"
With sht
For i = 1 To 88
gColNameArr(i) = .Cells(5, i + 1)
Next i
For i = 1 To 42
gRowNameArr(i) = .Cells(i + 8, 1)
Next i
End With
Set sht = Nothing
Set sht = Worksheets("data"
With sht
For i = 1 To RowsCount
where_str = "WHERE ("
For j = 1 To 7
str = .Cells(i, j + 1)
If str = "" Then
gItemNumsArr(i, j) = 0
Else
gItemNumsArr(i, j) = CInt(str)
where_str = where_str & "EntryTable.[Item Number] =" & str & " OR "
End If
Next j
gWhereStrArr(i) = Left$(where_str, Len(where_str) - 4) & " "
Next i
End With
End Sub
Public Sub Import()
Dim sht As Worksheet
Dim conn_str As String
Dim sql_str As String
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim i As Integer
Dim j As Integer
conn_str = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Documents and Settings\rhinson\My Documents\reporting.mde"
Set cn = New Connection
cn.Open conn_str
Set sht = Worksheets("Detail"
For i = 1 To RowsCount
For j = 1 To ColsCount
sql_str = "SELECT EntryTable(amount) as m " & _
"FROM EntryTable INNER JOIN HeaderTable ON EntryTable.[Reference #] = HeaderTable.[REference #] " & _
gWhereStrArr(i) & "And ItemTable.[Item Number] = " & gColNameArr(j) & " "
Set rs = New Recordset
Debug.Print sql_str
rs.Open sql_str, cn, adOpenStatic, adLockReadOnly
sht.Cells(i + 7, j + 1) = rs.fields("m"
rs.Close
Set rs = Nothing
Next j
Next i
cn.Close
Set cn = Nothing
End Sub