Solved

Reading File Using Instr

Posted on 2004-09-05
2
880 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 85

Accepted Solution

by:
Mike Tomlinson earned 100 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now