0

I have some const in a VBA module but I would like to put them in a INI file because I have other applications that use the same constants. This works:

Public Const PATH_DB As String = "\\server\folder\bdd.mdb"

This however doesn't work:

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

My getFromIni function actually works pretty well but not when I want to declare constants (it doesn't compile at all). I tried a global variable instead but for some reason, it doesn't work either, the variable cannot be found when used from another form in the same project (it only works when it's declared as a const, but I can't declare it as a const when getting the value from a function).

What's wrong?

dan
  • 3,439
  • 17
  • 54
  • 82
  • 1
    You can't define a constant with the results of a function, no different to saying Public Const PATH_DB As String = myVar. Basically If you want to get the values from a file, they aren't constant anymore – Tony Hopkinson Oct 05 '12 at 13:53

2 Answers2

3

You cannot assign a function call as the value of a CONST string. I would expect you to receive the error "Constant expression required" when running this.

David W
  • 10,062
  • 34
  • 60
1

Change

Public Const PATH_DB As String = getFromIni("path","db","C:\config.ini")

to

Public PATH_DB As String 

Place the following call that gets the value from INI file in a initialize method (say Database Open event)

PATH_DB = getFromIni("path","db","C:\config.ini")
shahkalpesh
  • 33,172
  • 3
  • 63
  • 88
  • So, there is no way to declare it as a constant even if it won't change when the app is running. I will do some more tests because a public string wasn't working in my previous tests (I got some problems with the scope of the variable). Any advantage/disadvantage to use the `global` keyword instead? – dan Oct 05 '12 at 13:55
  • Think about what you are saying - you immediately change this variable (which is "const") in its declaration. That makes no sense, since it's being redefined immediately. – enderland Oct 05 '12 at 13:57
  • 2
    @dnLL: `const` requires a value to be given at design/code time. – shahkalpesh Oct 05 '12 at 13:59
  • And the answer to my other question: http://stackoverflow.com/questions/3815547/what-is-the-difference-between-dim-global-public-and-private-as-modular-field Thank you everyone. – dan Oct 05 '12 at 14:04