Search code examples
vbafor-loopflat-file

How to get the final value in one of the columns generated by For Loop in a New Column?


I am using VBA. The For() Loop in my progam is:

        For i = 5 To 23
            Status$ = GetScreen(4,i,3)
            DateofDPS$=GetScreen(12,i,8)
            TimeofStatus$=GetScreen(23,i,8)
            TypeofDPS$=GetScreen(34,i,8)
            Tech$=GetScreen(41,i,8)
            techName$=GetScreen(50,i,30)

            If Asc(GetScreen(43,i,6))<>32 Then
                Write #OutputFile, DPSNo$, Status$,DateofDPS$,TimeofStatus$,TypeofDPS$,Tech$,techName$
            Else
            End If
        Next i

The result stored in the #OutputFile is

Num |   STAT    |   DAT         |   TIM         |   TYP |   TEC |   techName
----+-----------+---------------+---------------+-------+-------+------------
477 |   P       |   12/17/2014  |   17:20:34    |   EX  |   175 |   UME 
477 |   I       |   12/17/2014  |   17:40:47    |   EX  |   255 |   VIN 
477 |   O       |   12/17/2014  |   17:40:58    |   EX  |   255 |   VIN 
804 |   P       |   12/11/2014  |   11:01:39    |   EX  |   207 |   KAU 
804 |   I       |   12/11/2014  |   14:34:07    |   EX  |   253 |   JOS 
804 |   O       |   12/11/2014  |   14:34:26    |   EX  |   253 |   JOS 
402 |   P       |   11/13/2014  |   22:08:50    |   EX  |   243 |   SHA 
402 |   I       |   11/14/2014  |   05:04:04    |   EX  |   247 |   LAK 
402 |   O       |   11/14/2014  |   05:04:08    |   EX  |   247 |   LAK

I want to add another column finalAction and the result should look like:

Num |   STAT    |   DAT         |   TIM         |   TYP |   TEC |   techName    |   finaAction
----+-----------+---------------+---------------+-------+-------+---------------+-------------
477 |   P       |   12/17/2014  |   17:20:34    |   EX  |   175 |   UME         |   VIN
477 |   I       |   12/17/2014  |   17:40:47    |   EX  |   255 |   VIN         |   VIN
477 |   O       |   12/17/2014  |   17:40:58    |   EX  |   255 |   VIN         |   VIN
804 |   P       |   12/11/2014  |   11:01:39    |   EX  |   207 |   KAU         |   JOS
804 |   I       |   12/11/2014  |   14:34:07    |   EX  |   253 |   JOS         |   JOS
804 |   O       |   12/11/2014  |   14:34:26    |   EX  |   253 |   JOS         |   JOS
402 |   P       |   11/13/2014  |   22:08:50    |   EX  |   243 |   SHA         |   LAK  
402 |   I       |   11/14/2014  |   05:04:04    |   EX  |   247 |   LAK         |   LAK  
402 |   O       |   11/14/2014  |   05:04:08    |   EX  |   247 |   LAK         |   LAK    

The For() Loop writes to a Flat File.

I want to add the Final Name in techName Column and repeat the same.

Any way of doing this?


Solution

  • First option that comes to mind:

    create a variable to hold "LastDPONo" then watch for a change between the current record DPONo$ and the "LastDPONo" value.

    When a change occurs you have found your last record for that DPONo$ and you can then identify the Tech$ assigned to the last update on that DPSNo$

    You will either need to identify the correct Tech$ before writing your rows out to the output file or you will have to modify the output file once you find the Tech$ value.

    1. If you wish to identify the Tech$ first you could write the data for each row to an array or Recordset as you look for the last consecutive row of a DPSNo$ to find the Tech$ you need. Once you find the Tech$ you can loop through the array or recordset you created and write the value of each entity o the array or record of the recordset into your output file with the Tech$ added at the end.

    2. To modify the text file you can add an extra column and then adapt a method outlined here: http://software-solutions-online.com/2014/03/15/vba-modify-existing-text-file/#Jump2