logs for user activity in vba userform - vba

I am trying to work on a code that will give me the user activity on the userform that I have created. I want to keep this logs file in shared drive if possible.
below is my code, not getting any records in logs folder
Sub LogInformation(LogMessage As String)
Const LogFileName As String = "D:\FOLDERNAME\TEXTFILE.LOG"
Dim FileNum As Integer
FileNum = FreeFile ' next file number
Open LogFileName For Append As #FileNum ' creates the file if it doesn't exist
Print #FileNum, LogMessage ' write information at the end of the text file
Close #FileNum ' close the file
End Sub
Public Sub DisplayLastLogInformation()
Const LogFileName As String = "D:\FOLDERNAME\TEXTFILE.LOG"
Dim FileNum As Integer, tLine As String
FileNum = FreeFile ' next file number
Open LogFileName For Input Access Read Shared As #f ' open the file for reading
Do While Not EOF(FileNum)
Line Input #FileNum, tLine ' read a line from the text file
Loop ' until the last line is read
Close #FileNum ' close the file
MsgBox tLine, vbInformation, "Last log information:"
End Sub
Sub DeleteLogFile(FullFileName As String)
On Error Resume Next ' ignore possible errors
Kill FullFileName ' delete the file if it exists and it is possible
On Error GoTo 0 ' break on errors
End Sub
Private Sub Workbook_Open()
LogInformation ThisWorkbook.Name & " opened by " & _
Application.UserName & " " & Format(Now, "yyyy-mm-dd hh:mm")
End Sub

Related

Attaching additional lines on top of a Access CSV export

I have a process in MS Access where I am exporting the result of a query into a csv format using the DoCmd TransferText method, and using an export specification to contain the field names.
However, I need to attach to the beginning of this file several lines- all of a fixed value- I'm naming this in my code with vbCrLf to separate the several lines I need. How can I append this to the top of the file after it is created. So my file needs to look like this, with the three extra lines for example at the top, with the csv export contents directly below. How can I achieve this? Thanks!
***(need this line 1)
(need this line 2)
(need this line 3)***
field1,field2,field3
x, y, z
As #TimWilliams has suggested, you will need to use VBA to do this. Below is some code that exports the query to a text file using .TransferText, then opens it and imports the data as one chunk, before writing back out the three header lines and the original data:
Sub sExportCSV()
On Error GoTo E_Handle
Dim strFile As String
Dim strLine1 As String
Dim strLine2 As String
Dim strLine3 As String
Dim strData As String
Dim intFile As Integer
strLine1 = "This is line 1"
strLine2 = "This is the second line"
strLine3 = "And this is the last line"
strFile = "J:\test-data\csv.txt"
DoCmd.TransferText acExportDelim, , "qdfExport", strFile, False
intFile = FreeFile
Open strFile For Input As intFile
strData = Input(LOF(intFile), intFile)
Close #intFile
intFile = FreeFile
Open strFile For Output As intFile
Print #intFile, strLine1 & vbCrLf & strLine2 & vbCrLf & strLine3 & vbCrLf & strData
Close #intFile
sExit:
On Error Resume Next
Close #intFile
Exit Sub
E_Handle:
MsgBox Err.Description & vbCrLf & vbCrLf & "sExportCSV", vbOKOnly + vbCritical, "Error: " & Err.Number
Resume sExit
End Sub
Regards,

"WshShell The system cannot find the file specified Error 80070002"

I am using VBA to create some CSV files which are then used as references so that a batch file can use them as inputs. I have succeeded in making this work but found that I needed the VBA code to wait for the script to complete before continuing. From what I found on the web I should use the Windows Script Host Object Model" which would then allow me to set WaitOnReturn:=True. However I now get "Run-time error '-2147024894 (80070002)': Automation Error The system cannot find the file specified." This is obviously something elementary that I have missed but it is taking me hours to work out what it is. I have searched and not found an answer that seems to help. Here is the code that works, followed by the code that fails. Thanks in advance for any guidance.
Public Sub Create_run_delete_batch_file(Input_File)
Const MY_FILENAME = "E:\Test.bat"
Dim FileNumber As Integer
Dim retVal As Variant
FileNumber = FreeFile
'create batch file
Open MY_FILENAME For Output As #FileNumber
Print #FileNumber, "E:"
Print #FileNumber, "cd " & Chr(34) & "E:\Test_Dir\" & Chr(34)
'Set Current Risk File here
Print #FileNumber, "Script that works goes here" & Input_File
Close #FileNumber
'run batch file
retVal = Shell(MY_FILENAME, vbNormalFocus)
' NOTE THE BATCH FILE WILL RUN, BUT THE CODE WILL CONTINUE TO RUN.
If retVal = 0 Then
MsgBox "An Error Occured"
Close #FileNumber
End
End If
'Delete batch file
Kill MY_FILENAME
End Sub
Public Sub Create_run_delete_batch_file(Input_File)
Const MY_FILENAME = "E:\Test.bat"
Dim FileNumber As Integer
Dim retVal As Variant
Dim wsh As WshShell
Set wsh = New WshShell
FileNumber = FreeFile
'create batch file
Open MY_FILENAME For Output As #FileNumber
Print #FileNumber, "E:"
Print #FileNumber, "cd " & Chr(34) & "E:\Test_Dir\" & Chr(34)
'Set Current Risk File here
Print #FileNumber, "Script that works goes here" & Input_File
Close #FileNumber
'run batch file
'retVal = Shell(MY_FILENAME, vbNormalFocus)
retVal = wsh.Run(MY_FILENAME, WaitOnReturn:=True)
' NOTE THE BATCH FILE WILL RUN, BUT THE CODE WILL CONTINUE TO RUN.
If retVal = 0 Then
MsgBox "An Error Occured"
Close #FileNumber
End
End If
'Delete batch file
Kill MY_FILENAME
End Sub

Need help on Vb code i have created

i am working on this project, which I login in to router . ping to some IP, if i increase the packet size to 4, the O/p will be display result of Percent & roundtrip quicker as compared to packet count-100 which takes time. so need code so that Vb check the file real-time like "screening window" and if wherever it find the Percent & roundtrip on cmd windows it will send logout/exit/some my stuff to cmd and display in the MsgBox .
i need help on Vb checking the txt file real-time , if not found, close the file and again open for rechecking and so on or may be like "screening window" ..
pl help .
My original Code is below.
====================
Private Sub Runcode()
i = Shell("\windows\system32\cmd.exe", vbNormalFocus)
Dim MY_SERVER_IP As String
Dim LocalHost As String
Dim SourceIP As String
Dim DestinationIP As String
Dim Number As String
Dim Size As String
MY_SERVER_IP = "97.1.2.2"
Application.Wait (Now + TimeValue("0:00:02"))
SendKeys "telnet " & MY_SERVER_IP & " 6000 -f D:\Report\PING.txt{ENTER}"
Application.Wait (Now + TimeValue("0:00:02"))
SendKeys " Command to Login the Router"
Application.Wait (Now + TimeValue("0:00:02"))
SendKeys " PING IP: SRN=0, SN=14, SIPADDR=some_Source_IP , DESTIP=Some_Destination_IP, CONTPING=NO, TIMES=4, PKTSIZE=1400; "
Application.Wait (Now + TimeValue("0:00:50"))
' for short packet Number also i have to wait around 50 sec
' here currently Times=4 means it will recieve 4 reply packets which will show the result in 4 sec
' but same if i change it to 50 it wll take around 30-50 sec
Dim sFileName As String
Dim iFileNum As Integer
Dim sBuf As String
Dim Fields As String
Dim buffer As String
buffer = "round-trip"
sFileName = "D:\Report\PING.txt"
''//Does the file exist?
If Len(Dir$(sFileName)) = 0 Then
MsgBox ("File not Created")
End If
iFileNum = FreeFile()
Open sFileName For Input As iFileNum
Do While Not EOF(iFileNum)
Line Input #iFileNum, Fields
If Fields <> buffer Then
Call hint
Else
MsgBox ("didnt Work")
End If
Loop
Close iFileNum
End Sub
Close sFileName
Sub hint()
Dim myFile As String, text As String, textline As String, pkt As String, rndtrip As String
Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.AppActivate "Telnet " & MY_SERVER_IP
objShell.SendKeys "Logout{enter}"
objShell.Application.Wait (Now + TimeValue("0:00:02"))
objShell.SendKeys "Exit{enter}"
myFile = "D:\Report\PING.txt"
Open myFile For Input As #1
Do Until EOF(1)
Line Input #1, textline
text = text & textline
Loop
Close #1
pkt = Trim(Mid(text, InStr(text, "Percent") + 0, (InStrRev(text, "packet") + 0) - (InStr(text, "Percent") + 1)))
rndtrip = Trim(Mid(text, InStr(text, "round-trip") + 0, (InStrRev(text, "ms") + 0) - (InStr(text, "round-trip") + 1)))
Call MsgBox("Percent : " & pkt & " Round-Trip : " & rndtrip & ".", vbOKOnly)
End Sub
Try Adding this line. Your loop will only fire if the file exist.
If System.IO.File.Exists(myFile )
Do Until EOF(1)
Line Input #1, textline
text = text & textline
Loop
Close #1
else
' if the file is not there rerun the method.
Runcode()
end if

Can't pass variables from one sub to another in same module

The code I have works great as separate procedures. I decided to combine the 2 procedures as the code is mostly the same with a few differences in which cells are copied to the array to save to different log sheets. Each Procedure has a Inputbox to get the year of interest (2014, 2015, 2016....) that will be used to open AND in the event the Log does not exist Save a new one from a master using the year code in the file name.
Based on a cell value I am doing an If_Then_Else to determine to run one procedure or both. If I run them separately as individual procedures they work fine. When I combined them I decided to call one procedure (SaveToLog_Audit) from inside (SaveToLog_Replc). I removed the Date input from (SaveToLog_Audit) letting (SaveToLog_Replc) get that date code and pass it along. I don't want to have (SaveToLog_Audit) ask for a date a second time as it run since I basically want it to be inobtrusive (no need to ask twice when I can pass Ans to it.
I have tried setting public Ans and used Explicit at the beginning with both procedures in same module --> didn't work. I tried Dim Ans As String after option Explicit --> Didn't work. I tried Public Ans as String --> didn't work. Pretty much I couldn't get it to pass the variable 'Ans" no matter what I tried. I'm still pretty new to coding VBA so any help would be great. Showing the basic code, only the parts that have issues.
Option Explicit
Public Ans As String
Public Sub SaveToLog_Replc()
' Determine scrap and whether to save to Replacement_Credit tracking log
Dim ScrapQty As Range
' Date entry for proper Dated log save
Dim Ans As String
Dim c As Boolean
Dim fs As Object
' Declares path, newFile and fName variables
Dim path As String, newFile As String, fName As String
Set Sheet2 = ActiveSheet
Set ScrapQty = Worksheets("Main").Range("M_Qty_Scrap")
Application.ScreenUpdating = False
' >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Ans = InputBox("Enter Log Year" & _
"" & vbCrLf, "Year Selection", Format(Date, "YYYY"))
If Ans = "" Then
Exit Sub
End If
' >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
' If scrap qty is greater than Zero (0) save to both Logs
If ScrapQty > 0 Then
' If True
MsgBox "Saving to Replacement Log and Audit Log"
' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
' Save to Audit Log then Save to Replacement Log
' --> 'Ans' VALUE NEEDS TO PASS TO THE PROCEDURE
' --> 'Ans' DOES NOT PASS TO THIS PROCEDURE AT END OF MODULE
Call SaveToLog_Audit
' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
' +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' Sub routine to Save Data to Replacement Log
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Load Array code here for SaveToLog_Replc
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' ***************************************************************
'Check if Replacement Log exists. If not open Master, rename then save it. If it exists open it.
Set fs = CreateObject("Scripting.FileSystemObject")
' Filename to check for
c = fs.fileExists("S:\RECORDS\Logs\Replacement Log " & Ans & ".xls")
If Not c Then
'MsgBox "The file doesn't exist!"
' File doesn't exist. Open the Blank Master
Workbooks.Open Filename:="S:\RECORDS\Logs\_MASTER Replacement Log.xls"
fName = "Replacement Log " & Ans & ".xls" ' Set fName to new FileName
newFile = fName ' Sets new filename as fName
path = "S:\RECORDS\Logs\" ' Path to Incoming Audit Logs
ActiveWorkbook.SaveAs Filename:=path & newFile ' Saves as newFile
Else
'MsgBox "The file exists! Saving data to it."
Workbooks.Open Filename:="S:\RECORDS\Logs\Replacement Log " & Ans & ".xls"
End If
' ***************************************************************
' Unprotect Sheet and Show All Data code here
' Find LastRow. Set NextCell position code here
' Set the size of the new array and copy MyAr code here
' Draw Border Code here
' **********************************************
ActiveWorkbook.Save ' Saves Destination Workbook
ActiveWindow.Close ' Closes Destination Workbook
Application.ScreenUpdating = True
' Confirms Save to Log File
MsgBox "Your Data has been saved to the Log File: " & vbCrLf & vbCrLf _
& "'Replacement Log " & Ans & ".xls'", vbInformation, "Log Save Confirmation"
' +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Else
' If False
MsgBox "Saving to Audit Log Only."
' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
' Save to Audit Log as Normal
' --> 'Ans' VALUE NEEDS TO PASS TO THE PROCEDURE
' --> 'Ans' DOES NOT PASS TO THIS PROCEDURE AT END OF MODULE
Call SaveToLog_Audit
' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
End If
End Sub
Public Sub SaveToLog_Audit()
' Date entry for proper Dated log save
Dim Ans As String
Dim c As Boolean
Dim fs As Object
' Declares path, newFile and fName variables
Dim path As String, newFile As String, fName As String
Set Sheet2 = ActiveSheet
Application.ScreenUpdating = False
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Load Array code here for SaveToLog_Audit
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' For debug
' --> 'Ans' DOES NOT PASS FROM (SaveToLog_Replc) PROCEDURE IN THIS MODULE
MsgBox "The value of Ans is: " & vbCrLf & vbCrLf & "'" & Ans & "'"
' ********************************************************
' Checks if Log File exists. If not open the Master, rename then save it. If it exists open it.
Set fs = CreateObject("Scripting.FileSystemObject")
' Filename to check for
c = fs.fileExists("S:\RECORDS\Logs\" & Ans & " Audit Log.xls")
If Not c Then
'MsgBox "The file doesn't exist!" original code
' File doesn't exist. Open the Blank Master
Workbooks.Open Filename:="S:\RECORDS\Logs\_Master Audit Log.xls"
' Set fName to new FileName
fName = Ans & " Audit Log.xls"
' Sets new filename as fName saves to this directory
newFile = fName
' Path to Incoming Audit Logs
path = "S:\RECORDS\Logs\"
' Saves Blank Master as newFile
ActiveWorkbook.SaveAs Filename:=path & newFile
Else
'Workbooks.Open "C:\filename.xls"
Workbooks.Open Filename:= _
"S:\RECORDS\Logs\" & Ans & " Audit Log.xls"
End If
' ********************************************************
' Unprotect Sheet and Show All Data code here
' Find LastRow. Set NextCell position code here
' Set the size of the new array and copy MyAr code here
' Draw Border Code here
' **********************************************
ActiveWorkbook.Save ' Saves Destination Workbook
ActiveWindow.Close ' Closes Destination Workbook
Application.ScreenUpdating = True
' Confirms Save to Log File
MsgBox "Your Data has been saved to the Log File: " & vbCrLf & vbCrLf _
& "'" & Ans & " Audit Log.xls'", vbInformation, "Log Save Confirmation"
End Sub
Are you running the code exactly as you posted? If yes then problem might be that you are delaring public ans then you are using dim ans in the first sub, later on you are assigning value to dim Ans not to public Ans those are made two separate variables. Remove the dim Ans from both subroutines and it should work
I suggest you remove the Public declaration and change the second routine to take a String argument:
Option Explicit
Public Sub SaveToLog_Replc()
' Determine scrap and whether to save to Replacement_Credit tracking log
Dim ScrapQty As Range
' Date entry for proper Dated log save
Dim Ans As String
Dim c As Boolean
Dim fs As Object
' Declares path, newFile and fName variables
Dim path As String, newFile As String, fName As String
Set Sheet2 = ActiveSheet
Set ScrapQty = Worksheets("Main").Range("M_Qty_Scrap")
Application.ScreenUpdating = False
' >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Ans = InputBox("Enter Log Year" & _
"" & vbCrLf, "Year Selection", Format(Date, "YYYY"))
If Ans = "" Then
Exit Sub
End If
' >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
' If scrap qty is greater than Zero (0) save to both Logs
If ScrapQty > 0 Then
' If True
MsgBox "Saving to Replacement Log and Audit Log"
' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
' Save to Audit Log then Save to Replacement Log
' --> 'Ans' VALUE NEEDS TO PASS TO THE PROCEDURE
' --> 'Ans' DOES NOT PASS TO THIS PROCEDURE AT END OF MODULE
Call SaveToLog_Audit(Ans)
' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
' +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' Sub routine to Save Data to Replacement Log
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Load Array code here for SaveToLog_Replc
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' ***************************************************************
'Check if Replacement Log exists. If not open Master, rename then save it. If it exists open it.
Set fs = CreateObject("Scripting.FileSystemObject")
' Filename to check for
c = fs.fileExists("S:\RECORDS\Logs\Replacement Log " & Ans & ".xls")
If Not c Then
'MsgBox "The file doesn't exist!"
' File doesn't exist. Open the Blank Master
Workbooks.Open Filename:="S:\RECORDS\Logs\_MASTER Replacement Log.xls"
fName = "Replacement Log " & Ans & ".xls" ' Set fName to new FileName
newFile = fName ' Sets new filename as fName
path = "S:\RECORDS\Logs\" ' Path to Incoming Audit Logs
ActiveWorkbook.SaveAs Filename:=path & newFile ' Saves as newFile
Else
'MsgBox "The file exists! Saving data to it."
Workbooks.Open Filename:="S:\RECORDS\Logs\Replacement Log " & Ans & ".xls"
End If
' ***************************************************************
' Unprotect Sheet and Show All Data code here
' Find LastRow. Set NextCell position code here
' Set the size of the new array and copy MyAr code here
' Draw Border Code here
' **********************************************
ActiveWorkbook.Save ' Saves Destination Workbook
ActiveWindow.Close ' Closes Destination Workbook
Application.ScreenUpdating = True
' Confirms Save to Log File
MsgBox "Your Data has been saved to the Log File: " & vbCrLf & vbCrLf _
& "'Replacement Log " & Ans & ".xls'", vbInformation, "Log Save Confirmation"
' +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Else
' If False
MsgBox "Saving to Audit Log Only."
' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
' Save to Audit Log as Normal
' --> 'Ans' VALUE NEEDS TO PASS TO THE PROCEDURE
' --> 'Ans' DOES NOT PASS TO THIS PROCEDURE AT END OF MODULE
Call SaveToLog_Audit(Ans)
' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
End If
End Sub
Public Sub SaveToLog_Audit(Ans As String)
' Date entry for proper Dated log save
Dim c As Boolean
Dim fs As Object
' Declares path, newFile and fName variables
Dim path As String, newFile As String, fName As String
Set Sheet2 = ActiveSheet
Application.ScreenUpdating = False
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Load Array code here for SaveToLog_Audit
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' For debug
' --> 'Ans' DOES NOT PASS FROM (SaveToLog_Replc) PROCEDURE IN THIS MODULE
MsgBox "The value of Ans is: " & vbCrLf & vbCrLf & "'" & Ans & "'"
' ********************************************************
' Checks if Log File exists. If not open the Master, rename then save it. If it exists open it.
Set fs = CreateObject("Scripting.FileSystemObject")
' Filename to check for
c = fs.fileExists("S:\RECORDS\Logs\" & Ans & " Audit Log.xls")
If Not c Then
'MsgBox "The file doesn't exist!" original code
' File doesn't exist. Open the Blank Master
Workbooks.Open Filename:="S:\RECORDS\Logs\_Master Audit Log.xls"
' Set fName to new FileName
fName = Ans & " Audit Log.xls"
' Sets new filename as fName saves to this directory
newFile = fName
' Path to Incoming Audit Logs
path = "S:\RECORDS\Logs\"
' Saves Blank Master as newFile
ActiveWorkbook.SaveAs Filename:=path & newFile
Else
'Workbooks.Open "C:\filename.xls"
Workbooks.Open Filename:= _
"S:\RECORDS\Logs\" & Ans & " Audit Log.xls"
End If
' ********************************************************
' Unprotect Sheet and Show All Data code here
' Find LastRow. Set NextCell position code here
' Set the size of the new array and copy MyAr code here
' Draw Border Code here
' **********************************************
ActiveWorkbook.Save ' Saves Destination Workbook
ActiveWindow.Close ' Closes Destination Workbook
Application.ScreenUpdating = True
' Confirms Save to Log File
MsgBox "Your Data has been saved to the Log File: " & vbCrLf & vbCrLf _
& "'" & Ans & " Audit Log.xls'", vbInformation, "Log Save Confirmation"
End Sub

Permission Denied error in excel vba

I am writing a function in excel vba
Function WriteByteArray(vData As Variant, sFileName As String, Optional bAppendToFile As Boolean = False) As Boolean
Dim iFileNum As Integer, lWritePos As Long
Debug.Print " --> Entering WriteByteArray function with " & sFileName & " file to write."
On Error GoTo ErrFailed
If bAppendToFile = False Then
If Len(Dir$(sFileName)) > 0 And Len(sFileName) > 0 Then
'Delete the existing file
VBA.Kill sFileName
End If
End If
iFileNum = FreeFile
Debug.Print "iFileNum = " & iFileNum
'Open sFileName For Binary Access Write As #iFileNum
Open sFileName For Binary Lock Read Write As #iFileNum
If bAppendToFile = False Then
'Write to first byte
lWritePos = 1
Else
'Write to last byte + 1
lWritePos = LOF(iFileNum) + 1
End If
Dim buffer() As Byte
buffer = vData
Put #iFileNum, lWritePos, buffer
WriteByteArray = True
Exit Function
ErrFailed:
Debug.Print "################################"
Debug.Print "Error handling of WriteByteArray"
Debug.Print "################################"
FileWriteBinary = False
Close iFileNum
Debug.Print Err.Description & "(" & Err.Number & ")"
End Function
I am getting a permission denied error in VBA.Kill and Open Can any one help me???
You are forgetting to close the file when the function returns, you currently only do so when there is an error.
The file handle persists between runs of you code so when you re-run it you attempt to operate on a file that is already open and explicitly locked, causing an error.