Simon Staton
Simon Staton

Reputation: 4505

VBA close access after all functions finished

I have got vba which I am running from a batch that starts a macro to execute them, I am trying to close down access once all the functions have finished however making a new function and placing "Application.Quit" is closing the application before the functions have finished.

Anyone have any ideas?

Thanks, Simon

Edit:

Public Function finish()

    Application.Quit

End Function

Upvotes: 0

Views: 2625

Answers (2)

Christian Specht
Christian Specht

Reputation: 36421

Honestly, I don't understand what exactly you are doing so that your application closes before all your functions have finished.

If you want to execute some functions and then close the app, there are two ways how you can do it:

  1. Put several RunCode actions in the macro, each one executing one of your functions and the last one executing Application.Quit.

  2. Write a "runner" function that executes all the others and then quits:

    Public Function F1()
        'do stuff
    End Function
    
    Public Function F2()
        'do stuff
    End Function
    
    Public Function Finish()
        Application.Quit
    End Function
    
    'execute this function from the macro:
    Public Function Runner()
        F1
        F2
        Finish 'or directly call "Application.Quit" here instead
    End Function
    

No matter which of these ways you choose, Access executes the functions in the specified order, one after the other...and it shouldn't close before everything is finished!

If it does, I suppose you're doing something different.
As already suggested by shahkalpesh in his comment - show us more code, then.

Upvotes: 0

Johnny Bones
Johnny Bones

Reputation: 8402

Put a timer loop in your code to put the DB to sleep and give it time for the functions to run.

First, put this just under the Option Compare or Option Explicit line in any MODULE (NOTE: it must be in a proper module, and not a form's module):

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Then your function should look like this:

Public Function finish()

    Dim X as Integer

    For X = 1 To 10
        Sleep 1000
        DoEvents
    Next X

Application.Quit
End Function

Upvotes: 1

Related Questions