Batch file that runs a function (1 Viewer)

oxicottin

Learning by pecking away....
Local time
Today, 06:59
Joined
Jun 26, 2007
Messages
851
Hello I have a module that works so far as in it remaps my drives like I want but at the end of the code I want to run a function AutoLinkManager. It just skips over the relink.

Code:
'Global variable for path to original database location
Public gDBPath As String

Public Sub RemappingDrives()
    Dim BatchFile As String
    Dim strKillFile As String
    Dim strRestart As String

    'Sets the file name and location from your global variable
    strKillFile = gDBPath
    'Sets the file name of the temp batch file
    BatchFile = CurrentProject.Path & "\RemappingDrives.cmd"
    'Sets the restart file name
    strRestart = """" & strKillFile & """"

    'Creates the batch file
    Open BatchFile For Output As #1
    Print #1, "Echo Off"
    Print #1, "ECHO Remapping Drives P: X: G: H:"
    Print #1, "ping 127.0.0.1 -n 5 -w 1000 > nul"
    Print #1, ""
    Print #1, "net use P: \\weir-ms-001\weir-public  /persistent:yes"
    Print #1, "net use x: \\weir-ms-001\weir-appdata  /persistent:yes"
    Print #1, "net use G: \\weir-ms-001\weir-groupdata  /persistent:yes"
    Print #1, "net use h: \\weir-ms-001\weir-home  /persistent:yes "
    Print #1, ""
    Print #1, "ECHO Restarting Microsoft Access..."
    Print #1, "START /I " & """MSAccess.exe"" " & strRestart
    Print #1, ""
    Print #1, "ECHO Relinking Tables...."
    Print #1, "/X ""AutoLinkManager"""
    Print #1, "PAUSE"
    Print #1, "ECHO Deleting temp batch file...."
    Print #1, "Del """ & BatchFile & """"
    Close #1

    'Runs the batch file
    Shell BatchFile

    'Closes the current version and runs the batch file
    DoCmd.Quit

End Sub

Public Sub AutoLinkManager()
    Dim dbFE As DAO.Database
    Dim dbBE As DAO.Database
    Dim tdf As TableDef
    Dim tdf1 As TableDef
    Dim strFEFile As String
    Dim strBEFile As String
    Dim strTblName As String

    strBEFile = DLookup("be_masterlocation", "tbl-version_master_location")
    strFEFile = DLookup("fe_masterlocation", "tbl-version_master_location")


    Set dbFE = DBEngine.Workspaces(0).OpenDatabase(strFEFile)
    Set dbBE = DBEngine.Workspaces(0).OpenDatabase(strBEFile)

    On Error Resume Next
    For Each tdf1 In dbBE.TableDefs
        strTblName = tdf1.Name
        If Left(strTblName, 4) <> "msys" Then
            dbFE.TableDefs.Delete strTblName
            Set tdf = dbFE.CreateTableDef(strTblName)
            tdf.Connect = ";DATABASE=" & strBEFile
            tdf.SourceTableName = strTblName
            dbFE.TableDefs.Append tdf
        End If
    Next tdf1

    Set dbBE = Nothing
    Set dbFE = Nothing
    
MsgBox "Linking Complete", vbInformation

End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:59
Joined
Oct 29, 2018
Messages
21,358
Hi. Does your batch file results in the /x switch showing up in the same line as Start?
 

Isaac

Lifelong Learner
Local time
Today, 03:59
Joined
Mar 14, 2017
Messages
8,738
I think it may have to be a Macro to use the X
Which you can solve by creating a Macro that runs the procedure.

Alternately, as dbGuy mentioned, you could use it as part of a startup switch.
 

oxicottin

Learning by pecking away....
Local time
Today, 06:59
Joined
Jun 26, 2007
Messages
851
DBGuy im not sure im following your question? Below is the result of that section of the batch file when ran...

Restarting Microsoft Access...
The process cannot access the file because it is being used by another process.
Relinking Tables....
'/X' is not recognized as an internal or external command,
operable program or batch file.
Press any key to continue . . .
 

Isaac

Lifelong Learner
Local time
Today, 03:59
Joined
Mar 14, 2017
Messages
8,738
The x switch has to be on the same line as the Start line...it's all part of a startup switch technique.

But this read mentions it can be used for Macros....not functions or subs.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:59
Joined
Oct 29, 2018
Messages
21,358
DBGuy im not sure im following your question? Below is the result of that section of the batch file when ran...
Hi. As Isaac said, /x by itself on a separate line in a batch file will not resolve into a recognizable command.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:59
Joined
May 7, 2009
Messages
19,175
using vba, you can Map a folder to a Drive:
Code:
With CreateObject("WScript.Network")
    .MapNetworkDrive "P:", "\weir-ms-001\weir-public", True
    .MapNetworkDrive "x:", "\\weir-ms-001\weir-appdata", True
    .MapNetworkDrive "G:", "\\weir-ms-001\weir-groupdata", True
    .MapNetworkDrive "h:", "\\weir-ms-001\weir-home", True
End With
 

Users who are viewing this thread

Top Bottom