Wait until Call Shell is finished

TeneQuodBene

New member
Local time
Today, 17:30
Joined
Nov 20, 2003
Messages
9
Hi I'm a Newbie, below is my code which first calls a batch file and then a filecopy of values...
But I want to batch file to be ended first before the filecopy takes place...




Dim stAppName As String

stAppName = "d:\website\occasi~1\batch\delpics.bat"
Call Shell(stAppName, 0)


Dim db As DAO.Database
Dim rs As DAO.Recordset

Set db = CurrentDb
Set rs = db.OpenRecordset("Select Foto, Fotopad from Wagens")

rs.MoveFirst
Do While Not rs.EOF
On Error Resume Next
FileCopy rs!FotoPad, "d:\website\occasi~1\images\occasi~1\" & rs!Foto
rs.MoveNext
Loop
 
Thank u this helped me a lot


Thanks
 
I will bet that you can do the same thing using VBA instead of calling your batch file. Is you batch file deleting files? Post you code from your batch file and I will look at it.
 
Shell & Wait & Hide a window?

Okay, so I was using
Code:
Call Shell("Access.exe SomeDatabase /compact", vbHide)
in order to programmatically compact my databases. It worked great until the execution of the code got ahead of itself.

So I came across Shell & Wait, or CreateProcess or whatever you want to call it and that works beautifully. However, I was also using the code to automatically decompile the databases with the /decompile option. So now when the code executes not only do you see the windows (which I don't want) but also databases marked for automatic compiling will stay open until the user closes them. I'm trying to get this app ready to run in the background on a server, so this is no good. Any suggestions would be greatly appreciated.

For reference, here is the code I'm using to do this. I'm just throwing on the whole module so if it means nothing to you then just ignore it. =)

NOTE: ExecuteCmd is what I renamed the Shell & Wait function

Also, I should warn that this is still a half-baked solution so the code is a bit messy.
Code:
Option Compare Database

Public blnRunSilent As Boolean
Private strLogFile As String
Private blnCreateLog As Boolean
Private strBackupFolder As String
Private intLogType As Integer
Public Const LOG_OVERWRITE As Integer = 1
Public Const LOG_APPEND As Integer = 2
Public Const LOG_USEDATE As Integer = 3
Private blnBackupToCurrentDir As Boolean
Private strDbName As String
Private strDestination As String
Private strCompactTemp As String
Private Const strAccessPath As String = """C:\Program Files\OfficeXP\Office10\Msaccess.exe"""
Private fileNum As Integer
Private strOut As String
Private blnLogSeparated As Boolean


Public Function RunMaintenance(Optional strProfile As String)
On Error GoTo Err_RunMaintenance
    
    If IsMissing(strProfile) Or strProfile = "" Then
        fSetAccessWindow (0)
        strProfile = Command()
        blnRunSilent = True
    End If
    
    'MsgBox "Profile: " & strProfile
    
    Dim rs As dao.Recordset
    Set rs = CurrentDb.OpenRecordset("tblBackupProfiles", dbReadOnly)
    rs.FindFirst "ProfileName = '" & strProfile & "'"
    If rs.NoMatch Then
        If blnRunSilent = False Then
            MsgBox "Could not find the specified profile '" & strProfile & "'"
        Else
            DoCmd.Quit
        End If
        Exit Function
    Else
        blnCreateLog = rs!CreateLog
        If blnCreateLog Then
        
            If IsNull(rs!LogFile) Then
                strLogFile = ReplaceExtension(CurrentDb.Name, ".log")
            Else
                strLogFile = rs!LogFile
            End If
            
            If IsNull(rs!LogType) Then
                intLogType = LOG_APPEND
            Else
                intLogType = rs!LogType
            End If
            
        End If
        
        If IsNull(rs!BackupFolder) Then
            blnBackupToCurrentDir = True
        Else
            blnBackupToCurrentDir = False
            strBackupFolder = rs!BackupFolder
        End If
        
        'MsgBox "Profile :" & strProfile & vbCrLf & _
                "Create Log: " & blnCreateLog & vbCrLf & _
                "Log File: " & strLogFile & vbCrLf & _
                "Log Type: " & intLogType & vbCrLf & _
                "Backup To Current Dir: " & blnBackupToCurrentDir & vbCrLf & _
                "Backup Folder: " & strBackupFolder

        LogMaint "Starting Maintenance Profile: " & strProfile

        Dim sqlDatabasesInProfile As String
        sqlDatabasesInProfile = "SELECT tblBackupProfileDetails.*, tblDB.FilePath, tblDB.FileName FROM tblBackupProfileDetails INNER JOIN tblDB ON tblBackupProfileDetails.DatabaseID = tblDB.DatabaseID WHERE (((tblBackupProfileDetails.ProfileName)='" & strProfile & "'));"
        Set rs = CurrentDb.OpenRecordset(sqlDatabasesInProfile, dbReadOnly)
        Dim strDatabases As String
        While Not rs.EOF
            If rs.BOF Then
                rs.MoveFirst
            End If
            'strDatabases = strDatabases & rs!DatabaseID & ": " & rs!FilePath & rs!FileName & vbCrLf & _
                vbTab & "Backup: " & rs!AutoBackup & vbTab & "Compact: " & rs!AutoCompact & vbTab & "Repair: " & rs!AutoRepair & vbTab & "Recompile: " & rs!AutoCompile & vbCrLf
            strDbName = rs!FilePath & rs!FileName
            If blnBackupToCurrentDir Then
                strBackupFolder = rs!FilePath
            End If
            strDestination = strBackupFolder & ReplaceExtension(rs!FileName, ".bak")
            If rs!AutoBackup Then
                LogMaint "Backing up " & strDbName
                FileCopy strDbName, strDestination
                LogMaint "Back up of " & strDbName & " completed!"
            End If
            
            strCompactTemp = strBackupFolder & ReplaceExtension(rs!FileName, ".Compacting")
            If rs!AutoCompact Then
                LogMaint "Compacting " & strDbName
                If ifExists(strCompactTemp) Then
                    Kill strCompactTemp
                End If
                DBEngine.CompactDatabase strDbName, strCompactTemp
                Kill strDbName
                FileCopy strCompactTemp, strDbName
                Kill strCompactTemp
                'MsgBox strDbName & " was compacted."
                LogMaint "Compaction of " & strDbName & " was completed!"
            End If
            
            
            If rs!AutoRepair Then
                LogMaint "Repairing " & strDbName
                ExecuteCmd strAccessPath & " """ & strDbName & """ /repair"
                'MsgBox strDbName & " was repaired."
                LogMaint strDbName & " was repaired!"
            End If
            
            If rs!AutoCompile Then
                LogMaint "Recompiling " & strDbName
                ExecuteCmd strAccessPath & " """ & strDbName & """ /decompile"
                'MsgBox strDbName & " was recompiled."
                LogMaint strDbName & " was recompiled."
            End If
            
        rs.MoveNext
        DoEvents
        Wend
        'MsgBox strDatabases
        
        
    End If
    
    If blnRunSilent Then
        DoCmd.Quit
    End If
Exit_RunMaintenance:
    Exit Function
    
Err_RunMaintenance:
    MsgBox Err.Number & ": " & Err.Description
    Resume Next
    
End Function

Private Sub LogMaint(strActivity As String)
If blnCreateLog Then

    fileNum = freeFile
    
    strOut = Format(Now, "mm/dd/yyyy hh:nn") & "  -  " & strActivity
    
    Select Case intLogType
        Case LOG_OVERWRITE:
            Open strLogFile For Output As fileNum
        Case LOG_APPEND:
            Open strLogFile For Append As fileNum
            If Not blnLogSeparated Then
                Write #fileNum, ""
                Write #fileNum, "----------------------------------------------------------------------------"
                Write #fileNum, ""
                blnLogSeparated = True
            End If
        Case LOG_USEDATE:
            strLogFile = Format(Date, "yyyy-mm-dd") & " " & strLogFile
            Open strLogFile For Output As fileNum
    End Select
    
    Write #fileNum, strOut
    Close #fileNum

End If

End Sub
 
Last edited:
Hmm...I guess even if you hide the Window it still tries to load the database after decompiling =(

If anyone has any ideas for automatic decompile/recompile I'd love to hear them. For now I'll go without.

Still, I need to know if I can open these databases in hidden mode so that this app can run in the background.
 
Last edited:
Since you are not using Access 2003 then you can repair and compact a db with the "same" command line option switches besides each other... /repair /compact.
"C:\Program Files\Microsoft Office\OfficeXP\msaccess.exe" "X:\YourDB.mdb" /repair /compact

Here is the VBA command to compile all modules...
Code:
DoCmd.RunCommand acCmdCompileAllModules
 
I need to update my signature. I just started using Access 2003. This database is one I have converted from 97.

Also, I've seen the commands to do the compiling from within the database. However, in this case I'm actually looping through records of all my databases that are included in a certain "profile" that I create (profiles contain databases and maintenance tasks to perform). So I'm using this database to backup/compact/repair/recompile all my other ones.
 
Last edited:
You could set up a rountine in the db [when it is opened] to check if it is not in the compiled state and then run the command to compile it. Check the Access help files for the IsCompiled Property if you need more info.
 
That would work, however, I'm wanting to keep this app completely independent of my other databases...and to be honest I really just don't want to put a new routine in 113 databases that are done in all flavors of Access.
 
Not bad. I like it. However, mine has much more functionality built in. In fact, I'm thinking maybe I should clean it up and post it somewhere for download, maybe even start a site for it. I know this is off topic, but you think you'd be interested in seeing it? It does all of the following (some things it does better than others).

1. Schedules backups, compactions, and repairs both monitored and in the background (although still trying to work some bugs out on the background part).

2. Maintains a list of databases which contains relevant information including documentation links, Jet version, description, users, etc.

3. A database messenger/automatic shutdown utility (requires some modifications to the client database).

4. A task list for each database...with the ability to switch between normal and task modes (task mode limits the database list to those with attached tasks that are incomplete).

5. Enable/disable startup features for any database (currently I only use it for shit bypass setting)

6. Manages utilities/code to some extent. I just have a form that shows certain files that I can reference for some purpose.

7. Creates and/or emails RT shortcuts

8. Has a built-in LDB viewer

However, it definitely needs cleaned up before I'd let anyone see it :rolleyes:
 
Last edited:

Users who are viewing this thread

Back
Top Bottom