Solved

Access 2003 Crashes on reference to TableDefs - Why?

Posted on 2006-10-27
8
664 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

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.

Join & Write a Comment

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…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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…

747 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

15 Experts available now in Live!

Get 1:1 Help Now