I'm trying to have VB open a basic 1 line settings text file but I want it (for first time users) to create the file if it doesn't exist, but I can't open the file without getting an error. How do I go about creating this file if it doesn't exist?

At present I have this for the opening/reading of the file, but as soon as it hits the line with the open command it gets Runtime Error 53 'File not found':

Private Sub Form_Load()
Dim content As String
Open "H:\settings.txt" For Input As #1
Input #1, content
Set Image1.Picture = LoadPicture(content)
Close #1
End Sub

What you want is called error trapping. use 'On error goto'

The logic is that it will catch the errors then jump to the location you specific (in this case evilBadger) then run that code. To make the program jump back to the line that had the error you use resume, or to skip that line then resume next

To turn off error trapping, use 'on error goto 0' that is Zero

Private Sub Badger
On error goto EvilBadger 

<YOUR CODE HERE>


Exit sub ' must have an exit sub before the error trapping starts, otherwise VB will keep on running thru all the code
:Evilbadger
<CODE> 
or
call createstartupfile
Resume

end sud

Hi,
Open the file in output mode it will create the file automatically if doesnot exist

' Return True if a file exists

Function FileExists(FileName As String) As Boolean
    On Error GoTo ErrorHandler
    ' get the attributes and ensure that it isn't a directory
    FileExists = (GetAttr(FileName) And vbDirectory) = 0
ErrorHandler:
    ' if an error occurs, this function returns False
End Function

Private Sub Form_Load()
If FileExists("H:\settings.txt") = True Then
    MsgBox "Your Code If File exists"
Else
    MsgBox "Your Code if File not found"
End If
End Sub
commented: really nice +1
commented: Great code +1
' Return True if a file exists

Function FileExists(FileName As String) As Boolean
    On Error GoTo ErrorHandler
    ' get the attributes and ensure that it isn't a directory
    FileExists = (GetAttr(FileName) And vbDirectory) = 0
ErrorHandler:
    ' if an error occurs, this function returns False
End Function

Private Sub Form_Load()
If FileExists("H:\settings.txt") = True Then
    MsgBox "Your Code If File exists"
Else
    MsgBox "Your Code if File not found"
End If
End Sub

I've put that in with the MsgBox command just to see if it's working and if the file doesn't exist it comes up with the "Your code if File not found" message box, however I created the file and entered some text, but it still comes up with "Your code if File not found"

Ignore above...I created the wrong filename :P

Hi,
Its working fine. I will give another way using Windows API.

API Declaration
Use this in a Module

Private Declare Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsA" (ByVal pszPath As String) As Long

Public Function IsFileExists(sPath As String) As Boolean
   IsFileExists = (PathFileExists(sPath) <> 0)
End Function

Usage:
Same as the Above Function
Ex MsgBox IsFileExists ("D:\Settings.txt")

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.