Solved

Access 2003 Crashes on reference to TableDefs - Why?

Posted on 2006-10-27
8
703 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

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.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

679 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