Solved

Access 2003 Crashes on reference to TableDefs - Why?

Posted on 2006-10-27
8
709 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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
 
LVL 85

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 85
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

Technology Partners: 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!

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

691 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