The information in this article applies to:
- Professional Edition of Microsoft Visual Basic for Windows, version 3.0
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.
- 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.
|