Solved

Close if table is opened

Posted on 2011-09-23
5
274 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

837 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