Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to close MS access in Macro?

Status
Not open for further replies.

101287

MIS
Apr 8, 2006
189
US
I have a macro that is schedule to run every day to copy a database. However, when I execute the macro it does not allow me to close access. Access is open to execute the macro but would like to close it automatically (as part of this macro). I added a QUIT macro but give me an error. Does any can help me to complete this macro and be able to close Access. The code is below.

Thank you for guidance and collaboration.

Luis

Function CopyITPlanningDatabase()

' Using the Scripting.FileSystemObject to copy files
' Requires a reference to Microsoft Scripting Runtime Library
' In any code window Menu > Tools > References
' Microsoft Scripting Runtime Library - Checked

Dim strSourceFile As String
Dim strDestinationPath As String

Dim objFso As Scripting.FileSystemObject

' Set the target file path
strSourceFile = "\\FTWSS02\BIGBUCKS\itplan-copy\itplan.mdb"

' Set the target file path
strDestinationPath = "\\ftwgroups\jsf\jsfitcoord\acWKspace\F-35 Dash Board\"

' Instantiate instance of Scripting file system object
Set objFso = New Scripting.FileSystemObject

' Check that the source file exists
If objFso.FileExists(strSourceFile) Then

' Check that the destination folder exists.
If objFso.FolderExists(Mid(strDestinationPath, 1, InStrRev(strDestinationPath, "\", , vbTextCompare))) Then

' Copy the file, overwriting existing file of same name...
strDestinationPath = strDestinationPath & "LRitplan.mdb"
objFso.CopyFile strSourceFile, strDestinationPath, True
Else
' maybe use objFso.Buildpath to create the folder
' depends how bulletproof you want to make this.

End If

End If
DoEvents
' Destroy instantiated objects
Set objFso = Nothing

End Function
 
Code:
...
Set objFso = Nothing
docmd.quit
End Function

FYI: You can set your code apart as I did by wrapping it in [ignore]
Code:
[/ignore] tags.

[tt]_____
[blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ 181-2886 before posting.
 
Included the code you mentioned.
Code:
Set objFso = Nothing 
 docmd.quit 
 End Function

However, when execute the macro with the code provided I get the following error:

Macro action cancelled. "You used a method of the DoCmd object to carry out an action in Visual Basic, but then clicked Cancel in a dialog box. For example, you used the Close method to close a changed form, then clicked Cancel in the dialog box that asks if you want to save the changes?

Other suggestions, please

 
Did it give you a dialog box? Did you press cancel?



[tt]_____
[blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ 181-2886 before posting.
 
You can also try application.quit

[tt]_____
[blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ 181-2886 before posting.
 
The message says: Macro action cancelled. "You used a method of the DoCmd object to carry out an action in Visual Basic, but then clicked Cancel in a dialog box. For
example, you used the Close method to close a changed form, then clicked
Cancel in the dialog box that asks if you want to save the changes?

 
Ok... this is a MS Access vba function, right? Why is it a function? so you run it from a macro?

if so... it should be working... usually that error (from what I've seen) means that something else is still running, and it won't let you close the database...

if you look at the code for the switchboard, it would probably help...

Also... if this is the only thing that database does, you can put the code on a form, have the form autoload, which runs the code, then it would probably work....

or, if you want to have it run at a specific time, you could use a form with a timer event, that runs and checks the time, then runs the code at the right time...

other than knowing how the code is run, and what else is open/running when that code is being run, I can't see what the problem is...

GComyn
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top