Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems parsing parameters to function 1

Status
Not open for further replies.

patriciaxxx

Programmer
Jan 30, 2012
277
0
0
GB
I have the following two functions. The first one I can parse parameter as follows:-

IsFileOpen("C:\test.doc")

My question is how do I parse multiple files for the second function (arrays still baffle me a bit):-

CheckForOpenFiles()

Code:
[COLOR=#204A87]Public Function IsFileOpen(PathName As String) As Boolean
On Error GoTo ErrHandler
Dim i As Integer

If Len(Dir$(PathName)) Then
  i = FreeFile()
  Open PathName For Random Access Read Write Lock Read Write As #i
  Lock i 'Redundant but let's be 100% sure.
  Unlock i
  Close i
Else
  Err.Raise 53
End If

ExitProc:
On Error GoTo 0
Exit Function

ErrHandler:
Select Case Err.Number
  Case 70 'Unable to acquire exclusive lock.
    IsFileOpen = True
  Case Else
    MsgBox "Error " & Err.Number & " (" & Err.Description & ")"
  End Select
Resume ExitProc
Resume

End Function

Public Function CheckForOpenFiles(Files() As String) As Boolean
On Error GoTo ErrHandler
Dim i As Long
Dim lngLocks As Long
Dim strFiles() As String
Dim strMessage As String

Do
  lngLocks = 0
  For i = 0 To UBound(Files)
    If IsFileOpen(Files(i)) Then
      ReDim Preserve strFiles(lngLocks)
      strFiles(lngLocks) = Files(i)
      lngLocks = lngLocks + 1
    End If
  Next
  If lngLocks Then
    strMessage = "The following files are in use. " & _
      "Please close the application that may have it open." _
      & vbNewLine & vbNewLine
    For i = 0 To UBound(strFiles)
      strMessage = strMessage & strFiles(i) & vbNewLine
    Next
    If vbCancel = MsgBox(strMessage, vbRetryCancel, "Files in use") Then
      CheckForOpenFiles = False
      Exit Do
    End If
  End If
Loop Until lngLocks = 0
If lngLocks = 0 Then
  CheckForOpenFiles = True
End If

ExitProc:
On Error GoTo 0
Exit Function

ErrHandler:
Select Case Err.Number
  Case 53 'File doesn't exist, ignore.
    Resume Next
  Case Else
    MsgBox "Error " & Err.Number & " (" & Err.Description & ")"
End Select
Resume ExitProc
Resume

End Function
[/color]
 
Sorry, I don't understand what your problem is.
What is your meaning of "parse parameter" ????
 
Not sure if this helps, but you can pass an array or pass an array of parameters. See if this helps
Code:
Public Sub PassArray()
  Dim myValues(0 To 2) As String
  myValues(0) = "Cat"
  myValues(1) = "dog"
  myValues(2) = "fish"
  PassStandardArray myValues()
  
  PassParameterArray "Cat", "dog", "Fish"
End Sub

Public Sub PassStandardArray(ByRef myValues() As String)
  Dim i As Integer
  For i = LBound(myValues) To UBound(myValues)
    Debug.Print myValues(i)
  Next i
End Sub

Public Sub PassParameterArray(ParamArray myValues() As Variant)
  Dim i As Integer
  For i = LBound(myValues) To UBound(myValues)
    Debug.Print myValues(i)
  Next i
End Sub
 
I want to use the second function to test for multiple open files but I can’t work out how to use it?

Say I have the following three files:
C:\test_1.doc
C:\test_2.doc
C:\test_3.doc

How do I call the following function and, in this case, parse those three files

Function CheckForOpenFiles(Files() As String) As Boolean


 
Just been looking at MajP’s example, and it seems to answer my question.

The function I’m using wants the values as a Standard Array and if I parse them as your example shows then everything seems to work properly.

Thank you all for responding to this thread and especially to MajP whose example provided the solution.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top