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

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

Access 2003 Crashes on reference to TableDefs - Why?

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
WilbertWaterbury
Asked:
WilbertWaterbury
  • 2
  • 2
  • 2
  • +2
1 Solution
 
rockiroadsCommented:
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
 
peter57rCommented:
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
 
GordonPrinceCommented:
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
Industry Leaders: 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!

 
GordonPrinceCommented:
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
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
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
 
WilbertWaterburyAuthor Commented:
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
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
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
 
WilbertWaterburyAuthor Commented:
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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

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