Results 1 to 8 of 8
  1. #1
    chalupabatman is offline Competent Performer
    Windows 10 Access 2013 64bit
    Join Date
    Aug 2016
    Posts
    114

    Iterate Array And Delete If Exists

    I am attempting to set an array of table and or query names and pass to a function to delete. This is my syntax - but it is not functioning properly - how would I change this to work right?



    Code:
    Function CheckIfExist()
       tableArray = Array("ExportForWorksheet", "ExportForWorksheetNonInventory", "_LocalTable")
       
       'Iterating the array to pass each one individually to the Delete function
       For amx = LBound(tableArray) To UBound(tableArray)
       Debug.Print CStr(tableArray)
            If TableExists(CStr(tableArray)) Then
                With db.TableDefs
                    .Delete CStr(tableArray)
                    .Refresh
                End With
            End If
       Next
    End Function
      
    Public Function TableExists(strName As String) As Boolean
    
    
       On Error GoTo HandleErr
    
    
       Dim db As DAO.Database, tDef As DAO.TableDef
       
       Set db = CurrentDb
       
       TableExists = False
       
       For Each tDef In db.TableDefs
          If tDef.Name = strName Then
             TableExists = True
             Exit For
          End If
       Next tDef
    
    
       For Each qDef In db.QueryDefs
        If qDef.Name = strName Then
            TableExists = True
            Exit For
        End If
       Next qDef
    
    
    ExitFunction:
       db.Close
       Set db = Nothing
       Exit Function
       
    HandleErr:
       TableExists = False
       Resume ExitFunction
       
    End Function

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    there's no need for arrays in access... everything is a table or collection.
    loop thru the table and delete

    for i = 0 to lstTbls.listcount-1
    or
    while not rst.eof
    or
    set colTbls = new collection
    for each tbl in colTbls

  3. #3
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    there's no need for arrays in access
    Can't say I agree with that. Can't disagree that the goal might be simpler (as suggested) either.
    Chalupabatman: "it is not functioning properly" is of little help to anyone here. Often, the major issue in such an exercise using arrays is that after a delete the array size has been altered thus the loop counter is reduced (For amx =...). In some cases, the array as to be declared as Static so as to maintain the count.

    In your case, I suspect the array approach is because you don't have a list or recordset of the table values you want to delete. You could build one so that you can iterate over the tables collection as suggested, or try making the array static. To prove or disprove that this is the cause, check the array size as you step through using either a debug or msgbox statement. I think in your case it probably isn't since I don't see anything definite that would affect the array size, but I'm not sure - you don't provide much that explains the failure.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    chalupabatman is offline Competent Performer
    Windows 10 Access 2013 64bit
    Join Date
    Aug 2016
    Posts
    114
    Quote Originally Posted by Micron View Post
    Can't say I agree with that. Can't disagree that the goal might be simpler (as suggested) either.
    Chalupabatman: "it is not functioning properly" is of little help to anyone here. Often, the major issue in such an exercise using arrays is that after a delete the array size has been altered thus the loop counter is reduced (For amx =...). In some cases, the array as to be declared as Static so as to maintain the count.

    In your case, I suspect the array approach is because you don't have a list or recordset of the table values you want to delete. You could build one so that you can iterate over the tables collection as suggested, or try making the array static. To prove or disprove that this is the cause, check the array size as you step through using either a debug or msgbox statement. I think in your case it probably isn't since I don't see anything definite that would affect the array size, but I'm not sure - you don't provide much that explains the failure.
    So sorry I thought I included the error I was receiving. I get a type mismatch error on this line
    Code:
    If TableExists(CStr(tableArray)) Then

  5. #5
    chalupabatman is offline Competent Performer
    Windows 10 Access 2013 64bit
    Join Date
    Aug 2016
    Posts
    114
    Quote Originally Posted by ranman256 View Post
    there's no need for arrays in access... everything is a table or collection.
    loop thru the table and delete

    for i = 0 to lstTbls.listcount-1
    or
    while not rst.eof
    or
    set colTbls = new collection
    for each tbl in colTbls
    I do not follow....out of all the table names in the database, I only want to delete 3. How would I specify those 3 table names w/ no array?

  6. #6
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    You are probably trying to pass an invalid type to the function. Likely Null. Put a break on the line and when it stops there, check in the immediate window:
    ?tablearray and hit return. What do you get?

  7. #7
    chalupabatman is offline Competent Performer
    Windows 10 Access 2013 64bit
    Join Date
    Aug 2016
    Posts
    114
    Quote Originally Posted by Micron View Post
    You are probably trying to pass an invalid type to the function. Likely Null. Put a break on the line and when it stops there, check in the immediate window:
    ?tablearray and hit return. What do you get?
    If I type in the Immediate window ?tableArray - I get a Run-time error '13': Type mismatch

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Why are you using code to delete tables?

    Can't display array object in immediate window. Your code is referencing the array object, not its elements. Reference array elements by index.

    If TableExists(tableArray(amx)) Then
    ...
    .Delete tableArray(amx)
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Delete from Query B where record exists in Query A
    By patchesohouli in forum Queries
    Replies: 3
    Last Post: 04-29-2016, 01:04 PM
  2. Replies: 2
    Last Post: 01-31-2016, 08:47 PM
  3. Replies: 3
    Last Post: 10-19-2012, 04:30 PM
  4. How to iterate through a custom collection
    By vicrauch in forum Programming
    Replies: 6
    Last Post: 07-21-2011, 02:51 PM
  5. How to tell if a file exists and then delete it
    By orcinus in forum Programming
    Replies: 3
    Last Post: 05-17-2010, 05:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums