[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

Close if table is opened


Dim td As TableDef
    For Each td In CurrentDb.TableDefs
        If td.Name = stLocalTableName Then
            CurrentDb.TableDefs.Delete stLocalTableName
        End If
    Next

Open in new window


Hi I have gotten the following code which is said to close the open tables only. But sometimes my tables get closed and I suspect this code!

Is this code actually deleting open tables?
Is there anyway to amend it to close a table if its open?
0
Shanan212
Asked:
Shanan212
3 Solutions
 
peter57rCommented:
... code which is said to close the open tables only. But sometimes my tables get closed ...

??what??

This code is deleting a table whose name is in the variable stLocalTableName
0
 
dqmqCommented:
That's stupid code.  It loops through all tabledefs and deletes at most one, regardless of whether it is open.  This code closes all tables:


   Dim td As TableDef
    For Each td In CurrentDb.TableDefs
          docmd.close actable, td.name
    Next
0
 
Shanan212Author Commented:
Thanks dgmg and I used this (as this checks if the table is open)

Sub closeObj()
Dim td As DAO.TableDef
For Each td In CurrentDb.TableDefs
    If IsOpen(td.Name, acTable) Then
        DoCmd.Close acTable, td.Name
    End If
Next
End Sub

Function IsOpen(strname As String, strtype As String) As Boolean
    If SysCmd(acSysCmdGetObjectState, strtype, strname) <> 0 Then
   
        IsOpen = True
   
    End If
End Function

0
 
Shanan212Author Commented:
Found better solution via search
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now