Microsoft KB Archive/108467

= How to Request Exclusive Use of a Table in VB Prof 3.0 =

Article ID: 108467

Article Last Modified on 1/8/2003

-

APPLIES TO


 * Microsoft Visual Basic 3.0 Professional Edition

-



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
 Start a new project in Visual Basic. Form1 is created by default.  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  From the File menu, choose Make EXE File. Name the executable file TEST.EXE. 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.

 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.

  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.  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.

</li> You can end the first instance of the program, TEST.EXE, by clicking OK and closing the form.</li></ol>

<div class="references_section">