473,396 Members | 2,013 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.

why do you have to declare currentdb?

I recently made a very simple little function:
Function x()
Dim Tbl As TableDef
Dim fld As DAO.Field
Set Tbl = currentdb.TableDefs("search")
Debug.Print Tbl.Name
For Each fld In Tbl.Fields
Debug.Print fld.Name
Next fld

To my astonishment, it gave an error: object no longer set/not valid

Then I changed it to

Public Function xx()
Dim Tbl As TableDef
Dim fld As DAO.Field
Dim Dbs As Database

Set Dbs = CurrentDb

Set Tbl = Dbs.TableDefs("search")
Debug.Print Tbl.Name
For Each fld In Tbl.Fields
Debug.Print fld.Name
Next fld
End Function

and it worked.

Can someone tell me why you explicitly have to declare the current database?

TIA

- Nicolaas
Nov 13 '05 #1
2 3999
CurrentDb creates a new instance of the dbEngine(0)(0) after flushing all
collections. In the context you describe, this instance goes out of scope if
you do not point it to a variable.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"WindAndWaves" <ac****@ngaru.com> wrote in message
news:qU*******************@news.xtra.co.nz...
I recently made a very simple little function:
Function x()
Dim Tbl As TableDef
Dim fld As DAO.Field
Set Tbl = currentdb.TableDefs("search")
Debug.Print Tbl.Name
For Each fld In Tbl.Fields
Debug.Print fld.Name
Next fld

To my astonishment, it gave an error: object no longer set/not valid

Then I changed it to

Public Function xx()
Dim Tbl As TableDef
Dim fld As DAO.Field
Dim Dbs As Database

Set Dbs = CurrentDb

Set Tbl = Dbs.TableDefs("search")
Debug.Print Tbl.Name
For Each fld In Tbl.Fields
Debug.Print fld.Name
Next fld
End Function

and it worked.

Can someone tell me why you explicitly have to declare the current
database?

TIA

- Nicolaas

Nov 13 '05 #2
WindAndWaves wrote:
Can someone tell me why you explicitly have to declare the current database?


Because every invocation of CurrentDb returns a fresh pointer, which
does generally not point to the same object as before.
--
Bas Cost Budde, Holland
http://www.heuveltop.nl/BasCB/msac_index.html
I prefer human mail above automated so in my address
replace the queue with a tea
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Howard | last post by:
I am trying to use DoCmd.TranferSpreadsheet to import a spreadsheet into an Access table that's not the CurrentDB. I have the database open, but I don't see how to tell the TransferSpreadsheet...
5
by: Vladislav Moltchanov | last post by:
I am making data entry tool in Acc2K for several data collecting centres. It is convenient to have several files (mde file, .mdb with tables only etc) located in the same directory. To make...
4
by: polux | last post by:
Hi everyone, i'm getting lost here... used to work with Access97 now Access2002 I'm having trouble finding currentdb Following references are checked: vb for applications
4
by: paii, Ron | last post by:
Are the following 2 bits of code equivalent. I want to use ws for transactions. Dim ws as Workspace Dim db as Database Dim rs as Recordset Set ws = DBEngine.Workspaces(0) Set db =...
1
by: jnikle | last post by:
I have a parameter query named "qry_employee_info_reports" that I need to run in the OnOpen event of a form. I'm after its total number of records. The query's got several joins in it, and one of...
7
by: Tony M. | last post by:
I'm trying to execute an append query. I have a case that works, and one that returns an object not set error. THIS WORKS: Private Sub cmdArchiveRecs_Click() BeginTrans CurrentDb.Execute...
3
by: Richard Hollenbeck | last post by:
In DAO, is there any difference between "set db = CurrentDb()" with parentheses and "set db = CurrentDb" without parentheses? I'm just looking for data from a single field in a table inside the...
1
by: sphinney | last post by:
As my Access form opens, I want it to find the names of the tables in the current Access database and populate a combobox with the table (recordset) names. Problem is, the CurrentDb.Recordsets...
1
MitchR
by: MitchR | last post by:
Hello Folks; I have run myself into a hole I cannot get of and need some assistance. I have a Form "Frm_ATandT_Activation_Center" that has a sub form "Frm_ATandT_Activation"....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.