Welcome to Dream.In.Code
Become a VB Expert!

Join 149,957 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,344 people online right now. Registration is fast and FREE... Join Now!




Search for strings

 
Reply to this topicStart new topic

Search for strings

nirmala.kk
6 Oct, 2008 - 04:06 AM
Post #1

New D.I.C Head
*

Joined: 6 Oct, 2008
Posts: 3

How can i search a particular string from a document file using VB 6
User is offlineProfile CardPM
+Quote Post

akhileshbc
RE: Search For Strings
6 Oct, 2008 - 04:29 AM
Post #2

D.I.C Head
Group Icon

Joined: 26 Sep, 2008
Posts: 177



Thanked: 3 times
Dream Kudos: 50
My Contributions
Hi nirmala,
pls check the Visual Basic Tutorials and Snippets

u will find many samples. V wont give u any code. Only assist u in ur problems smile.gif

** Edit ** Link removed
User is offlineProfile CardPM
+Quote Post

ragingben
RE: Search For Strings
7 Oct, 2008 - 06:30 AM
Post #3

D.I.C Head
Group Icon

Joined: 7 Oct, 2008
Posts: 60



Thanked: 2 times
Dream Kudos: 100
My Contributions
Hi there, the function you are looking for is InStr, this returns the location of one string within another.

Below is an example:

CODE

Private Sub lookForString()
    
    'input text
    Const bodyString As String = "This is a search string"
    'search string
    Const searchString As String = "search"
    
    'if string found (returns different to 0)
    If InStr(1, bodyString, searchString) <> 0 Then
        MsgBox "WOHOO!!!"
    Else
        MsgBox "DOH!!!"
    End If
    

End Sub


To get the body string (the one you are looking in) you will need to get to grips with basic file handling - I have included a crude example with no error handling or anything just to get you going

CODE

Private Function readFile() As String

    'a integer for the file handle
    Dim fh As Integer
    'the total text to be read
    Dim totalText As String
    'the current line being read
    Dim line As String
    
    'the file to be opened
    Const file As String = "C:\Test.txt"
    
    'assign a free file handle to the variable
    fh = FreeFile
    
    'ope the file for input to your program
    Open file For Input As #fh
        
        'repeat this code while not at the end of the file
        Do While Not EOF(fh)
            
            'put the next line of file into the line variable
            Line Input #fh, line
            
            'add this line to toal read input
            totalText = totalText + line
        
        Loop
    
    'close file
    Close #fh
    
    'assign all read input to function to be returnd
    readFile = totalText

End Function


Hopefully this will help a bit!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 06:04PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month