Problems passing a varible to Wscript.Shell.Run() method

I am writing some VBA for an access database and I am having some problems. I have gotten my commands to work properly with static values in the code, now I need it to be based on varibles.

The code looks like this:

    Dim cmd As String
    cmd = "%comspec% /k title hello|cscript.exe C:\XCACLS.vbs "" & elevatedNetworkPath & "" /g ESELLC\ESE:;2 /E /SPEC B"
    Debug.Print cmd
    If ESEAccessible = True Then
    Set objShell = CreateObject("Wscript.Shell")
        objShell.Run (cmd), 0, True

Open in new window

The Code will work right if i use:

 objShell.Run ("%comspec% /c title hello|cscript.exe C:\XCACLS.vbs ""Z:\folder"" /g ESELLC\ESE:;3 /E /SPEC B"), 0, True

Open in new window

When the code goes to execute, It just hangs and hangs until I close the form and hit the stop button. I have tried using different configurations of quotations (up to four on each side of the varible) - and did a Debug.print of both strings commands (with and without the varialbe) and the string is exactly the same in the Debug window.

Any ideas?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Justin MathewsCommented:
The line should be:

cmd = "%comspec% /k title hello|cscript.exe C:\XCACLS.vbs """ & elevatedNetworkPath & """ /g ESELLC\ESE:;2 /E /SPEC B"
Gssc1414Author Commented:

I agree - that makes sense. However, when I do this - it just hangs. I have tried one, two, three and four quotations on both sites of the variable - all with no luck.
Justin MathewsCommented:
How about /c instead of /k for %comspec%

cmd = "%comspec% /c title hello|cscript.exe C:\XCACLS.vbs """ & elevatedNetworkPath & """ /g ESELLC\ESE:;2 /E /SPEC B"
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Gssc1414Author Commented:
Also have tried that - doesn't seem to make a difference :/
I've had a weird issue before with quotes and the answer was to use a function for the quotes

cmd = "%comspec% /k title hello|cscript.exe C:\XCACLS.vbs " & Quote(elevatedNetworkPath) & " /g ESELLC\ESE:;2 /E /SPEC B"

Function Quote(sText)
    Quote = chr(34) & sText & chr(34)
End Function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Darren CollinsTech Lead (Windows), Endpoint Device Management ServicesCommented:

Try an ampersand instead of the pipe, and use three double-quotes each side of the variable as jmatix said:

cmd = "%comspec% /k title hello & cscript.exe C:\XCACLS.vbs """ & elevatedNetworkPath & """ /g ESELLC\ESE:;2 /E /SPEC B"

Gssc1414Author Commented:

That was it! It didn't work completely that way, but when I put the string directly in the objShell.Run() command it worked perfect. For some reason when I assign it to a string and then pass that into the objShell.Run command it did the same thing.

objShell.Run ("%comspec% /c title hello|cscript.exe C:\XCACLS.vbs " & Quote(elevatedNetworkPath) & " /g ESELLC\ESE:;2 /E /SPEC B"), 0, True

Not sure why, but thanks for the help!

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.