如何在Access中访问sub之间的对象变量

时间:2014-04-16 22:00:08

标签: excel vba module procedures

我有一个访问模块sub(),它通过查询创建了几百个统计列表。我开始使用记录集方法创建列表,然后将值传输到Excel电子表格。我的问题是我得到一个错误,我的程序无法编译,因为它太大,大于64k。所以如果我把它分成另一个模块中的第二个子,那么所有这些都是很好的尺寸。但是对于我的生活,我无法引用第二个子中的对象。

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

我陷入了对象的引用需要位于第二个子的顶部的位置。这是我的代码

    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

我结束了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.....

1 个答案:

答案 0 :(得分:0)

您可以在第一个方法中将这些对象作为参数传递给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编写的项目。