当执行移动到不同模块中的sub时,我的公共变量正在丢失范围.我无法理解为什么会发生这种情况.
模块A:
Option Compare Database
Public db As DAO.Database
Public xlApp As Excel.Application
Sub Main()
Dim db As DAO.Database
Set db = CurrentDb
ProcessFiles ' upload the data from the report files
AppendToPentana ' transfer the data from the main tables to the pentana export table
ProduceFinalExport ' export the final data to Excel
End Sub
Run Code Online (Sandbox Code Playgroud)
模块B:
Sub ProcessFiles()
Dim recSet As Recordset
' open a recordset containing keywords to look for in the file names to identify the area
Set recSet = db.OpenRecordset("tblFiles")
' rest of code
End Sub
Run Code Online (Sandbox Code Playgroud)
在模块B中,变量db已设置为Nothing,我得到一个对象引用错误.我尝试使用较旧的"全局"声明而不是使用相同的结果.
公共变量是常用的,我不知道这里出了什么问题.
Sub Main()
Dim db As DAO.Database
Set db = CurrentDb
Run Code Online (Sandbox Code Playgroud)
你有一个额外的本地变量db中Main,这是你设置的一个.局部变量优先于全局变量.
只需删除该Dim行即可Main.
| 归档时间: |
|
| 查看次数: |
134 次 |
| 最近记录: |