[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 478
  • Last Modified:

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
1 Solution
Jim HornMicrosoft 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.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now