How to Change Read-Only Access of a Data Control at Run TimeLast reviewed: June 21, 1995Article ID: Q107074 |
The information in this article applies to:
- Professional Edition of Microsoft Visual Basic for Windows, version 3.0
SUMMARYThis article describes and demonstrates techniques you can use to change (toggle on or off) read-only access of a data control or database at run time. All behavior discussed in this article is by design.
MORE INFORMATION
Notes to Remember When Changing Read-Only AccessFor database object variables, you must close all database variables opened for a given database before you can change its read-only access. Use the Close method to close a database object stored in a variable. Use the OpenDatabase function with a parameter to specify the read-only access for the opened database. For a data control, you must use the Refresh method to reset the data control after you change its read-only access by changing the ReadOnly property. Another more complicated way to reset the data control is to unload the form, which closes the data control. Then use Load and Show to reload the display form. You must set the data control's new ReadOnly property value in the Form Load event procedure, or else ReadOnly will use its design-time setting. The read-only parameter of the OpenDatabase function will be ignored at run time when you open a database that is currently bound to a data control. To change the read-only access of a bound database at run time, you must set the ReadOnly property for the data control and use the Refresh method. The ReadOnly property of a data control will retain the value that you set, but the new ReadOnly property won't take effect until you use the Refresh method. For example, if you change the ReadOnly property from True to False and fail to use the Refresh method, the database bound to the data control will remain read-only even though the ReadOnly property does contain the new value of False. You must execute the Refresh method on the data control to tell the bound database that you changed the ReadOnly property. The following examples demonstrate how to change the read-only access of a database at run time.
Close Database Before Changing Read-Only AccessThe following two examples demonstrate that you must close a database before changing the read-only access (False = 0, True = -1). Example One -- Using Database Object Variables: Open database A and set the read-only parameter to True. Without closing database A, open database A a second time and set read-only access to False. The second open uses read-only access, ignoring your request for read-only to be False. This behavior is by design. The following code example demonstrates this behavior:
Dim d1 As database, d2 As database Set d1 = OpenDatabase("biblio.mdb", 0, -1) 'set read-only option True debug.print d1.Updatable ' Updatable prints False, 0, as expected. Set d2 = OpenDatabase("biblio.mdb", 0, 0) 'set read-only option False debug.print d2.Updatable ' Updatable still prints False, 0By definition, the Updatable property returns False when the read-only access is True, and vice versa. In the above example, the Updatable property of database variable d2 remains False from the first open, despite your request to turn off read-only access. To reset the read-only access, close the database first. Then change the read-only access. For example:
Dim d1 As database, d2 As database Set d1 = OpenDatabase("biblio.mdb", 0, -1) ' set read-only option True debug.print d1.Updatable ' Updatable prints False, 0, as desired. d1.Close Set d2 = OpenDatabase("biblio.mdb", 0, 0) ' set read-only option False debug.print d2.Updatable ' Updatable prints True, -1, as desired.Database variable d1 is erased by the Close. The Updatable property of the second database variable (d2) will be False, as desired. Updatable returns False when the database was opened with read-only access. Example Two -- Using a Text Control Bound to a Data Control: A data control does implicit OpenDatabase and CreateDynaset function calls at load time using design-time properties such as DatabaseName, RecordSource, and ReadOnly. If you change the ReadOnly property of a data control at run time, you must use the Refresh method to reset the database. The following steps show how to do it:
You Can Open Different Databases with Different Read-only AccessEach different database you open can have a different read-only setting. In the following example, d1.Updatable will be True and d2.Updatable will be False, because they refer to different databases:
Dim d1 As database, d2 As database Set d1 = OpenDatabase("biblio.mdb", 0, -1) ' Set read-only option True Set d2 = OpenDatabase("mydb.mdb", 0, 0) ' Set read-only option False debug.print d1.Updatable, d2.Updatable ' Prints -1 and 0 |
Additional reference words: 3.00 R/W status lock locking how-to
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |