Solved

Access 2003 Crashes on reference to TableDefs - Why?

Posted on 2006-10-27
8
676 Views
Last Modified: 2012-06-21
This code called from a form field change event causes Access 2003 to crash.  The DAO 3.6 is enabled.

Sub HideUnhideTable(txtTableName As String, bolEnable As Boolean)
    Dim tdf As TableDef
     For Each tdf In CurrentDb.TableDefs '     <--This statement crashes
           If (tdf.Name = txtTableName) Then
                  If bolEnable Then tdf.Attributes = 0 Else tdf.Attributes = dbHiddenObject
            End If
      Next tdf
End Sub

Many tables exist in the database. Why would this cause Access to crash. 'Found nothing in M$ knowledge base.
0
Comment
Question by:WilbertWaterbury
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 17817826
Does it make a difference if u define tdf as dao.tabledef?

Dim tdf as DAO.TableDef


U might want to check references also, r u using the latest DAO Object Library?
in vba window, go tools/references
0
 
LVL 77

Expert Comment

by:peter57r
ID: 17818056
Hi WilbertWaterbury,
There is nothing wrong with your code.
And the fact that there is no error on the previous line suggests the DAO ref is present.
If you have added any non-standard refs then the 'dao.' qualification might be worthwhile but I can't quite imagine what other reference would also have a tabledef object.
I'd be inclined to cut the code out to a Word doc, delete it from the module and then paste it back in again. (Last resort or what?(:-))


Pete
0
 
LVL 4

Expert Comment

by:GordonPrince
ID: 17818461
Does this lead anywhere?

        Debug.Print "Attributes of fields in " & .TableDefs(0).Name & " table:"
        For Each fldLoop In .TableDefs(0).Fields
            Debug.Print "  " & fldLoop.Name & " = " & _
                fldLoop.Attributes
        Next fldLoop
0
 
LVL 4

Expert Comment

by:GordonPrince
ID: 17818587
I don't even get the table's attributes to make sense. E.g.

  Dim tdf As DAO.TableDef
    Debug.Print "Begin output ----------------------------------------"
    For Each tdf In CurrentDb.TableDefs
        Debug.Print "Attributes of table " & tdf.Name & " are: " & tdf.Attributes
    Next tdf

The hidden tables and the not hidden tables both show up as tdf.Attributes = 0

So I'm missing something here, I think. Sorry for the clutter.
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 250 total points
ID: 17822552
Try setting a variable for the Database:

dim dbs as DAO.Databse

Set dbs = Currentdb

for each tdf in dbs.tabledefs
etc etc

I'd advise you to be very careful when setting attributes for tables ... the way you're going about doing this, you would erase ALL the attibutes of your table except for the Hidden value ... instead, you'd probably want to OR them together:

If (tdf.Name = txtTableName) Then
                  If bolEnable Then tdf.Attributes = tdf.Attributes OR NOT dbHiddenObject Else tdf.Attributes = tdf.Attributes OR dbHiddenObject
            End If
0
 

Author Comment

by:WilbertWaterbury
ID: 17824709
Scott - One minor correction:

If (tdf.Name = txtTableName) Then
                  If bolEnable Then tdf.Attributes = tdf.Attributes AND NOT dbHiddenObject Else tdf.Attributes = tdf.Attributes OR dbHiddenObject
            End If

Thanks the "dim dbs as DOA.Database" and "Set dbs = Currnetdb" did the trick.
0
 
LVL 84
ID: 17825432
I don't believe that's correct, I believe you should use OR to add the attribute. Note this is the logical OR operator we're talking about, not the standard SQL-type operator, and it uses bitwise comparison to determine whether it needes to add the attrib or not, so tdf.Attributes OR NOT dbHiddenObject would/should remove only that attribute, and no others. Of course, I could be wrong, but the only way to determine that would be to (a) check ALL attributes before issuing this statement and then (b) check ALL attributes after issuing the command ... then compare (a) to (b) and see what is changed.

And again - I seriously caution you against doing this. Most tables have something like 30+ attributes which must co-habitat peacefully in order for you to have a stable, working system. Micheal Kaplan, who was on the Access development team, has this to say about it in a newsgroup posting which featured code almost identical to yours:

"I would not ever recommend using this code as there are seriously nasty issues with objects you hide this way..."

If you're using 2000+, you can use SetHiddenAttribute to do this:

Application.SetHiddenAttribute acTable,"Customers", True

So you'd do this:

If tdf.Name = txtTableName then SetHiddenAttribute(acTable, txtTAbleName,True)

There's also GetHiddenAttribute to retrieve the property.
0
 

Author Comment

by:WilbertWaterbury
ID: 17826554
You said:

"so tdf.Attributes OR NOT dbHiddenObject would/should remove only that attribute, and no others"

A bitwise OR will not remove bits only add them or leave them alone if one operand is zero.  The bitwise AND with the inverse of dbHiddenObject (NOT dbHiddenObject) will remove onlly that bit. This should not cause any problem.

Your code failed with an 'invalid value' error, my works.  I'm not sure where we are miscommunicating.

Thanks for the information about Set./GetHiddenAttribute.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now