We help IT Professionals succeed at work.

Access VBA - Compile Error

Mark01
Mark01 asked
on
I am trying to write a procedure that runs batch files, but I get an "invalid qualifier" compile error. Please help me resolve the error. The attached file contains the code.

The error occurs on "BatchFile."

Code:
Private Sub RunBatchFiles(BatchPath As String, BatchFile As String)

Dim varRetVal As Variant
'Dim varRetVal As Long
varRetVal = Shell(BatchPath & "" & BatchFile.cmd)
End Sub

Open in new window

EE_100619.mdb
Comment
Watch Question

Senior Developer
CERTIFIED EXPERT
Commented:
Well, it's either

Private Sub RunBatchFiles(BatchPath As String, BatchFile As String)
  
  Dim CommandLine As String
  
  If Len(BatchPath) > 0 And Right(BatchPath, 1) <> "\" Then
    BatchPath = BatchPath & "\"
  End If
  
  CommandLine = """" & BatchPath & BatchFile & """"
  Shell CommandLine

End Sub

Open in new window

or

Private Sub RunBatchFiles(BatchPath As String, BatchFile As String)
  
  Dim CommandLine As String
  
  If (InStr(BatchFile, ".bat") = 0) And (InStr(BatchFile, ".cmd") = 0) Then
    BatchFile = BatchFile & ".cmd"
  ElseIf (Right(BatchFile, 4) <> ".bat") And (Right(BatchFile, 4) <> ".cmd") Then
    BatchFile = BatchFile & ".cmd"
  End If
  
  If Len(BatchPath) > 0 And Right(BatchPath, 1) <> "\" Then
    BatchPath = BatchPath & "\"
  End If
  
  CommandLine = """" & BatchPath & BatchFile & """"
  Shell CommandLine

End Sub

Open in new window

depending on your use-case.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
See if this fixes the problem:

Private Sub RunBatchFiles(BatchPath As String, BatchFile As String)
    Dim varRetVal As Variant
    'Dim varRetVal As Long
    varRetVal = Shell(BatchPath & "\" & BatchFile & ".cmd")
End Sub

Open in new window


»bp

Author

Commented:
There are no compile errors in ste5an's and Bill's code. I get an "expected: end of statement" compile error when I test ste5an's and Bill's code. All of the code is in the attached file.

Here's how I am trying to call the procedure:

BillPrew_1 "C:\Temp" "1"
EE_100619.mdb
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
You are missing the comma between parms, try:

BillPrew_1 "C:\Temp", "1"


»bp

Author

Commented:
It works. Thank you, Bill and ste5an.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Welcome.


»bp