mercredi 1 août 2018

DoCmd.DeleteObject acTable - The record ... specified on this form or report does not exist

Let me start by saying I am not at all familiar with Access or VBA. However, I am the IT guy and at some point someone create an MS Access database that does a thing and now I have to support it.

We have a database that upon opening deletes any old data and re-runs the external query that makes this application work. Occasionally whatever state the program exited out in that table already does not exist. This causes MS Access to hang on the delete line and I have to run the debugger, comment out the DoCmd.DeleteObject line, re run, and then un-comment the line to let the user continue with their day.

I want to add in some sort of conditional statement, but anything I've been able to google in terms of If...Then statements or 'TableExist' type functions always causes an error to be thrown that I haven't defined the function. My best guess is I'm nesting this incorrectly or I'm not calling some sort of external function correctly, but as I said my VBA knowledge is extremely limited.

This code executes on startup:

Public Function Startup() As Integer

    DoCmd.Close acForm, "soLookup"
    DoCmd.DeleteObject acTable, "sales_order"
    DoCmd.RunSavedImportExport "Import-sales_order"
    DoCmd.OpenForm "soLookup"

End Function

Its the

DoCmd.DeleteObject acTable, "sales_order"

Line that causes things to fail.

I've attempted to restructure this several times based on several examples, but I'll only bother with one below

Public Function Startup() As Integer

    DoCmd.Close acForm, "soLookup"
    If TableExists("sales_orders") Then
      DoCmd.DeleteObject acTable, "sales_orders"
    Else    
      'Do nothing
    End If 
    DoCmd.RunSavedImportExport "Import-sales_order"
    DoCmd.OpenForm "soLookup"

End Function

Nothing I seem to try seems to give me any result other than an error of some sort. All I want to do is add a conditional statement to this 'Startup' bit that checks if the "sales_order" table even if exists, and if it doesn't, then to just move on to the next comment and forget the DoCmd.DeleteObject. How can I achieve this functionality?! Thanks!

Aucun commentaire:

Enregistrer un commentaire