我试图通过使范围尽可能严格来尽可能地隐藏ADO
在我的例程中的代码部分中。Excel-vba
模块一:
Sub control()
Const strConn As String = _
"PROVIDER=SQLOLEDB.1;" & _
"PASS******=xxxxxxxxxxxxxxxxx;" & _
"PERSIST SECURITY INFO=True;" & _
"USER ID=xxxxxxxxxxxxxxxxx;" & _
"INITIAL CATALOG=xxxxxxxxxxxxxxxxx;" & _
"DATA SOURCE=xxxxxxxxxxxxxxxxx;" & _
"USE PROCEDURE FOR PREPARE=1;" & _
"AUTO TRANSLATE=True;" & _
"CONNECT TIMEOUT=0;" & _
"COMMAND TIMEMOUT=0" & _
"PACKET SIZE=4096;" & _
"USE ENCRYPTION FOR DATA=False;" & _
"TAG WITH COLUMN COLLATION WHEN POSSIBLE=False"
Dim c As ADODB.Connection
Dim r As ADODB.Recordset
Set c = New ADODB.Connection
c.ConnectionTimeout = 0
c.Open strConn
Set r = New ADODB.Recordset
r.ActiveConnection = c
Call someADOproc(r)
End Sub
模块 2:
Sub someADOproc(ByRef ar As Object)
ar.Open _
"SELECT top 1 Operator " & _
"FROM xxxxxxxxx.dbo.xxxxxxxxxxxx "
MsgBox ar.Fields(0).Value
End Sub
例程中变量的严格范围control
让我感到惊讶,它ByRef
实际上有效并且变量在someADOproc
位于不同模块中的例程中可用?我原以为这会失败。
ByVal
也可以工作 - 这是否符合预期,因为它正在传递记录集的副本r
?
我对这些概念的理解缺少什么?
我应该使用ByVal
orByRef
吗?