Hi guys so here is the deal.

I am writing a small VB program that will shell CMD and run a command.

The problem is that the program being called in CMD needs some enviroment variables. I have set these and they are showing up correctly if I open CMD and type 'set'. The enviroment variables are created within the VB script.

Now the problem is that when I shell CMD the command fails as it cannot find the environment variables. I have used Shell ("CMD /k ") so that I could debug this. When I type 'set' here the variables are not there. I assume this is because VB is executing cmd from a user profile without the environment variables saved under.

If anyone has any Ideas on how I can fix this please let me know. I hope this makes sense and feel free to ask any questions to help me sort this out.

Thanks

I have no idea what is causing your issue, but this VBA code works fine for me from Excel.

Private Declare Function SetEnvironmentVariable Lib "kernel32" Alias "SetEnvironmentVariableA" _
                        (ByVal lpName As String, ByVal lpValue As String) _
                        As Long

Sub test()
    Dim env As Long
    env = SetEnvironmentVariable("somethingnew", "Hello")

    Dim res As Integer
    res = Shell("CMD /k echo %somethingnew%", vbNormalFocus)

    'somethingnew will be set for the cmd window created above,
    'eventhough this next line deletes it from the system

    env = SetEnvironmentVariable("somethingnew", "")

End Sub

I WANT TO FOCUS A DATA FROM A TABLE IN ACCESS
I M HAVING A TABLE NAMED AS DATA, IT INCLUDES COMPLES, STATION, TRANSFORMER AND CAPACITY OF TRANSFORMER,
IF I SELECT THE NAME OF THE TRANSFORMER IN COMBOBOX, CAPACITY TEXTBOX WANT TO DISPLAY THE AMOUNT OF THE RELATED TRANSFORMER CAPACITY........
PLEASE HELP

Thanks for the reply. I will test the code later.

I did however found a way around the issue.

I have already set the enviroment variables for the user and found that the reason that my shell to the Command prompt was not working is because when the shell command runs CMD it runs under the local system account context. As this account dose not use the windows messeging service it will not know of enviroment variable changes until a reboot. To get round this I have shelled the runas command to open CMD as the user and then use sendkeys to input the password and other commands.

This article has been dead for over six months. Start a new discussion instead.