Lock 语句、Unlock 语句Lock, Unlock statements

控制其他过程对使用**Open** 语句打开的文件的全部或部分的访问。Controls access by other processes to all or part of a file opened by using the Open statement.

语法Syntax

锁定[ # ]filenumber、[ recordrange ] ...Lock [ # ] filenumber, [ recordrange ] . . .
解除锁定[ # ]filenumber、[ recordrange ]Unlock [ # ] filenumber, [ recordrange ]


LockUnlock 语句语法包含以下部分:The Lock and Unlock statement syntax has these parts:

PartPart 说明Description
filenumberfilenumber 必需。Required. 任何有效的文件编号Any valid file number.
recordrangerecordrange 可选。Optional. 要锁定或解锁的记录的范围。The range of records to lock or unlock.

设置Settings

Recordrange 参数设置为:The recordrange argument settings are:

recnumber |[ start ]结束recnumber | [ start ] To end


设置Setting 说明Description
recnumberrecnumber 要开始锁定或解锁的记录编号(Random 模式文件)或字节编号(Binary 模式文件)。Record number (Random mode files) or byte number (Binary mode files) at which locking or unlocking begins.
startstart 要锁定或解锁的第一个记录或字节的编号。Number of the first record or byte to lock or unlock.
endend 要锁定或解锁的最后一个记录或字节的编号。Number of the last record or byte to lock or unlock.

备注Remarks

LockUnlock 语句将在多个过程可能需要访问同一个文件的环境中使用。The Lock and Unlock statements are used in environments where several processes might need access to the same file.

LockUnlock 语句始终成对使用。Lock and Unlock statements are always used in pairs. LockUnlock 的参数必须完全匹配。The arguments to Lock and Unlock must match exactly.

文件中的第一个记录或字节位于位置 1,第二个记录或字节位于位置 2,依此类推。The first record or byte in a file is at position 1, the second record or byte is at position 2, and so on. 如果只指定一条记录, 则仅锁定或解除锁定该记录。If you specify just one record, only that record is locked or unlocked. 如果您指定一系列记录并忽略开始记录 (start),则将锁定或解锁从第一个记录到范围结尾 (end) 的记录。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. 在没有 recnumber 的情况下使用 Lock 将锁定整个文件;在没有 recnumber 的情况下使用 Unlock 将解锁整个文件。Using Lock without recnumber locks the entire file; using Unlock without recnumber unlocks the entire file.

如果已打开文件进行连续输入或输出,则 LockUnlock 将影响整个文件,而不管 startend 指定的范围如何。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.

重要

[!重要信息] 务必在关闭文件或退出程序之前使用 Unlock 语句删除所有锁定。Be sure to remove all locks with an Unlock statement before closing a file or quitting your program. 删除锁定失败将产生无法预测的结果。Failure to remove locks produces unpredictable results.

示例Example

此示例演示对 LockUnlock 语句的使用。This example illustrates the use of the Lock and Unlock statements. 修改记录时,将拒绝其他过程对记录的访问。While a record is being modified, access by other processes to the record is denied. 此示例假定TESTFILE是一个文件, 其中包含五个用户定义类型Record的记录。This example assumes that TESTFILE is a file containing five records of the user-defined type Record.

Type Record    ' Define user-defined type. 
    ID As Integer 
    Name As String * 20 
End Type 
 
Dim MyRecord As Record, RecordNumber    ' Declare variables. 
' Open sample file for random access. 
Open "TESTFILE" For Random Shared As #1 Len = Len(MyRecord) 
RecordNumber = 4    ' Define record number. 
Lock #1, RecordNumber    ' Lock record. 
Get #1, RecordNumber, MyRecord    ' Read record. 
MyRecord.ID = 234    ' Modify record. 
MyRecord.Name = "John Smith" 
Put #1, RecordNumber, MyRecord    ' Write modified record. 
Unlock #1, RecordNumber    ' Unlock current record. 
Close #1    ' Close file. 

另请参阅See also

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.