make a constant that involves tying a variable into itself in Access 97 VBA

I'm trying the following to make a constant variable in Access 97 VBA...

Public Const sCPATH = "C:\Documents and Settings\" & fOSUserName & "\My Documents\EC"

...but I keep getting an error Compile error constant expression required:  fOSUsername is actually a function with the following:  How should I arrange the above and below code to make sCPATH a constant for my use.

Public Function fOSUserName() As String
On Error GoTo Err_fOSUserName

' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
    strUserName = String$(254, 0)
    lngLen = 255
    lngX = apiGetUserName(strUserName, lngLen)
    If (lngX > 0) Then
        fOSUserName = Left$(strUserName, lngLen - 1)
        fOSUserName = vbNullString
    End If
    Exit Function
    Call Error_Action(Err, Err.Description, "modCPUName @ fOSUserName", Erl())
    Resume Exit_fOSUserName
End Function

Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
    "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Who is Participating?
Jim HornConnect With a Mentor Microsoft SQL Server Developer, Architect, and AuthorCommented:
If you're using a variable as part of its declaration, then it's not very constant, is it?  

Likely what happens is that Access sets all constants to memory before you code sets a value in fOSUserName.

I recommend using a global string variable instead, and only change it when you declare it on startup.  

Global sPath as String

... somewhere in code...
sPath = "C:\Documents and Settings\" & fOSUserName & "\My Documents\EC"
stephenlecomptejrAuthor Commented:
thanks Jim.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.