如何访问Access中的子对象之间的对象variables

我有一个访问模块sub(),通过查询创build数百个统计列表。 我开始使用logging集方法创build列表,然后将值传输到Excel电子表格。 我的问题是,我得到一个错误,我的程序无法编译,因为它太大,大于64K。 所以,如果我把它分成另一个模块中的第二个子元素,那么所有的都是明智的。 但是在我的生活中,我无法参考第二部分中的对象。

我怀疑我可以使用with语句,但是在search论坛之后,我无法弄清楚不会抛出错误的语法。

我坚持在对象的引用需要在第二个子顶部的点。 这是我有的代码

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 

我结束了子,并继续在第二个模块

  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..... 

您可以将这些对象作为parameter passing给第一个方法中的SomeSub_part2()。

 Call Module2.SomeSub_part2(xlx, xlw, xls, xlc) 

然后将它们作为参数添加到“第2部分”方法中:

 Public Sub SomeSub_part2(xlx As Object, xlw As Object, xls As Object, xlc As Object) ... 

免责声明:我是C#开发人员,但我目前正在翻译用VBA编写的项目。