我有一个.ini文件设置,我从中读取连接字符串。我有一个模型来读取字符串:
Option Explicit
Private Declare Function GetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any _
, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long _
, ByVal lpFileName As String) As Long
Public Const iniPath = "\DBSettings.INI"
Public Sub Main()
Dim dbPath As String
Dim dbPath As String
dbPath = GetSetting("DataBase", "DBPath")
dbPath= GetSetting("DataBase", "DBPath")
Form1.Show
End Sub
Private Function GetSetting(ByVal pHeading As String, ByVal pKey As String) As String
Const cparmLen = 100
Dim sReturn As String * cparmLen
Dim sDefault As String * cparmLen
Dim aLength As Long
aLength = GetPrivateProfileString(pHeading, pKey _
, sDefault, sReturn, cparmLen, App.Path & iniPath)
GetSetting = Mid(sReturn, 1, aLength)
End Function
现在,我试图在点击按钮时显示字符串:
Option Explicit
Public Sub Command1_Click()
MsgBox (dbPath)
MsgBox (dbPath)
End Sub
但是,似乎表单无法看到模块中的变量。我该如何解决这个问题? 任何帮助将不胜感激。
答案 0 :(得分:3)
dbPath
在sub
中声明,因此范围规则规定变量仅存在于该子变量中。
要使变量可见,请在具有public
访问修饰符的模块中声明它:
public dbPath as string
然后移除dim
main()
答案 1 :(得分:1)
DBPath是过程范围变量,因为它在过程(Sub Main())中声明。 它不会从其他模块,甚至模块本身看到。 而且,您不能在Procedure(Sub或Function)中声明Public变量。
有关变量范围的更多信息: http://msdn.microsoft.com/en-us/library/1t0wsc67.aspx