Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Reading File Using Instr

Posted on 2004-09-05
2
Medium Priority
?
887 Views
Last Modified: 2013-12-25
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
0
Comment
Question by:jamesbenson
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 400 total points
ID: 11984823
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"
        Else
            Open fileName For Append As #1
            Print #1, checkForString
            Close #1
            MsgBox checkForString, vbInformation, "Appended to File"
        End If
    Else
        MsgBox fileName, vbCritical, "File not found"
    End If
End Sub
0
 

Author Comment

by:jamesbenson
ID: 11991566
thanx so much, A gr8 answer and so I have doubled the no. of point
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

885 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