How to Request Exclusive Use of a Table in VB Prof 3.0 (108467)



The information in this article applies to:

  • Microsoft Visual Basic Professional Edition for Windows 3.0

This article was previously published under Q108467

SUMMARY

Below is an example of how to request exclusive use of a table and deny access to other users.

MORE INFORMATION

If you do not want any other users to access a table while you have it open, open the table exclusively. Set the options argument of the OpenTable method to a value equal to DB_DENYREAD plus DB_DENYWRITE to give your program exclusive access.

The values of all constants for data access, such as DB_DENYREAD and DB_DENYWRITE, are defined in the DATACONS.TXT text file installed in your Visual Basic directory.

Step-by-Step Example

  1. Start a new project in Visual Basic. Form1 is created by default.
  2. Double-click the form to open its code window. Add the following code to the Form Load event:
       Sub Form_Load ()
          Dim i As Integer
          Dim db As Database
          Dim tb As table
    
          On Error GoTo ExclusiveErr
          Const DB_DENYWRITE = &H1  ' Constant defined in DATACONS.TXT file
          Const DB_DENYREAD = &H2   ' Constant defined in DATACONS.TXT file
          Set db = OpenDatabase("C:\VB3\BIBLIO.MDB")
          Set tb = db.OpenTable("authors", DB_DENYREAD + DB_DENYWRITE)
          ' To open shared access, use: Set tb = db.OpenTable("authors", 0)
          MsgBox "Database & table opened successfully, denying read & write."
          'PlaceDataInControls
          EndExclusiveOpen:
       Exit Sub
    
       ExclusiveErr:
       Select Case Err
       Case 3262
          MsgBox "Table is locked. You cannot open it exclusively. Try shared."
          'optExclusive.Value = False
          Resume EndExclusiveOpen
       Case 3261
          MsgBox "Table exclusively locked by another user -- cannot open."
          End
       Case Else
          MsgBox Err & " " & Error$
       End Select
    
       End Sub
    						
  3. From the File menu, choose Make EXE File. Name the executable file TEST.EXE.
  4. Run one copy of TEST.EXE from the Windows File Manager or Program Manager. Leave the following message on the screen without choosing OK, in order to leave the database open:
    Database & table opened successfully, denying read & write.
  5. Start the program in Visual Basic by pressing the F5 key. This second instance of the program displays the following message:
    Table is locked. You cannot open it exclusively. Try shared.
  6. Close the form to end the program session in Visual Basic. Change DB_DENYREAD + DB_DENYWRITE to 0 in the OpenTable method as follows:
          Set tb = db.OpenTable("authors", 0)
    						
    This opens the table with shared access, the default.
  7. Start the program in Visual Basic by pressing the F5 key. This second instance of the program now displays the following message:
    Table exclusively locked by another user. You cannot open it.
  8. You can end the first instance of the program, TEST.EXE, by clicking OK and closing the form.

REFERENCES

  • Microsoft Visual Basic 3.0: Professional Features Book 2: Data Access Guide, page 58.
  • DATACONS.TXT text file installed in your Visual Basic directory.
  • The VISDATA.MAK file installed in the VB3\SAMPLES\VISDATA directory loads extensive examples of data access. The VISDATA sample program uses every data access function in Visual Basic. You can refer to the VISDATA source code for examples of how to use each data access function.

Modification Type:MinorLast Reviewed:1/8/2003
Keywords:KB108467