Select to view content in your preferred language

Code won't run twice - Run time error 462

1337
1
09-27-2010 09:55 AM
GabrielUpchurch
New Contributor
I am trying to create a command button that when clicked does the following:

1) Automatically closes any MS Access applications that are running.
2) Exports records for selected features to a dbf, overwriting any existing data in the dbf.
3) Automatically opens Access and a particular report.

I need to close Access before the rest of the code runs because I get an error with my geoprocessing tool if the code has been run previously and Access is still open.  The problem I am encountering is that my code runs fine the first time it is executed but if I try to run it again I receive the following error:

"Run-time error '462': The remote server machine does not exist or is unavailable."

I have tried to resolve the problem and think it has to do with non-explicit referencing but I can't figure out how to solve it.  The issue seems to arise with the first procedure for quitting Access (see code below).  I am very new to programming so my code can probably be improved significantly considering that it is just a patchwork of other people's code I found on the net.  Any guidance people can provide will be greatly appreciated.

Private Sub ExportPlots_Click()

    'Create the Access object
    Dim oAccess As Access.Application
    Set oAccess = Access.Application
   
    'Quit Access
    oAccess.Quit '<--This is where the error occurs the second time the code is run.

    'Release Access Object
    Set oAccess = Nothing
   
    'Create the Geoprocessor object
    Dim GP As Object
    Set GP = CreateObject("esriGeoprocessing.GpDispatch.1")
          
    'Set the toolbox
    GP.Toolbox = "C:\My Documents\Monitoring\Gabes_Toolbox.tbx"
          
    'Execute tool
    GP.ExportPlotsTool "plot_locations", "C:\My Documents\Monitoring\Table1.dbf"
       
    'New instance of Access
    Set oAccess = New Access.Application
          
    'Create Report object
    Dim sReport As String
    sReport = "Cover_By_Life_Form"
          
    'Open Vegpolots db
    oAccess.Visible = True
    oAccess.OpenCurrentDatabase _
    "C:\My Documents\Monitoring\east_ca_vegplots_082310_Gabes.mdb", False
          
    'Open report
    oAccess.DoCmd.OpenReport sReport, acViewReport, , , acWindowNormal
          
    'Cleanup
    Set oAccess = Nothing
    Set GP = Nothing

End Sub
0 Kudos
1 Reply
HeribertoMantilla_Santamaría
Deactivated User
Hi.

Try this:

On Error Goto   AccessClosed
'Create the Access object 
Dim oAccess As Access.Application 
Set oAccess = Access.Application 

'Quit Access 
oAccess.Quit '<--This is where the error occurs the second time the code is run. 

'Release Access Object 
Set oAccess = Nothing 

AccessClosed:

On Error Resume Next


If the code fails the script continue in the Next Line of the bug.

In others words after AccessClosed:

Now if VBA support API's you can use PostMessage Function and Kill the Access Process. I attached an example.

[ATTACH]2771[/ATTACH]
0 Kudos