CurrentDB VBA Access - learnit

Home Top Ad

Post Top Ad

Sunday, March 28, 2021

CurrentDB VBA Access

CurrentDB VBA Access

Database and CurrentDB VBA Access

What is Database?

A database is a data structure that stores organized information. Most databases contain multiple tables, which may each include several different fields. For example, a company database may include tables for products, employees, and financial records. Each of these tables would have different fields that are relevant to the information stored in the table.


CurrentDb() Function

Access always maintains a single permanent reference to the current database. The first member of the Databases collection is populated with a reference to the current database at startup. This reference, pointed to by DB Engine (0) (0), is fine under most circumstances, but when, for example, you are working on wizards, it is not always up-to-date. In these circumstances, it is possible for the first database collection member to point to something other than the default database. The chance of this occurring in normal databases is negligible, but to ensure that you are working with the current database, you need to execute the Refresh method, which rebuilds the collection, placing the current database in the first position in the Databases collection. This can be annoying, of course, , but in addition, your code experiences a huge performance hit every time you want to use the current database.


The solution that Microsoft came up with was to provide the CurrentDb() function. CurrentDb (the parentheses are optional) is not an object; it is a built-in function that provides a reference to the current user's default database. Although they do refer to the same database, it is essential that you understand two important concepts.


CurrentDb and DBEngine(0)(0) are not the same objects internally. Access maintains a single permanent reference to the current database, but CurrentDb temporarily creates a new internal object - one in which the collections are guaranteed to be up-to-date.


When CurrentDb is executed, Access creates a new internal object that recreates the hierarchy and refers to the current database. The interesting fact is that immediately after CurrentDb executes and returns a pointer, the internal object is destroyed.

Example

Private Sub cmdAppendQuery_Click()
Dim cquery As New QueryDef
cquery.SQL = "Select Userid,password from UserLogin"
cquery.Name = "UserLoginDetail"
CurrentDb.QueryDefs.Append cquery
End Sub

Please Watching My Video is Below

No comments:

Post a Comment

Post Top Ad