How to Change Read-Only Access of a Data Control at Run Time

Last reviewed: June 21, 1995
Article ID: Q107074
The information in this article applies to:

- Professional Edition of Microsoft Visual Basic for Windows,

  version 3.0

SUMMARY

This 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 Access

For 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 Access

The 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, 0

By 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:

  1. Start Visual Basic or begin a New Project. This creates Form1 by default.

  2. Add a data control (Data1) to Form1, and give Data1 these properties:

       DatabaseName:  C:\VB\BIBLIO.MDB   [This database ships with VB.]
       RecordSource:  Authors            [Uses Authors Table in BIBLIO.MDB.]
       ReadOnly:      True               [Makes ReadOnly=True at start.]
    
    

  3. Add a text box (Text1) to Form1. Give Text1 the following properties:

       DataSource:    Data1
       DataField:     Authors
    
    

  4. Add two command buttons (Command1 and Command2) to Form1. Change the Caption of Command1 to say "Enable Database Writing." Change the Caption of Command2 to say "Make Database Read-only." Enter the following code:

       Sub Command1_Click ()
          debug.Print Data1.ReadOnly   ' DATA1.ReadOnly starts True, -1
          Data1.ReadOnly = False       ' Change to False
          Data1.Refresh   ' Refresh required to change ReadOnly access.
          debug.Print Data1.ReadOnly   ' Prints False, 0
       End Sub
    
       Sub Command2_Click ()
          debug.Print Data1.ReadOnly   ' Prints False, 0
          Data1.ReadOnly = True        ' Change to True
          Data1.Refresh   ' Refresh required to change ReadOnly access.
          debug.Print DATA1.ReadOnly   ' Prints True, -1
       End Sub
    
    

  5. Start the program by pressing the F5 key or choosing Start from the run menu.

  6. Add some characters to the record in the text box. This record is the Authors field of the C:\VB\BIBLIO.MDB database. Click the right arrow on the data control to move to the next record. This automatically updates the first record if you have write access. Then click the left arrow on the data control to view your change to the first record. When ReadOnly is True, such as at the beginning of the program, no change will occur in the record shown in Text1.

  7. Now click the Command1 button, "Enable Database Writing." Then repeat step 6 to update a record. Because ReadOnly is False now, the Text1 box will now reflect your change in the record.

  8. Now click the Command2 button, "Make Database Read-only." Then repeat step 6 to update a record. Because ReadOnly is True now, no change will occur in Text1.

  9. Close the form to end the program.

You Can Open Different Databases with Different Read-only Access

Each 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
KBCategory: kbprg kbcode
KBSubcategory: APrgDataOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.