Solved

Close if table is opened

Posted on 2011-09-23
5
266 Views
Last Modified: 2012-08-14

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
Comment
Question by:Shanan212
5 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 36587718
... 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
 
LVL 74

Assisted Solution

by:Jeffrey Coachman
Jeffrey Coachman earned 250 total points
ID: 36587721
0
 
LVL 42

Accepted Solution

by:
dqmq earned 250 total points
ID: 36587936
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
 
LVL 13

Assisted Solution

by:Shanan212
Shanan212 earned 0 total points
ID: 36587960
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
 
LVL 13

Author Closing Comment

by:Shanan212
ID: 36715357
Found better solution via search
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question