?
Solved

Reading from a file

Posted on 2000-01-04
4
Medium Priority
?
131 Views
Last Modified: 2010-05-02
If I want to open a file for reading, how do I check to first be sure the file exists?
0
Comment
Question by:Mirado
  • 2
4 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 40 total points
ID: 2324277
Function FileExist(strDir As String) As Boolean
Dim tmpStr As String
tmpStr = Dir(strDir)
If Len(tmpStr) > 0 Then
    FileExist = True
    Else
    FileExist = False
    End If
End Function

Function PathExist(strDir As String) As Boolean
Dim tmpStr As String
tmpStr = Dir(strDir, vbDirectory)
If Len(tmpStr) > 0 Then
    PathExist = True
    Else
    PathExist = False
    End If
End Function
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2324526
Actually that FileExist function WON'T work for some files (hidden, systems etc). Here's the correct and more compact code...

   Function FileExist(strDir As String) As Boolean

      FileExist = Len(Dir("D:\A.frm", vbArchive + vbHidden + vbReadOnly + vbSystem)) > 0)

   End Function

Another solution would be to use the FileLen function like this (just another way to do it)...

   Function FileExist(Filename As String) As Boolean

      On Error GoTo Errorhandler
 
      Call FileLen(Filename)
      FileExist = True
      Exit Function

   Errorhandler:
      FileExist = False

   End Function
0
 
LVL 30

Expert Comment

by:Axter
ID: 2324945
Actually, you have some errors in your code.  You're missing the strDir variable and you have an extra brace at the end.
The following is the correct more compact version.

Function FileExist(strDir As String) As Boolean
      FileExist = Len(Dir(strDir, vbArchive + vbHidden + vbReadOnly + vbSystem)) > 0
End Function

0
 

Author Comment

by:Mirado
ID: 2330196
Thanks. That did just what I needed. The first reponse would have been fine for what I was needing but thanks for the added comment too.

0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month5 days, 22 hours left to enroll

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question