Jump: Search:

Total Visual SourceBook

Total Visual SourceBook CD and Printed Manual

Microsoft Access 2007/2010 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office 2007 and VB6

Supports Access/Office 2010 (32 bit), 2007, 2003, 2002, 2000, and
Visual Basic 6.0!

Also available for:
Access 97/95


View all FMS products for Microsoft AccessAll Our Microsoft Access Products

SourceBook Info:

Why SourceBook?

 

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

 

 

 

 

 
Royalty free source code library modules for Microsoft Access, VB6, VBA, OfficeRoyalty free source code library modules for Microsoft Access, VB6, VBA, OfficeVBA and VB6 Source Code Library Download the free trial version of Total Visual SourceBook for Microsoft Access, Office, VB6 and VBA
Category: Database : Access/Jet
Description: Microsoft Access Jet databases and their objects (MDB and ACCDB database formats)

Class: CJetDatabase

Using DAO with Microsoft Access Jet database objects

Microsoft Jet contains a versatile Database object that represents an open database. The term "database" is a broad term that can represent any one of the following: - A Jet or Access Database (.mdb, .mda) - A Jet workgroup information file (.mda, .mdw) - An ODBC database, represented by a DSN - A file system directory, such as a directory of xBASE or Paradox files This class works with DAO/Jet objects at the database level. The class itself maps on top of a Jet/Access database and provides methods and properties for working with the objects contained in that database.

Procedure Name

Type

Description

(Declarations) Declarations Declarations and private variables for the CJetDatabase class
AccessVersion Property Gets the version of Microsoft Access used to create or open the current database. When Microsoft Access creates a database, it adds many Access-specific objects and properties. Included is the AccessVersion property which identifies the version of Access that created or last accessed the database.
Database Property Gets a handle to the current open database. The Database property returns the DAO Database object of the database the class currently has open. This allows you to: 1. You can use the Database property to gain access to any of the properties and methods available for the database object that refers to the currently open database. For example, you can instantiate the class, call the OpenDB() method, and then use the class Database property to refer to any of the properties or methods that are applicable to the DAO Database object type. In the code below, we use this technique to display the version of database that the class has open by using the DAO Version property of the database object: Dim MyDB As CJetDatabase Set MyDB = New CJetDatabase MyDB.OpenDB "C:\NWIND.MDB, False, False" Debug.Print MyDB.Database.Version 2. If your code already has the database open, you can Set the class Database property to your database variable. In such a case, this is preferable to using the class OpenDB() method because only one instance of the database is open. Dim dbs As DAO.Database Dim MyDB As CJetDatabase Set dbs = DAO.DBEngine(0).OpenDatabase("C:\NWIND.MDB") Set MyDB = New CJetDatabase Set MyDB.Database = dbs You shouldn't close the database object through the Database property using the DAO close method because class will no longer have the necessary state to perform operations. Instead, perform the CloseDB method if you had the class open the database, or close your own database variable that was passed to the class.
DatabasePassword Property Get The database password used to open the database. This property only has a value if the OpenDB method was called and a password was specified. The property cannot 'extract' the database password used to open a database outside of the class
IsAccessDatabase Property Returns whether the current dadatabase was created by, or has been opened by, Microsoft Access. When Microsoft Access creates a database, it adds many Access-specific objects and properties. These include DAO container objects for the Access-specific objects such as forms and reports, and system tables to hold Access-specific object binary data. When you create a database using VB, or the DAO CreateDatabase method, these objects are not create, and the database is considered to be a pure-Jet database. The first time you open such a database in Access, Access adds its objects and properties, and from then on, those objects and properties live in that database. This property makes it easy to differentiate pure-Jet databases from Access databases. It works by disabling error handling and trying to access the Forms container. In a pure-Jet database, this container doesn't exist, so we can check for an error and determine the status of the database.
LastErrorDescription Property Gets the description of the last error that occurred. This value is useful while debugging to determine why an operation failed. This value is the same as the first member in the DAO.DBEngine.Errors collection.
LastErrorNumber Property Gets the number of the last error that occurred This value is useful while debugging to determine why an operation failed. This value is the same as the Number property of first member in the DAO.DBEngine.Errors collection.
Name Property Gets the name of the current database, without the path
OpenExclusive Property Returns whether or not the database was opened by the class in exclusive mode. The value of this property only has meaning if the OpenDB method was called. This property cannot 'extract' the Exclusive setting of a database opened outside of the class.
OpenReadOnly Property Returns whether or not the database was opened by the class in ReadOnly mode. The value of this property only has meaning if the OpenDB method was called. This property cannot 'extract' the ReadOnly setting of a database opened outside of the class.
Path Property Gets the path of the current database, without the file name
Version Property Gets the version of the Jet engine used to create the database
Class_Terminate Terminate Release resources used by the class
CloseCompactOpen Private Close, compact or repair, then open the specified database. Note that Access cannot compact the current database from VBA code. Set the database's Compact on Close option to do that.
CloseDB Method Closes the current open database Note that this only works if the class opened the database through the OpenDB() method. If the class has a database open that was passed to it through the Database property, or the OpenDB() method has not yet been called, this method silently fails.
Compact Method Closes the currently open database, compacts it, then reopens it. Compacting a database accomplishes several things: - Moves data pages for each table into adjacent locations to speed table access. - Discards unused space left over from object and record deletions, and resizes the database accordingly. - Resets the increment value for AutoNumber (counter) fields so the next number used is one more than the current highest number for each field. - Rebuilds the statistics for tables that are used to generate query optimization plans. All queries are flagged as uncompiled so the next time they run they will use the updated statistics. This method makes it easy to compact the database currently open by the class. It does this by saving the options used to open the database, such as the read-only flag and the database password. It then closes the database, compacts it into a new file, and then renames the new file to the original file name. Finally, it re-opens the database with the same parameters specified when the class OpenDB method was originally called. Note that this method closes the database and re-opens it. Therefore, you should only use this method when your call to the class used the OpenDB method. If you call the class and don't use the OpenDB method, electing instead to set the class Database property to a database you already have open, this method will likely fail, because it will not be able to close your database.
Decrypt Method Closes the currently open database, decrypts it, then reopens it Decrypting a database reverses the act of encrypting a database. For more information on database encryption, see the EncryptDatabase method of this class, or search DAO online help for "EncryptDatabase". This method makes it easy to decrypt the database currently open by the class. It does this by saving the options used to open the database, such as the read-only flag and the database password. It then closes the database, decrypts it into a new file, and then renames the new file to the original file name. Finally, it re-opens the database with the same parameters specified when the class OpenDB method was originally called. Note that this method closes the database and re-opens it. Therefore, you should only use this method when your call to the class used the OpenDB method. If you call the class and don't use the OpenDB method, electing instead to set the class Database property to a database you already have open, this method will likely fail, because it will not be able to close your database. This operation requires exclusive access to the database. If other users have the database open, or other processes or applications on your computer have the database open, the operation will fail.
EmptyAllTables Method Deletes all records from all tables in the current open database **CAUTION: this deletes all data from the database!
Encrypt Method Closes the currently open database, compacts it, encrypts it, then reopens it. Encryption is one of the security features available to Microsoft Jet databases. Encrypting a database makes the contents of the database file unreadable from external sources. Un-encrypted Jet databases contain viewable data that can be seen at the operating system level with tools such as hex editors. While the data is difficult to read (because of embedded high-order bits), it is nonetheless decipherable. On the other hand, encrypted databases are completely unreadable because every byte in the database file has been encrypted using the RSA RC4 algorithm with a 32-bit key for every 2K page. To encrypt a database, you must be logged in as a member of the Admins group of the workgroup information file that was in use when the database was created. Encrypted databases have a 10-15% performance degradation over unencrypted databases. Also, since encryption works by removing repeated patterns in data, an encrypted database is essentially uncompressible using popular compression utilities such as Zip, ARJ, and ARC. Similarly, encrypted databases take more space on compressed archives such as tape drives and compressed disk drives. Note that encrypting a database does not add any additional security as far as applications such as Access and Visual Basic are concerned--an encrypted database can be opened just like any other database without Jet user-level security. The only difference is that the database file cannot be read using external tools such as hex editors. For full data security, you must implement user-level Security.
FieldTypeString Private Convert a field type ID (number) to a string
GetAccessFormCount Method Returns the number of Microsoft Access forms in the database. Note that pure-VB Jet databases do not contain Access objects.
GetAccessMacroCount Method Returns the number of Microsoft Access macros in the database. Note that pure-VB Jet databases do not contain Access objects.
GetAccessModuleCount Method Returns the number of Microsoft Access modules in the database. Note that pure-VB Jet databases do not contain Access objects.
GetAccessReportCount Method Returns the number of Microsoft Access reports in the database. Note that pure-VB Jet databases do not contain Access objects.
GetDatabaseIndexCount Method Returns the count of indexes in the database on all tables. This method does not include indexes on system tables, but does include any system-created indexes on non-system tables. For example, if you create a table with only one index, and then enter that table in a relationship with another table where the relationship link is on an unindexed field, Jet creates an index on your behalf to support the relationship. For this reason, the count returned by this method may be more than the number of indexes you explicitly created on the database's tables.
GetNamePart Private Returns the file name with extension without the leading drive and folder names. This is a private class helper function.
GetNamePartNoExt Private Returns the name of a fully qualified file name with no extension. This is a private class helper function.
GetObjectCount Method Counts the number of primary objects in the database (doesn't include indexes, fields, relationships, etc.) The count for tables includes the system tables (those whose names begin with MSys).
GetPathFromFullPath Private Returns the path (drive and folders) without the file name from a fully qualified file name. This is a private class helper function.
GetQueryCount Method Returns the count of queries in the currently open database. When you create objects with DAO, the collection object that holds that object is not automatically refreshed. This means that when you programmatically create objects, or other users create objects on a shared database, the collection holding the object may not show the most recent additions and deletions. For example, if your code creates a Table object and appends that object to the database's TableDefs collection, the object is not visible to your code, or to other users until a Refresh is issued. To ensure that you are looking at the most recent state of an object's collection, call this procedure with the fRefresh parameter set to True. ' Params : fRefresh True to refresh the database's relations collection, False to get the current state
GetQueryType Method Returns the type of the specified query Microsoft Jet supports several types of query objects. The type of the query is identified by a long integer value in its Type property. This procedure takes the value of the Type property and translates it to the English language name for the type of the query. Valid Jet query types are: - Select - Action - Crosstab - Delete - Update - Append - Make Table - Data Definition - Pass Through - Union
GetRelationCount Method Returns the count of relations in the currently open database. When you create objects with DAO, the collection object that holds that object is not automatically refreshed. This means that when you programmatically create objects, or other users create objects on a shared database, the collection holding the object may not show the most recent additions and deletions. For example, if your code creates a Table object and appends that object to the database's TableDefs collection, the object is not visible to your code, or to other users until a Refresh is issued. To ensure that you are looking at the most recent state of an object's collection, call this procedure with the fRefresh parameter set to True.
GetTableCount Method Returns the count of tables in the currently open database When you create objects with DAO, the collection object that holds that object is not automatically refreshed. This means that when you programmatically create objects, or other users create objects on a shared database, the collection holding the object may not show the most recent additions and deletions. For example, if your code creates a Table object and appends that object to the database's TableDefs collection, the object is not visible to your code, or to other users until a Refresh is issued. To ensure that you are looking at the most recent state of an object's collection, call this procedure with the fRefresh parameter set to True.
GetTableIndexCount Method Returns the count of indexes on the specified table
IndexFieldsToArray Method Populates an array with a list of fields in the specified index
IndexFieldsToString Method Populates a string with a delimited list of fields in an index
IsValidJetName Method Determines if the passed name conforms to Jet naming rules. Jet and DAO use the following rules: - Names can be up to 64 characters long. - Names can include any combination of letters, numbers, spaces, but cannot contains periods (.), exclamation points (!), accent grave (`) or brackets ([ ]). - Names cannot begin with leading spaces. - Names cannot include control characters (ASCII values 0 through 31) Note that even though Jet and DAO allow you to use spaces in object names, you should avoid doing this. Spaces in object names are not legal in other systems such as SQL server, and trying to integrate/upsize Jet database with spaces in names is a very difficult and tedious process. Additionally, spaces in object names require special delimiting when you refer to those objects in code.
ObjectExists Method Determines if the named object exists in the currently opened database object
ObjectsToArray Method Populates the supplied array with a list of object names of the specified type
ObjectsToString Method Returns a string with a list of object names of the specified type
OpenDB Method Opens the named Access/Jet database, and keeps the database open until the class is de-instantiated. If you already have a database variable open and want to use this class, you can set the class Database property to an open DAO Database object variable. See the Database property of this class for more information.
PasswordChange Method Closes the current openly database, compacts it and removes the password, then reopens it
PasswordRemove Method Closes the current openly database, compacts it and removes the password, then reopens it
QueryFieldsToArray Method Populates an array with a list of fields in a query
QueryFieldsToString Method Populates a string with a delimited list of fields in a query
QueryParametersToArray Method Populates an array with a list of parameters in a query
QueryParametersToString Method Populates a string with a delimited list of Parameters in a query
RelationFieldsToArray Method Populates an array with a list of fields in a relation
RelationFieldsToString Method Populates a string with a delimited list of fields in a Relation
TableFieldsToArray Method Populates an array with a list of fields in a table
TableFieldsToString Method Populates a string with a delimited list of fields in a table
TableIndexesToArray Method Populates an array with a list of indexes in a table
TableIndexesToString Method Populates a string with a delimited list of indexes in a table

Overview of Total Visual SourceBook

The source code in Total Visual SourceBook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Additional Resources

Feedback

Contact Us  l   Web questions: Webmaster   l   Copyright © FMS, Inc., Vienna, Virginia
Celebrating our 26th Year of Software Excellence