Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2009
    Posts
    35

    Unanswered: How to go to next line??

    Hi, can anybody help or at least point me in the right direction. I have a text file :

    TCS
    LIN01
    Lin02
    Lin03
    TCS
    Lin01
    Lin02
    PTC
    Lin01
    Lin02
    TCS
    Lin01
    Lin02
    LPL
    Lin01
    Lin01

    Basically it is a list of different coupon types with their corresponding lines. What I need is a function that will allow me pick JUST the TCS and their lines and output them to a new file. I can output the TCS but i need the code to skip all other coupon types and their lines.

    The code I have attached may not be relevant as this inserts "000" into the line. But this can be changed to just output the TCS coupons.

    Do Until EOF(intInHandle)
    Line Input #intInHandle, strInLine
    If Left(strInLine, 4) = "TCS " Then
    strOutLine = Left(strInLine, 11) & "000" & Mid(strInLine, 12)
    Else
    strOutLine = strInLine
    End If

    Basically it is a function that allows the code to step through each line or go to next line?

    Thanks

  2. #2
    Join Date
    Jun 2004
    Location
    Arizona, USA
    Posts
    1,848
    Can you change the format of the file? Because, as is, it's not very 'friendly' for your needs.

    There's no way to determine this data, unless you parse the entire file.

    You could load the whole file into a single string with one GET, then split it into an array of string (one array per line in the file.) Then, iterate through the array. This would be significantly faster than reading the whole file over and over. If the source file isn't too large (no more than approximately 10% of memory size) then this may be a viable alternative. If the file is larger than this, you can still use this approach, but, you would read/process the source file in chunks, which would take more code.

    ex:
    Code:
    Dim Lines() as string  ' dynamic array of string
    Dim N as Long
    
    strInLine = Space(LOF(#intInHandle))  ' Size the input buffer
    Get #intInHandle, , strInLine
    Lines = Split(strInLine, vbNewLine)
    strInLine = ""
    Close #intInHandle
    
    For N = 0 to UBound(Lines)
      debug.print Lines(N)
      ' Process the lines of the file, now in the Lines array
    Next N
    Do you have a list of all the possible coupon types? (how many different coupon codes are there?) Or is there a way to determine (programatically) the lines which contain a coupon type? (Your example file data only has three letter coupon types. Is THIS a distinguishing property of the coupon type line?)

    In your code, you would need to add a flag to indicate that you've found a matching coupon type. You would set this flag to TRUE. If the flag is true, you need to process the following line(s) you read, as per your code. If one of the following lines contains a different coupon code, you need to reset the flag (set it to false.)

    Let's assume that coupon types will always be 4 characters or less in length. To process this, something along the lines of the code below should come close.
    Code:
    Dim blFound as Boolean
    
    ' Use code per above to load the file data into the Lines array
    ' within the loop,
    For N = 0 to UBound(Lines)
      If LenB(Lines(N)) > 4 
          If blFound then
            ' Output the TCS Coupons
          End If
      Else
        If Lines(N) = "TCS " then 
          blFound = True
        Else
          blFound = False
        End If
      End If
    Next N
    Last edited by loquin; 06-30-09 at 20:01.
    Lou
    使大吃一惊
    "Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
    "I have my standards. They may be low, but I have them!" - Bette Middler
    "It's a book about a Spanish guy named Manual. You should read it." - Dilbert


  3. #3
    Join Date
    Feb 2009
    Posts
    35

    How to go to next line?

    Hi loquin, unfortunately the file is produced in that format. Also the file(s) do change, but I can always alter the size myself if need be.

    I am only new to the VBA side of things in MS Access, so I have had to do my homework to get a grasp of what you are detailing here. Just a few queries :

    strInLine = Space(LOF(#intInHandle)) this line is appearing as a fault. Does this coding need to appear as a new module or inserted into some part of the code I added to the 1st mail?

    If LenB(Lines(N)) > 4 is LenB declared earlier on in the coding. Sorry again but my understanding is very basic.

    Len

    There are 4 different coupon types which are included within the file but I am only interested in the TCS and their corresponding line(s). Each of the Lines relating to their coupon are unique, i.e

    TCS = Lin01, Lin02 etc
    LPL = Line01, Line02 etc
    PTC = Line01N, Lin02N etc
    PTP = Line01NN, Lin02NN etc.

    Hope this helps.

    Ste

  4. #4
    Join Date
    Jun 2004
    Location
    Arizona, USA
    Posts
    1,848
    what version of VB are you using?

    re the strinline = space... line. I forgot to tell you to open the file for binary input.

    You've used freefile to to get the file handle... so
    Code:
    Open "YourFileName" For Binary as #intInHandle
    strInLine = Space(LOF(#intInHandle))  ' Size the input buffer
    Get #intInHandle, , strInLine
    LenB is a VB6 function to get the length of the variable in bytes. You might want to use the LEN function instead.

    Now. When checking to see if there is another Coupon Type to reset the flag.

    You can use the instr function and mid function to check to see if the first 4 characters of the target line begin with LPL , PTC , or PTP .

    Also, you said VBA and Access - this is not standalone VB, but is VBA? that makes a difference. some functions which are available in VB6 aren't available in VBA for Access. LenB is one of them. I'm not sure about MID and INSTR...

    If the coupon type line is 4 characters long, the code should work as is.

    If the coupon type line can be greater than 4 characters, you'll have to check the first 4 characters of every line, rather than checking the line length (which is faster.) The following code would be placed inside the For-Next Loop, replacing the existing code that's inside the loop.

    Code:
    Const strChk as String = "TCS LPL PTC PTP"  
    Dim chkPos as Integer
    
    ' get the position of the first 4 characters of the line in the check string (0 if not found)
    ChkPos =  instr(strChk, Left(Lines(N),4)) 
    
    Select Case chkPos
      0 
        ' The 1st 4 characters don't match ANY of the Coupon Types.  
        If blFound then
          ' Process the Line as it is assigned to a TCS coupon Type
        End If
      1
        ' The Coupon Type is TCS.  Set the Found flag & go to the next line.
        blFound = True
      Else
        ' The line is the start of a different coupon type.  Reset the found flag
        blFound = False
    End Select
    Last edited by loquin; 07-01-09 at 20:58.
    Lou
    使大吃一惊
    "Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
    "I have my standards. They may be low, but I have them!" - Bette Middler
    "It's a book about a Spanish guy named Manual. You should read it." - Dilbert


  5. #5
    Join Date
    Feb 2009
    Posts
    35
    It is VBA which I am using, that answers a few things..

    I get the jist of the type of code which I need to use and will try and find equilvalent functions within VBA.

    I will have a crack at it and post back when / if I have any problems.

    Thanks

Posting Permissions

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