Results 1 to 3 of 3
  1. #1
    2 Star Lounger
    Join Date
    Jun 2002
    Posts
    114
    Thanks
    0
    Thanked 0 Times in 0 Posts

    delete only if a module exists (Access 2000)

    I have a delete code that delets a module,something like that

    DoCmd.DeleteObject acModule, "Up"

    This code must function only if a module named "Up" exists"
    otherwise the code should not disturb the other functions.How can i do it?

  2. #2
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Sint Niklaas, Belgium
    Posts
    2,778
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: delete only if a module exists (Access 2000)

    The easiest way to do this is add a on error statement.
    On Error Resume Next
    DoCmd.DeleteObject acModule, "Up"
    On Error Goto ... (fill in here the name of normal on error function)
    Francois

  3. #3
    Silver Lounger GARYPSWANSON's Avatar
    Join Date
    Aug 2001
    Location
    Frederick, Maryland, USA
    Posts
    1,788
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: delete only if a module exists (Access 2000)

    Another option would be to create a function to test for the name. The below example tests for the name of a table and deletes the table if it exists.


    Function TableExist(dbname As String, tname As String) As Integer

    'Determines if a table exists in the current database
    'Function returns true (-1) it table exists, false (0)if table does not exist

    'TName is the Name of the Table being tested

    Dim Db As DAO.Database
    Dim Found As Integer ' Value is True (-1) if table exists
    Dim Test As String ' Use for Name of Table being tested

    Set Db = CurrentDb()
    Const NAME_NOT_IN_COLLECTION = 3265

    ' Assume table does not exist
    Found = False

    ' Trap for any errors
    On Error Resume Next

    'Test to see if table exists
    Test = Db.TableDefs(tname).NAME
    If Err <> NAME_NOT_IN_COLLECTION Then Found = True

    'If Found = True Then
    'MsgBox Found, , "table found"
    'Else: MsgBox Found, , "TABLE NOT FOUND"
    'End If

    ' Reset error variable
    Err = 0

    TableExist = Found

    End Function

    ..... you would then use the following code to test to see if the object, in this case table, exists and complete an action.


    Test = TableExist("", "BASIS OF ESTIMATE")
    If Test = -1 Then DoCmd.DeleteObject acTable, "BASIS OF ESTIMATE"


    HTH - Of course this is slightly more complicated the Francois response but once the function is in use, the code is simple.
    Regards,

    Gary
    (It's been a while!)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •