Rasm
02-02-2012, 06:46 AM
Below is my code to read records from two acces tables using VBA in xl2007 - rather than reading specific fields - I use the 'FROM *' - so I figure this is much faster than having a much longer and complicated SQL statement - hower my records sets can be very large (Recordcount>20K and FieldCount>30) - should I be concerned about memory usage - or is the memory released by a RstRcd.Close line in my code.
I would be interested in comments of using a complicated SQL statement with WHERE and >= clauses and so on Vs the memory concern by just reading the lot.
Second question:
I immdeiately copy the data into a temporary sheet - so I can use the Sort function in Excel - I know of no way to easily sort multidimentional arrays - If anybody has a good sort routine for sorting arrays I would appreciate a copy - I looked at Chip Pearson's code example - but it only handles single dimentional arrays.
Third Question:
The ADO connection has a memory leak - was covered in earlier post here - any suggestion what to use as alternative - should I use an ODBC connection or similar - This is all new to me - so I am not sure if I am going to have memory issues.
Set adoConn = CreateObject("ADODB.Connection")
Set RstRcd = CreateObject("ADODB.Recordset")
On Error Resume Next
With adoConn
Err.Clear
.provider = "Microsoft.Jet.Oledb.4.0"
.Properties("Jet OLEDB:Database Password") = DB_PW
.Open "Data Source=" & strDBPath
If Err.Number <> 0 Then
On Error GoTo 0
MsgBox "Cannot find database - select another"
Exit Sub
End If
End With
'***** Reads the SampleHeader/SampleData Tables - using relationship
strTable = "SampleHeader"
Application.StatusBar = "Reading Access table --- 'SampleHeader'"
Astr = "SELECT * FROM (SampleHeader INNER JOIN SampleData ON SampleHeader.SampleID = SampleData.SampleID) INNER JOIN Parameter ON SampleData.ConstituentID = Parameter.ParameterID"
RstRcd.Close
RstRcd.Open Astr, adoConn, 3, 3
'******* Copies the entire dataser into worksheet "TempData" -- in ThisWorkBook
ThisWorkbook.Worksheets("TempData").UsedRange.Clear
For ii = 1 To RstRcd.fields.Count
ThisWorkbook.Worksheets("TempData").Cells(1, ii).Value = RstRcd.fields(ii - 1).Name
Next ii
ThisWorkbook.Worksheets("TempData").Cells(2, 1).CopyFromRecordset RstRcd
I would be interested in comments of using a complicated SQL statement with WHERE and >= clauses and so on Vs the memory concern by just reading the lot.
Second question:
I immdeiately copy the data into a temporary sheet - so I can use the Sort function in Excel - I know of no way to easily sort multidimentional arrays - If anybody has a good sort routine for sorting arrays I would appreciate a copy - I looked at Chip Pearson's code example - but it only handles single dimentional arrays.
Third Question:
The ADO connection has a memory leak - was covered in earlier post here - any suggestion what to use as alternative - should I use an ODBC connection or similar - This is all new to me - so I am not sure if I am going to have memory issues.
Set adoConn = CreateObject("ADODB.Connection")
Set RstRcd = CreateObject("ADODB.Recordset")
On Error Resume Next
With adoConn
Err.Clear
.provider = "Microsoft.Jet.Oledb.4.0"
.Properties("Jet OLEDB:Database Password") = DB_PW
.Open "Data Source=" & strDBPath
If Err.Number <> 0 Then
On Error GoTo 0
MsgBox "Cannot find database - select another"
Exit Sub
End If
End With
'***** Reads the SampleHeader/SampleData Tables - using relationship
strTable = "SampleHeader"
Application.StatusBar = "Reading Access table --- 'SampleHeader'"
Astr = "SELECT * FROM (SampleHeader INNER JOIN SampleData ON SampleHeader.SampleID = SampleData.SampleID) INNER JOIN Parameter ON SampleData.ConstituentID = Parameter.ParameterID"
RstRcd.Close
RstRcd.Open Astr, adoConn, 3, 3
'******* Copies the entire dataser into worksheet "TempData" -- in ThisWorkBook
ThisWorkbook.Worksheets("TempData").UsedRange.Clear
For ii = 1 To RstRcd.fields.Count
ThisWorkbook.Worksheets("TempData").Cells(1, ii).Value = RstRcd.fields(ii - 1).Name
Next ii
ThisWorkbook.Worksheets("TempData").Cells(2, 1).CopyFromRecordset RstRcd