Lock, Unlock Statements

See Also1CMY0XG              Example1C4YFCL>Low

Control access by other processes to all or part of an opened file.

Syntax

Lock [#]filenumber [,{ record | [start] To end} ]
. . .

Unlock [#]filenumber [,{ record | [start] To end} ]

Remarks

The Lock and Unlock statements are used in networked environments in which several processes might need access to the same file.  The arguments to Lock and Unlock must match exactly.  The Lock and Unlock statements have these parts:

Part               Description

 

filenumber      Number used in the Open statement to open the file.  It can be any numeric expression71RISN that evaluates to the number of an open file.

record            Number of the record or byte to lock.  It can be any number from 1 through 2,147,483,647 (equivalent to 2^31-1).  A record can be up to 65,535 bytes long.

start              Number of the first record or byte to lock.

To                 Separates start from end.

end                Number of the last record or byte to lock.

 

For Binary mode files, record, start, and end represent the number of a byte relative to the beginning of a file.  The first byte in a file is byte 1.

For Random files, record, start, and end represent the number of a record relative to the beginning of a file.  The first record is record 1.

If the file has been opened for sequential input or output, Lock and Unlock affect the entire file, regardless of the range specified by start and end.

Lock and Unlock statements are always used in pairs.  The arguments to Lock and Unlock must match exactly.

If you specify just one record, then only that record is locked or unlocked.  If you specify a range of records and omit a starting record (start), all records from the first record to the end of the range (end) are locked or unlocked. Using Lock without record locks the entire file; using Unlock without record unlocks the entire file.

If you are using a version of MS-DOS that supports networking (MS-DOS version 3.1 or later), you must run the SHARE.EXE program to enable locking operations.  You can't use Lock and Unlock with earlier versions of MS-DOS.

 

Note   Be sure to remove all locks with an Unlock statement before closing a file or terminating your program.  Failure to remove locks produces unpredictable results.

 


See Also

Open Statement103I7PS


Lock, Unlock Statements Example

The example illustrates the use of the Lock and Unlock statements.  While a record (RecNum) is being updated, access to it on a network is locked to other users until the update is complete.  To try this example, paste the user-defined type declaration into the Declarations section of a module.  Paste the remaining code into the Declarations section of a form.  Then press F5 and click the form.

 

Type AccountRec                          ' Define user-defined type.

   Payer As String * 20

   Address As String * 20

   Place As String * 20

   Owe As Single

End Type

 

Sub Form_Click ()

   Dim CustRec As AccountRec             ' Declare variables

   Dim Change, Msg, NL, RecNum

   NL = Chr(10)                          ' Define newline.

   On Error GoTo ErrorHandler            ' Set up error handler.

   MakeDataFile                          ' Create sample data file.

   Open "TESTFILE" For Random Shared As #1 Len = Len(CustRec)

   Do

      RecNum = 1                         ' Since there is only 1 record,

      Lock #1, RecNum                    ' lock the current record.

      Get #1, RecNum, CustRec            ' Read a record.

      Msg = "Customer " & CustRec.Payer

      Msg = Msg & " currently owes: "

      Msg = Msg & Format(CustRec.Owe, "$#,##0.00") & NL & NL

      Msg = Msg & "Please input net change (+ or - )"

      Change = InputBox(Msg)             ' Show data and get change.

      If Len(Change) = 0 Then Change = 0

      CustRec.Owe = CustRec.Owe + Change

      Put #1, RecNum, CustRec            ' Update record.

      Unlock #1, RecNum                  ' Unlock the record.

   Loop Until Change = 0

 

   Close #1                              ' Close the file.

Cleanup:

   Msg = "Transaction complete. Choose OK to remove the sample "

   Msg = Msg & "data file."

   MsgBox Msg                            ' Display message.

   Kill "TESTFILE"                       ' Remove file from disk.

   Exit Sub

ErrorHandler:

If Err = 70 Then                         ' Permission denied error.

   Msg = "You must run SHARE.EXE before running this example. Exit "

   Msg = Msg & "Visual Basic, Exit Windows, run SHARE.EXE, and "

   Msg = Msg & "reenter Visual Basic to run this example. Do not "

   Msg = Msg & "shell to DOS and run SHARE.EXE or you may not be "

   Msg = Msg & "able to run other programs until you reboot."

Else                                     ' Some other error occurred.

   Msg = "Error " & Err & " occurred."

End If

MsgBox Msg                               ' Display error message.

Reset                                    ' Close files, flush buffers.

Resume Cleanup                           ' Do an orderly exit.

End Sub

 

Sub MakeDataFile ()

   Dim CustRec As AccountRec             ' Declare variables.

   Open "TESTFILE" For Random Shared As #1 Len = Len(CustRec)

   CustRec.Payer = "John Smith"          ' Put information in

   CustRec.Address = "1 Maple Street"    ' each field of the record.

   CustRec.Place = "New York, NY"

   CustRec.Owe = 12!                     ' Initialize owed amount.

   Put #1, 1, CustRec                    ' Put record in file.

   Close #1                              ' Close the file.

End Sub