Check if file is in use

K1600

Board Regular
Joined
Oct 20, 2017
Messages
181
I have a workbook that on clicking 'Submit' on a User Form opens another workbook and places data from the User Form into the as yet unopened workbook. Some time back someone on here kindly helped me with some coding to do just that and it works great. I have now added another User Form and want this to update to the workbook as previously. I've used the exact same coding and the file reference is correct but when I click 'Submit' I get an error message saying "FileName \ Path Not Found". I've put the code below, it's a straight lift but I've had to change a few bits to protect the company details, I've replaced it with "XXXX".

Any help would be greatly appreciated.

Code:
Private Sub CMD_Update_Click()
'Check if Master Workbook Is In Use  -  Also needs additional code in (Function FileInUse(ByVal FileName As String) As Boolean) below!!
Application.ScreenUpdating = False 'Stops screen from showing during check
Dim Response As VbMsgBoxResult
Dim FullFileName As String
    FullFileName = "\\XXXX\Returns v.2.0.xlsx"
    If Dir(FullFileName, vbDirectory) <> vbNullString Then
Do
If FileInUse(FullFileName) Then
Response = MsgBox("Database in use." & Chr(10) & _
"Do You Want To Retry?", 37, "File In Use")
'cancel pressed
If Response = 2 Then Exit Sub
Else
Exit Do
End If
Loop
Else
MsgBox FullFileName & Chr(10) & "FileName \ Path Not Found", 48, "Not Found"
Exit Sub
    End If
'Adds new workplace to database
Set wbk = Workbooks.Open("\\XXXX\Returns v.2.0.xlsx", WriteResPassword:="XXXX", IgnoreReadOnlyRecommended:=True) 'Sets Workbook
Dim Found As Range
Cancel = 0
If Me.CboNewWorkplace.value = "" Then
Cancel = 1
CboNewWorkplace.SetFocus
End If

If Cancel = 1 Then
MsgBox "Enter a new workplace", vbCritical, "XXXX"
Exit Sub
End If

Set Found = Sheets("Authorised Users").Range("A:A").Find(What:=Me.TxtPIN.value, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Found Is Nothing Then
MsgBox "PIN Number Not Found In Database", vbCritical, "XXXX"
Else
Sheets("Authorised Users").Cells(Found.Row, Columns.Count).End(xlToLeft).Offset(0, -1).value = Me.CboNewWorkplace.value
    End If
'Closes master data workbook
wbk.Save
wbk.Close
'Clear UsrFrmNewWorkplace entries
TxtPIN.value = ""
TxtSurname.value = ""
TxtCurrentWorkplace.value = ""
    CboNewWorkplace.value = ""
'Unload UsrFrmNewWorkplace
Unload Me
MsgBox "                   Your workplace has now been amended as requested." & vbCr & _
"" & vbCr & _
        "Please check when you next use the system to ensure your workplace is correct", , "XXXX"
'Re-Load User Form
UsrFrmUserConsole.Show
End Sub

'Part of checking if Master Workbook is in use
Function FileInUse(ByVal FileName As String) As Boolean
End Function
On Error Resume Next
Open FileName For Binary Access Read Lock Read As #1
Close #1
FileInUse = CBool(Err.Number > 0)
On Error GoTo 0
End Function
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
FullFileName = "\\XXXX\Returns v.2.0.xlsx"
Check your File Explorer setting. If you happen to have 'Hide extensions for known file types' checked then that .xlsx will cause a problem with the Dir looking for the file

Possibly just an error copying to the forum but you have a stray End Function line in the posted code.
Rich (BB code):
'Part of checking if Master Workbook is in use
Function FileInUse(ByVal FileName As String) As Boolean
End Function
On Error Resume Next
Open FileName For Binary Access Read Lock Read As #1
Close #1
FileInUse = CBool(Err.Number > 0)
On Error GoTo 0
End Function
 
Upvote 0
Firstly sorry for the delay, I lost all internet whilst away and have only just got it back! Thanks for the suggestions, I tried both but neither assisted me but I have now resolved it after much faffing and playing. It was due to the properties/attributes of the file being marked as 'hidden' in windows explorer. This was a conscious decision to stop the file showing on most computers but it seems this is my problem. Does anyone by any chance know how I can get it to open a file if the attributes are set to hidden?
 
Upvote 0
Does anyone by any chance know how I can get it to open a file if the attributes are set to hidden?
Not an area of strength of mine, but you could see if anything here is any use to you.
 
Upvote 0
I haven't looked through the link Peter supplied, but try
VBA Code:
If Dir(FullFileName, vbHidden) <> vbNullString Then
 
Upvote 0
Thanks both, as ever very helpful. Fluff, your suggestion worked a treat, thanks.
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top