473,802 Members | 2,332 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Table Defs("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 4031
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.

"WindAndWav es" <ac****@ngaru.c om> wrote in message
news:qU******** ***********@new s.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.Table Defs("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
4962
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 command that the table is not in the CurrentDB. Is there a way to specify a filename and table within the table string of the TransferSpreadsheet command? Is there a way of temporarily changing the CurrentDB to be the other database so the...
5
8277
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 linkage automatically it is necessary to get the directory where all these files are located. One way is to supply it through interaction with local user. However, it would be easier to have reliable way to get this infor automatically. Does Somebody...
4
3788
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
8915
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 = ws.Databases(0)
1
10824
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 them is to query "qry_last_transition," which is also a parameter query. Both querys use the same parameter: a control called "txtSecondDate" on a pop up form. What I've been trying to do up to this point is to open up this parameter form,...
7
5006
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 "qapp_RecsNotYetArchived", dbFailOnError CommitTrans End Sub
3
8633
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 same Access database, if that means anything. I forgot to put the blank parentheses but I'm not finished building the recordset. I'm betting it will work just fine. Why and when (and what) would anything go inside those parentheses?
1
3743
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 object is empty even though I have two tables in the database. I'm using the code below, but the combobox remains empty. That's because CurrentDB.Recordsets is empty. Is this a bug with CurrentDb or have I done something dumb? Dim TBL As...
1
3117
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". Frm_ATandT_Activation_Center is standard in Form View with no Master or Child links to the Sub form. this allows me to show available SIM ID"S in inventory for activation. The Subform diplays as a datasheet that has a Query as the control source. So when the user opens...
0
9559
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10529
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10280
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9107
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7596
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6835
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5620
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4268
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3788
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.