Picture
 VB Tricks & Tips
 Strings
 Controls
 Files
 Links
Picture

Visitors:

Picture
 Files

Here you can find some VB code snippets that are useful when dealing with file i/o:


Private Function FileExists(fileName As String) _
  As Boolean

  ' Checks to see if a file exists on disk.
  ' Returns True if the file is found, or
  ' False if it is not.
 
  ' Set up an error trap.
  On Error GoTo noFile
 
    ' Attempt to open the file.
    Open fileName For Input As #1
    Close #1
   
    ' Return True if no error occurs.
    FileExists = True
    Exit Function
 
noFile:

  ' If the file can't be opened,
  ' return False.
  FileExists = False
 
End Function  ' FileExists


Private Function FindRecordCount()

  ' The FindRecordCount function determines
  ' the number of records in the current
  ' file. Note that this function is called
  ` only if a file is open.
 
  ' The variable fileNumber represents the
  ' open file in the active child form.
  FindRecordCount = LOF(fileNumber) / _
    Len(MyRecord)

End Function  ' FindRecordCount


Function IsAlreadyOpen(testFileName As String) _
  As Boolean

  ' The IsAlreadyOpen function determines whether
  ' a file that the user has requested is already
  ' open. (If it is, the program avoids the attempt
  ' to open it a second time.)
 
  Dim i

  ' Start out assuming the file is not open.
  IsAlreadyOpen = False
 
  ' Loop through the open forms, not including
  ' the MDI parent form. Use Visual Basic's
  ' Forms collection to identify each form in
  ' turn. (The Count property tells how many
  ' forms are currently open.)
  For i = 1 To Forms.Count - 1
    With Forms(i)
   
      ' If the user has entered a file name
      ' that matches one of the open files,
      ' the function returns a value of True.
      If Trim(.formsFileName) = Trim(testFileName) _
        Then IsAlreadyOpen = True
    End With
  Next i

End Function  ' IsAlreadyOpen


[VB Tricks & Tips] [Strings] [Controls] [Files] [Links]

Author: John N. Kostaras       Last modification: 2 May 2002
URL: https://jnkvb.tripod.com/