Solved

Reading File Using Instr

Posted on 2004-09-05
2
883 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

756 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