公共变量超出范围

时间:2017-08-22 14:24:16

标签: vba ms-access

当执行移动到不同模块中的sub时,我的公共变量正在丢失范围。我无法理解为什么会这样。

模块A:

websockify/websocket.py

模块B:

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

在模块B中,变量db已设置为Nothing,我得到一个对象引用错误。我尝试使用较旧的“全局”声明而不是使用相同的结果。

公共变量是常用的,我不知道这里出了什么问题。

1 个答案:

答案 0 :(得分:6)

Sub Main()

Dim db As DAO.Database

Set db = CurrentDb

db中还有一个本地变量Main,这是您设置的变量。局部变量优先于全局变量。

只需从Dim删除该Main行即可。