473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Error accessing fields in a tabledef

Before I state my problem I want to say thanks to anyone who reads this post. I appreciate your time.

I have a simple form in an Access 2007 database. The form contains two combo boxes. Combo box 1 contains the names of all the base and linked tables in the database.

When the user selects a table name in combo box 1, I want combo box 2 show the list of fields for that table.

The code I have to populate combo box 2 with field names looks like this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Combo_Box_1_Change()
  2. Dim FLD As String
  3. Dim INDEXtdef As DAO.TableDef
  4. Dim VarItm As Variant
  5.  
  6.     'Initiate the FLD string variable
  7.     FLD = ""
  8.  
  9.     Set INDEXtdef = CurrentDb.TableDefs(Combo_Box_1.Value)
  10.     For Each VarItm In INDEXtdef.Fields
  11.  
  12.         'Build the string variable
  13.         FLD = FLD & VarItm.Name & "; "
  14.  
  15.     Next
  16.  
  17.     'Trim off the extra semi-colon and space
  18.     FLD = Left(FLD, Len(FLD) - 2)
  19.  
  20.     Combo_Box_2.RowSource = FLD
  21.  
  22. End Sub
Problem is I get an run-time error 3420 ("Object invalid or no longer set"). When I Debug, the offending line is:

Expand|Select|Wrap|Line Numbers
  1. For Each VarItm In INDEXtdef.Fields
Can anyone point out what I'm doing wrong? I've looked at the code for too long and I can't see what's wrong with it.

Thanks,
sphinney
Sep 17 '08 #1
5 5579
FishVal
2,653 Expert 2GB
Hello, sphiney.

You've already asked a similar question.
Me answer is still the same. From some reasons Access doesn't allow to hold a reference to DAO.Tabledef, DAO.Field etc objects unless they has been just created and not yet added to a correspondent collection.

You should call Tabledefs property each time you want to call nested properties/methods.

Kind regards,
Fish
Sep 17 '08 #2
ADezii
8,834 Expert 8TB
Just a slight change in syntax will do the trick:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Combo_Box_1_AfterUpdate()
  2. Dim MyDB As DAO.Database
  3. Dim INDEXtdef As DAO.TableDef
  4. Dim fld As DAO.Field
  5. Dim strFldName As String
  6.  
  7. 'Must contain a Table Name
  8. If IsNull(Me![Combo_Box_1]) Then Exit Sub
  9.  
  10. Set MyDB = CurrentDb()
  11.  
  12. 'Initiate the strFldName String Variable
  13. strFldName = ""
  14.  
  15. Set INDEXtdef = MyDB.TableDefs(Me![Combo_Box_1])
  16.  
  17. For Each fld In INDEXtdef.Fields
  18.   'Build the string variable
  19.   strFldName = strFldName & fld.Name & ";"
  20. Next
  21.  
  22. 'Trim off the extra semi-colon
  23. strFldName = Left(strFldName, Len(strFldName) - 1)
  24.  
  25. 'Define certain characteristics of the 2nd Combo Box
  26. With Me![Combo_Box_2]
  27.   .ColumnCount = 1
  28.   .RowSourceType = "Value List"
  29.   .RowSource = strFieldName
  30. End With
  31.  
  32. Combo_Box_2.RowSource = strFldName
  33. End Sub
Sep 18 '08 #3
Just a slight change in syntax will do the trick:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Combo_Box_1_AfterUpdate()
  2. Dim MyDB As DAO.Database
  3. Dim INDEXtdef As DAO.TableDef
  4. Dim fld As DAO.Field
  5. Dim strFldName As String
  6.  
  7. 'Must contain a Table Name
  8. If IsNull(Me![Combo_Box_1]) Then Exit Sub
  9.  
  10. Set MyDB = CurrentDb()
  11.  
  12. 'Initiate the strFldName String Variable
  13. strFldName = ""
  14.  
  15. Set INDEXtdef = MyDB.TableDefs(Me![Combo_Box_1])
  16.  
  17. For Each fld In INDEXtdef.Fields
  18.   'Build the string variable
  19.   strFldName = strFldName & fld.Name & ";"
  20. Next
  21.  
  22. 'Trim off the extra semi-colon
  23. strFldName = Left(strFldName, Len(strFldName) - 1)
  24.  
  25. 'Define certain characteristics of the 2nd Combo Box
  26. With Me![Combo_Box_2]
  27.   .ColumnCount = 1
  28.   .RowSourceType = "Value List"
  29.   .RowSource = strFieldName
  30. End With
  31.  
  32. Combo_Box_2.RowSource = strFldName
  33. End Sub
ADezii - That was it! Apparently it must have been setting a reference to the CurrentDb that did the trick. Thanks!

FishVal - My appologies. I didn't even remember posting a similar question in the past. I will definitely be more thorough in the future in searching previous posts to this forum. Thanks for taking the time to read my post and responding.

Sincerely,
sphinney
Sep 18 '08 #4
FishVal
2,653 Expert 2GB
Oops.

ADezii, do you have any thoughts about that behavior of Application.CurrentDb method?

BTW,
DBEngine.Workspaces(0).Databases(0).TableDefs(...)
or omitting defaults
DBEngine(0)(0).Tabledefs(...)

gives stable reference to Tabledef object too.

Regards,
Fish
Sep 18 '08 #5
ADezii
8,834 Expert 8TB
Oops.

ADezii, do you have any thoughts about that behavior of Application.CurrentDb method?

BTW,
DBEngine.Workspaces(0).Databases(0).TableDefs(...)
or omitting defaults
DBEngine(0)(0).Tabledefs(...)

gives stable reference to Tabledef object too.

Regards,
Fish
ADezii, do you have any thoughts about that behavior of Application.CurrentDb method?
Hello FishVal, actually I've been aware of this peculiar behavior for some time and have compensated for it on many occasions. If you have a minute, read the following Thread, specifically Posts 5 thru 7, in which this same problem was discussed.
http://bytes.com/forum/thread834052-MyDB.html

P.S. - Nice Tip on DBEngine(0)(0).Tabledefs(...)
Sep 18 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tim Pascoe | last post by:
I'm trying to implement a sorting routine for a string array. The sort routine is from http://ourworld.compuserve.com/homepages/attac-cg/acgsoft.htm (a great selection of routines in the 'Sequence...
2
by: gssstuff | last post by:
I have a piece of code I use to compare two identically structured tables. There are 15+ sets of tables I am comparing. I am looking to see what has changed between the "old" and "new" versions...
3
by: Smriti Dev | last post by:
Hi There, I have the following code and when I try to run it, I get a type mismatch error. I would really appreciate your help with this. Thanks kindly, smriti --- Private Sub cmdOK_Click()
9
by: Jimbo | last post by:
Hello, I have a user request to build a form in an Access database where the user can check off specific fields to pull in a query. For example, let's say I have 10 fields in a table. The user...
3
by: MLH | last post by:
If Err.Number = 2001 Then I have the above line in a procedure I downloaded. I don't know what error that is exactly.
8
by: PW | last post by:
Hi, There is code in Alison Balter's excellant "Mastering Access 2003" to create a list of error codes and descriptions but it only generates error messages 3 through 94. Is there a website...
10
richardhodge
by: richardhodge | last post by:
I am a VB6 database programmer and have run into a small problem. The company I work for primarily uses Microsoft Access 2000 for the database that is the back end for our software. Well the...
3
by: alnug | last post by:
Hello, I'm trying to use the following code to read data from Excel files and put the data into an MS Access Table. Some of the Excel files have the tabs in the sequence Chart1, Sheet1, Sheet2 etc...
2
by: RLN | last post by:
Luke Chung at FMS Inc provide this link in another thread that provided a really helpful .pdf file error listing: http://www.fmsinc.com/MicrosoftAccess/Errors/ErrorNumberAccess2007-2000. pdf ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.