Results 1 to 3 of 3

Thread: some VB code help.....?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2003
    Location
    newcastle england
    Posts
    3

    Question some VB code help.....?

    hi

    In my program i want to search through the file to check to see the number
    which user enter, does not exist in the file already and also to keep track of saved numbers in the file, can anyone give me some code to meet these things?

    thanks.
    jamshid
    www.linkstrain.com

  2. #2
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    First of all are you gona be creating the file or does the file already exist?
    Secondly are all numbers going to be less than 10 ie 0-9 cos this way one character can be used for each letter. Or if you want to be able to do 0-255 you could then uses the ascii code for each character as the number. If the range you want is bigger then you'll have to come up with an idea for what will be between each number to seperate them.

    Either way I suggest you open the file as follows:
    VB Code:
    1. Open FileName For Random As #1 Len=1
    This way to get data from a certain position in the file use:
    VB Code:
    1. Get #1, Pos, Letter
    Where Letter is should be a Byte type, it will be given the ascii code for the character.
    To Change/Add a number use this:
    VB Code:
    1. Put #1, Pos, NewLetter
    Once again NewLetter is abyte that will be the ascii code for the character.

    Notice that if your using the range 0-255 you can ignore the stuff about it being a ascii code and treat it as the number.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  3. #3
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411
    An option if your file is simply a list of numbers (one number to a line in the file). This code will help you as a starting point. The code doesn't care what the list contains. It looks at each line at a time and compares it to your target.

    I think the best thing for you to learn here are the file opening and file stepping code. The EOF() function is very important especially in sequential (ASCII) files.

    Regards


    Code:
    Option Explicit
    
    Private Sub Command1_Click()
      Dim result As Long
      result = FindLineInFile("123", "c:\somefile.txt")
      If result = 0 Then
        ' this means line was not found or there was an error opening the file.
        MsgBox "not found"
      Else
        MsgBox "found at line number " & CStr(result)
      End If
    End Sub
    
    Private Function FindLineInFile(sLinetoFind As String, sFileName As String) As Long
      
      Dim lLine As Long       'line number found at
      Dim f As Long           'filehandle
      Dim bFound As Boolean   'flag set when line is found
      Dim sLine As String     ' current line read from file
      
      f = FreeFile
      bFound = False
      
      On Error GoTo err_FindLineInFile  ' if the file does not exist
                                        ' then the open command will fail
      Open sFileName For Input As #f
      
      While Not bFound And Not EOF(f)
        Line Input #f, sLine
        lLine = lLine = 1     ' increment the line count
        If sLine = sLinetoFind Then
          bFound = True
        End If
      Wend
      FindLineInFile = lLine
      
    err_FindLineInFile:
      On Error Resume Next    ' reset the error handler
      Close #f                ' close the file - may generate an error if it was
                              ' never opened, but the error handler will prevent a crash
    End Function
    Paul Lewis

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width