无法在模块VB6中声明字符串

时间:2015-01-28 16:50:51

标签: vba vb6

我正在尝试从遗留应用程序的文件中读取数据库字符串。为此,我将文件放在每个用户的AppData文件夹中。现在,我需要告诉应用程序文件的位置。但是,我似乎无法在模块中声明一个字符串(原始字符串在此模块中声明为常量)。我怎么能克服这个?任何帮助将不胜感激。

这就是我的尝试:

Dim loc As String
loc = Environ$("APPDATA")
Public Const CnnSTR = ReadIniValue(loc & "\PensionMaster\PM-Strings.ini", "DB", "DBSQL")

但我收到了'无效的外部程序' 错误。

2 个答案:

答案 0 :(得分:5)

蒂姆威廉姆斯是对的。而不是试图将其声明为公共变量,而是创建公共函数或属性。我的偏好是在.bas模块中创建一个函数,在类中创建一个属性。

Public Function GetCnnSTR() As String
    Dim loc As String
    Dim strPath as String

    loc = Environ$("APPDATA")
    strPath = ReadIniValue(loc & "\PensionMaster\PM-Strings.ini", "DB", "DBSQL")

    GetCnnSTR = strPath

End Function

Public Property Get CnnSTR() As String
    Dim loc As String
    Dim strPath as String

    loc = Environ$("APPDATA")
    strPath = ReadIniValue(loc & "\PensionMaster\PM-Strings.ini", "DB", "DBSQL")

    CnnSTR = strPath

End Property

答案 1 :(得分:0)

CnnSTR = ReadIniValue(Environ$("APPDATA") & "\PensionMaster\PM-Strings.ini", "DB", "DBSQL"

会工作吗?也许是Dim CnnSTR as String行。

Const x=5
y = X

完全相同
y=5