Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2005
    Posts
    333

    Unanswered: Setting system level environment variable

    Does anyone know how to set a system level enviroment variable in VBA? I need to set a variable and append a new directory to the existing path variable. Any help would be greatly appreciated. Thanks

  2. #2
    Join Date
    Mar 2003
    Location
    The Bottom of The Barrel
    Posts
    6,102
    Provided Answers: 1
    If you mean the way I think you mean, you can't.

    Are you talking about changing a variable from one front end and having that change the same variable in every other front end that's hitting the database? The best way to do that is to create a "control" table that contains a single row. Each field in that table is a single, application-wide value that needs to be dynamic and available to all users.

    Or are you doing something completely different?
    oh yeah... documentation... I have heard of that.

    *** What Do You Want In The MS Access Forum? ***

  3. #3
    Join Date
    Feb 2005
    Posts
    333
    Or are you doing something completely different?
    Something totally different. What I want to do is equivalent to right clicking on "My Computer", going to properties, select the "Advanced" tab, clicking the "Enviornment Variables" button and setting a variable/path.

  4. #4
    Join Date
    Feb 2005
    Posts
    333
    Here is some code that I found on MSDN that retrieves the environment PATH variable but I want to know if I can set the variable.

    Code:
    Dim EnvString, Indx, Msg, PathLen   ' Declare variables.
    Indx = 1   ' Initialize index to 1.
    Do
       EnvString = Environ(Indx)   ' Get environment 
                ' variable.
       If Left(EnvString, 5) = "PATH=" Then   ' Check PATH entry.
          PathLen = Len(Environ("PATH"))   ' Get length.
          Msg = "PATH entry = " & Indx & " and length = " & PathLen
          Exit Do
       Else
          Indx = Indx + 1   ' Not PATH entry,
       End If   ' so increment.
    Loop Until EnvString = ""
    If PathLen > 0 Then
       MsgBox Msg   ' Display message.
    Else
       MsgBox "No PATH environment variable exists."
    End If

  5. #5
    Join Date
    Feb 2005
    Posts
    333
    I'm getting close. Here's a link that helps but I have to convert some of this to VBA.http://www.microsoft.com/technet/scr..._wsh_kmmj.mspx
    For Example, the script is this:
    Code:
    Set objShell = WScript.CreateObject("WScript.Shell")
    Set colSystemEnvVars = objShell.Environment("System")
    Set colUserEnvVars = objShell.Environment("User")
    Wscript.Echo "Computer-specific PATH Environment Variable"
    Wscript.Echo colSystemEnvVars("PATH")
    Wscript.Echo "User-specific PATH Environment Variable"
    Wscript.Echo colUserEnvVars("PATH")
    But this works in VBA
    Code:
    Set objShell = CreateObject("WScript.Shell")
    Set colSystemEnvVars = objShell.Environment("System")
    Set colUserEnvVars = objShell.Environment("User")
    
    MsgBox colSystemEnvVars("PATH")
    MsgBox colUserEnvVars("PATH")
    So the next step is to add/alter the variables. The Link says to do this
    colUsrEnvVars("MyVariable") = 0
    I cannot figure out where to go form here. Any suggestions?

  6. #6
    Join Date
    Feb 2005
    Posts
    333

    Problem Solved (I think)

    So the next step is to add/alter the variables. The Link says to do this

    colUsrEnvVars("MyVariable") = 0
    This seems to work.
    colUserEnvVars.Item("MyVariable") = 0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •