我const
在 VBA 模块中有一些,但我想将它们放在 INI 文件中,因为我有其他使用相同常量的应用程序。这有效:
Public Const PATH_DB As String = "\\server\folder\bdd.mdb"
然而这不起作用:
Public Const PATH_DB As String = getFromIni("path","db","C:\config.ini")
Public Function getFromIni(ByVal strSectionName As String, ByVal strEntry As String, ByVal strIniPath As String) As String
Dim x As Long
Dim sSection As String, sEntry As String, sDefault As String
Dim sRetBuf As String, iLenBuf As Integer, sFileName As String
Dim sValue As String
sSection = strSectionName
sEntry = strEntry
sDefault = ""
sRetBuf = Strings.String$(256, 0) '256 null characters
iLenBuf = Len(sRetBuf$)
sFileName = strIniPath
x = GetPrivateProfileString(sSection, sEntry, "", sRetBuf, iLenBuf, sFileName)
sValue = Strings.Trim(Strings.Left$(sRetBuf, x))
If sValue <> "" Then
getFromIni = sValue
Else
getFromIni = vbNullChar
End If
End Function
# C:\config.ini
[path]
db=\\server\folder\bdd.mdb
我的 getFromIni 函数实际上工作得很好,但当我想声明常量时却不行(它根本不编译)。我尝试了一个全局变量,但由于某种原因,它也不起作用,从同一个项目中的另一个表单使用时找不到该变量(它仅在它被声明为 const 时才有效,但我不能声明它从函数中获取值时作为 const)。
怎么了?