I have an access module sub() that is creating several hundred statistic lists via queries. I began with using the recordset method to create the list then transfer the values to an excel spreadsheet. My problem is that I get an error that my procedure cannot be compiles because it is too large, greater than 64k. So all is fine size wise if I split it into a second sub in another module. But for the life of me I am unable to reference the objects in the second sub.
I suspect that I can use a with statement, but after scouring the forums I cant figure out the syntax that doesnt throw and error.
Im stuck at the point where the reference to the objects needs to be at the top of the second sub. Here is the code that I have
Public Sub SomeSub()
Dim lngColumn As Long
Dim xlx As Object, xlw As Object, xls As Object, xlc As Object
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim blnEXCEL As Boolean, blnHeaderRow As Boolean
Dim sqlMin As Variant
blnEXCEL = False
blnHeaderRow = False
' Establish an xls app object
On Error Resume Next
Set xlx = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set xlx = CreateObject("Excel.Application")
blnEXCEL = True
End If
Err.Clear
On Error GoTo 0
' select True to keep xls visible while program runs
xlx.Visible = True
' Path to file
Set xlw = xlx.Workbooks.Open("C:\file.xlsx")
' Name of worksheet=
Set xls = xlw.Worksheets("SomeWorksheet")
Set xlc = xls.range("C5") ' this is the first cell into which data go
Set dbs = CurrentDb()
'Table or query or source whose data are to be written into the worksheet
Set rst = dbs.OpenRecordset("qryCount", dbOpenDynaset, dbReadOnly)
If rst.EOF = False And rst.BOF = False Then
.......Lots of code iterations
Call Module2.SomeSub_part2
End Sub
I end the sub and continue in a second module
Option Compare Database
Public Sub SomeSub_part2()
'Im not sure what to put here to reference the objects that are being set
'where the code resumes in a second module
Set xlc = xls.range("AC18") ' this is the first cell into which data go
Set dbs = CurrentDb()
'Table or query or source whose data are to be written into the worksheet
Set rst = dbs.OpenRecordset("qryCount77", dbOpenDynaset, dbReadOnly)
If rst.EOF = False And rst.BOF = False Then
rst.MoveFirst
If blnHeaderRow = True Then
For lngColumn = 0 To rst.Fields.Count - 1
xlc.Offset(0, lngColumn).Value = rst.Fields(lngColumn).Name
Next lngColumn
Set xlc = xlc.Offset(1, 0)
End If
' write data to worksheet
Do While rst.EOF = False
For lngColumn = 0 To rst.Fields.Count - 1
xlc.Offset(0, lngColumn).Value = rst.Fields(lngColumn).Value
Next lngColumn
rst.MoveNext
Set xlc = xlc.Offset(1, 0)
Loop
......The rest of the code.....
You can just pass those objects as arguments into SomeSub_part2() in your first method.
Call Module2.SomeSub_part2(xlx, xlw, xls, xlc)
And then add them as parameters to your "part 2" method:
Public Sub SomeSub_part2(xlx As Object, xlw As Object, xls As Object, xlc As Object)
...
Disclaimer: I'm a C# developer, but I'm currently working on translating a project written in VBA.