| {\rtf1\ansi\ansicpg1252\deff0\deflang1044{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}} \viewkind4\uc1\pard\f0\fs17 If your ever editing files or records within files, and hence want to prevent other users/processes from accessing these files, then you can lock the files or records within a file. The following demostrates how to lock and unlock files. \par \par Option Explicit \par \par Public Enum eOpenType \par eOpenUpdate 'Open the file to update it \par eOpenRead 'Open the file to read it \par eOpenNone 'The file is already open \par End Enum \par \par 'Example type \par Public Type tUserDetails \par ID As Integer \par Name As String * 20 \par End Type \par \par \par 'Purpose : Locks a file to prevent other users opening or updating it. \par 'Inputs : sFilePathName The path and name of the file \par ' [eUpdateFile] If True, opens the file for Appending data, else \par ' will open the file for Input \par ' [lRecordLength] The file of the input/read type \par ' [lRecordNumber] The record to lock in the file. If -1, will lock the whole file \par ' [iFreeFile] The file handle of the file to lock. \par 'Outputs : Returns the file handle on success, else returns -1 \par 'Author : Andrew Baker (copyright www.vbusers.com) \par 'Date : 08/01/2001 20:24 \par 'Notes : \par 'Example : \par \par Function FileLock(sFilePathName As String, Optional eUpdateFile As eOpenType = eOpenUpdate, Optional lRecordLength As Long = -1, Optional lRecordNumber As Long = -1, Optional iFreeFile As Integer = 0) As Integer \par \par On Error GoTo ErrFailed \par 'Lock file \par Select Case eUpdateFile \par Case eOpenUpdate \par iFreeFile = FreeFile \par If lRecordLength = -1 Then \par 'Lock whole file \par Open sFilePathName For Random Shared As iFreeFile \par Else \par 'Lock a specific record \par Open sFilePathName For Random Shared As iFreeFile Len = lRecordLength \par End If \par Case eOpenRead \par iFreeFile = FreeFile \par If lRecordLength = -1 Then \par 'Lock whole file \par Open sFilePathName For Random Shared As iFreeFile \par Else \par 'Lock a specific record \par Open sFilePathName For Random Shared As iFreeFile Len = lRecordLength \par End If \par Case eOpenNone \par 'The file is already open, don't open it again \par End Select \par \par If lRecordNumber = -1 Then \par 'Lock whole file, so only this process can edit it \par Lock #iFreeFile \par Else \par 'Lock a specific record, so only this process can edit that record \par Lock #iFreeFile, lRecordNumber \par End If \par \par FileLock = iFreeFile \par \par Exit Function \par \par ErrFailed: \par Debug.Print "Error in FileLock: " & Err.Description \par FileLock = -1 \par End Function \par \par \par 'Purpose : Unlocks a file \par 'Inputs : iFileHandle The handle of the file returned from the function "FileLock" \par ' [lRecordNumber] The record to lock in the file. If -1, will lock the whole file \par ' [bCloseFile] If True will close the file handle \par 'Outputs : Returns the False on success, else returns True \par 'Author : Andrew Baker (copyright www.vbusers.com) \par 'Date : 08/01/2001 20:24 \par 'Notes : \par 'Example : \par \par Function FileUnLock(ByRef iFileHandle As Integer, Optional lRecordNumber As Long = -1, Optional bCloseFile As Boolean = True) As Boolean \par On Error GoTo ErrFailed \par If iFileHandle Then \par If lRecordNumber = -1 Then \par 'Unlock a the whole file \par Unlock iFileHandle% \par Else \par 'Unlock a record \par Unlock iFileHandle%, lRecordNumber \par End If \par If bCloseFile Then \par Close #iFileHandle \par iFileHandle = 0 \par End If \par FileUnLock = True \par End If \par Exit Function \par \par ErrFailed: \par Debug.Print "Error in FileUnLock: " & Err.Description \par FileUnLock = -1 \par End Function \par \par 'Locks and Unlocks a file. This enables you to update a exclusively update file \par 'that is shared by more than one process. \par \par Sub Test() \par Dim iFileHandle As Integer, lThisUser As Long \par Dim tUser As tUserDetails, sFileName As String \par \par '---Store some example data in a file \par sFileName = "C:\\test.txt" \par If Dir$(sFileName) <> "" Then \par 'Delete existing file \par VBA.Kill sFileName \par End If \par iFileHandle = FileLock(sFileName, eOpenUpdate, Len(tUser)) 'Open the file and lock it \par \par 'Add 10 users (all called Andrew Baker!) \par tUser.Name = "Andrew Baker" \par For lThisUser = 1 To 20 \par 'Alter the ID \par tUser.ID = lThisUser \par 'Write the type to the file \par Put #iFileHandle, lThisUser, tUser \par Next \par 'Unlock the file, but don't close it \par FileUnLock iFileHandle, , False \par MsgBox "This file ' " & sFileName & "' now contains 10 records... ", vbInformation \par \par 'Lock record 1 then update it \par Call FileLock(sFileName, eOpenNone, Len(tUser), 1, iFileHandle) \par MsgBox "This record 1 of the file ' " & sFileName & "' is now locked.", vbInformation \par tUser.Name = "Bill Gates" \par tUser.ID = 1 \par 'Write the type to the file \par Put #iFileHandle, 1, tUser \par \par 'Retrieve the records \par For lThisUser = 1 To 20 \par 'Alter the ID \par tUser.ID = lThisUser \par 'Write the type to the file \par Get #iFileHandle, lThisUser, tUser \par Debug.Print "Name/ID: " & Trim$(tUser.Name) & "/" & tUser.ID \par Next \par \par 'Release the lock on the first record and close the file \par FileUnLock iFileHandle, 1, True \par \par End Sub \par } |
Locking and unlocking files |
India web developer web development India | India web development company India ecommerce web developer