0

I have an access database that I want to run completely from a shared drive for other users, with the VBA written into a FrontEnd form.

As part of one of it's functions, it calls a batch file, using VBA, to compress other files into a text file which is then linked and imported into the database.

However since moving the database onto the shared drive, along with the batch files, it cannot find the batch file, as CMD says UNC paths are not supported. Also ideally I would like the VBA to wait for the batch file to complete before continuing with the VBA, as I currently have another function that makes the code wait for a set amount of time (WaitFor(10)).

The CMD claims "U:\My" is not recognised as an internal or external command. Is this in the VBA coding or the batch file?

Code Concerned:

Dim txtPath, batpath, FormDate, tempPath As String

FormDate = Format(Now(), "yyyymmdd")
batpath = Application.CurrentProject.Path & "\Compressor\"
txtPath = Application.CurrentProject.Path & "\Data_TextFile\"
tempPath = Application.CurrentProject.Path & "\TempTextFlows\"

'Do files exist in temp folder?
If Dir(tempPath & "*.*") = "" Then

    'If files don't exist change status to:
  Me.Text41 = "No File(s) Found"
  Else

   'If files do exist change status to:
  Me.Text41 = "Compressing Files, Please Wait..."

   'Call the Files compressor
  Call Shell(Environ$("COMSPEC") & " /c " & batpath & "myBat.bat", vbNormalFocus)

   'Wait 5 seconds for flow compressor to run
  Call WaitFor(10)

   'Link text file created by flow compressor using "TextFile_" and todays date
  DoCmd.TransferText acLinkDelim, "TextFile20160126 Link Specification", _
 "TextFile_" & FormDate, txtPath & "TextFile_" & FormDate & ".txt"

   'Format recently linked FormatTextFlows into tables
  Call FormatTextFlows
  Me.Text41 = "Files Completed"

End If

Below is the batch file it calls:

U:
cd "My Folder\test\FormatTextFlows\TempTextFlows"

echo Date format = %date%
echo dd = %date:~0,2%
echo mm = %date:~3,2%
echo yyyy = %date:~6,4%
echo.
echo Timestamp = %date:~6,4%-%date:~3,2%-%date:~0,2%

copy *.USR "My Folder\test\FormatTextFlows\Data_TextFile\TextFile_%date:~6,4%%date:~3,2%%date:~0,2%.txt"

Thanks in advance for any help anyone can provide.

3
  • 1
    You can't use the CD command on a UNC path; you can mount the UNC path to a drive letter and access it that way if you need to change the active directory. If you can put the base UNC path into a variable then you can use that to build the full path to each object in the batch file.
    – CoveGeek
    Feb 4, 2016 at 16:08
  • Hi CoveGeek! That would explain why I couldn't call it and just kept getting the same error message. I actually change the Call Shell(... to just Variable = Shell(... and that seemed to work.
    – Wowdude
    Feb 4, 2016 at 16:22
  • When you pass a path with spaces in it through the shell command, you need to make sure there are quotes around the path that are included into the parameter. using chr(34) can add a quote character to the parameter so that the command line interpreter will treat it as a single parameter. If I had more time I could work up a full example for both the vbs and cmd portions.
    – CoveGeek
    Feb 4, 2016 at 17:04

2 Answers 2

1

When you have spaces, you need quotes:

U:
cd "My Folder\test\FormatTextFlows\TempTextFlows"

But using this, I guess this should read:

copy *.USR "U:\My Folder\test\FormatTextFlows\Data_TextFile\TextFile_%date:~6,4%%date:~3,2%%date:~0,2%.txt"

You may also need quotes in the Shell command:

Call Shell(Environ$("COMSPEC") & " /c " & Chr(34) & batpath & "myBat.bat" & Chr(34), vbNormalFocus)

To debug:

'Link text file created by flow compressor using "TextFile_" and todays date
Debug.Print "TextFile20160126 Link Specification", _
"TextFile_" & FormDate, txtPath & "TextFile_" & FormDate & ".txt"
DoCmd.TransferText acLinkDelim, "TextFile20160126 Link Specification", _
"TextFile_" & FormDate, txtPath & "TextFile_" & FormDate & ".txt"

To check the Shell call:

Debug.Print Environ$("COMSPEC") & " /c " & Chr(34) & batpath & "myBat.bat" & Chr(34)
Call Shell(Environ$("COMSPEC") & " /c " & Chr(34) & batpath & "myBat.bat" & Chr(34), vbNormalFocus)
10
  • Hi Gustav! Thanks for the advice. I've added the " " around the file path and added in the ` "U:\...` for the copy destination, but i'm still getting the same message in CMD and the batch file does not run. I'm guessing then it can't find the .bat.
    – Wowdude
    Feb 4, 2016 at 9:45
  • Hi again Gustav! I added in the Chr(34) into the places as the line you gave me, but the CMD is still cutting the path down to just the root and first word of the path :(
    – Wowdude
    Feb 4, 2016 at 11:13
  • Perhaps you need batpath to hold both drive and folder. Where exactly does the error pop?
    – Gustav
    Feb 4, 2016 at 11:34
  • Hi Gustav! The error in the VBA actually occurs on line beginning DoCmd.TransferText acLinkDelim however this is because it cannot find the file that should have been created by the batch file, and bugs out. I thought that because I have made batpath from Application.CurrentProject.Path and its next folder, that it would contain the full path to the file, just not the file name itself. Does the batch file name and extension also need to be added?
    – Wowdude
    Feb 4, 2016 at 13:11
  • Then insert the Debug.Print as shown above, and carefully study the output to see if it matches the name of the created file.
    – Gustav
    Feb 4, 2016 at 13:19
1

I managed to solve the issue by read on Microsoft's website (https://support.office.com/en-us/article/shell-function-ff2e4b1b-712d-4e34-aea6-6832eadd3c63). so I replaced the line Call Shell(Environ$("COMSPEC") & " /c " & batpath & "myBat.bat", vbNormalFocus) with Dim UtilShell and UtilShell = Shell(batpath & "D0301.bat", vbHide)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.