Reading File Using Instr

Basically I've spent hours agonising over some code which will not work for me.

I'm trying to

Read a .txt file checking using instr whether a string exists in the file
If it exists a msgbox "already exists" appears
If it does not exist in the file then appending it to the file
Who is Participating?
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Option Explicit

Private Sub Form_Load()
    checkFile "c:\someFile.txt", "someValue"
End Sub

Private Sub checkFile(ByVal fileName As String, ByVal checkForString As String)
    Dim entireFile As String
    If Dir(fileName) <> "" Then
        Open fileName For Binary Access Read As #1
        entireFile = Input(LOF(1), 1)
        Close #1
        If InStr(entireFile, checkForString) > 0 Then
            MsgBox checkForString, vbInformation, "Already Exists"
            Open fileName For Append As #1
            Print #1, checkForString
            Close #1
            MsgBox checkForString, vbInformation, "Appended to File"
        End If
        MsgBox fileName, vbCritical, "File not found"
    End If
End Sub
jamesbensonAuthor Commented:
thanx so much, A gr8 answer and so I have doubled the no. of point
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.