text file operations with vb6.0

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ronin
    New Member
    • Feb 2007
    • 25

    #1

    text file operations with vb6.0

    Hello guys,

    I have a little bit of problem with textfile operations.I would like to some help from you smart people in the forum.

    I have a textfile by the name of 10117053.swd. This file contains a certain data in specific format like

    010110010001ADI TY3104200310450 001
    010110010001ADI TY3104600310460 001
    010110010001ADI TY3104800310490 001
    010110010001DD8 320200032020000 1
    010110010001ENA D 316110031628000 1
    010110010001ENA D 319140032006000 1
    010110010001GEM I 314350031435000 1
    010110010001GEM I 315240031526000 1
    010110010001HBO 314360031436000 1
    010110010001SAN SK3113300311330 001
    010110010001SIT MU3092400310240 001
    010110010001TEJ NW3103900310400 001
    010110010001TEJ NW3104700310470 001
    010110010004DD8 307200030720000 1
    010110010004DD8 320200032020000 1
    010110010004ENA D 308080030808000 1
    010110010004ENA D 308340030847000 1
    010110010004GEM I 307420030803000 1
    010110010004GEM I 308060030807000 1
    010110010004SIT MU3065300306530 001
    010110010004SIT MU3070000307000 001
    * *
    the first 12 numbers as homeid and the next 5 letters are channelname

    now i need to give the count of unique channels that are watched by the homeid in a new txtfile in the format

    homeid - count of unique channels that are watched by this id.

    for suppose homeid 010110010001 watched ADITY TWICE AND OTHER CHANNELS,BUT WE WANT THE COUNT OF THE UNIQUE CHANNELS THAT ARE WATCHED BY THESE HOMEID LIKE FOR ABOVE HOMEID 010110010001 HAS WATCHED 8 UNIQUE CHANNELS SO ON FOR EACH HOMEID.

    THE NEW TXTFILE SHOULD BE HOMEID

    010110010001 8
    010110010004 4 AND SO ON.


    NOW I AM NOT ABLE TO GET THIS i HAVE THE CODE pLEASE HAVE THE LOOK AT THE CODE

    Code:
    Option Explicit
    Dim fsys As New FileSystemObject
    Dim txtstream As TextStream
    Dim outstream As TextStream
    
    
    Private Sub cmdclick_Click()
    
     Dim j As Integer
     Dim searchline As String   ' reads each line of txtstream
     Dim filename As String     'file name which is dynamically generated
     Dim homeid As String   'to store 12 numbers
     Dim prevhomeid As String '  to comapre with other numbers
     Dim channelname As String '  to store channelname
     Dim prevchannelname As String  ' to compare prevchannelname
     Dim channelcount As Integer  ' to give channelcount
    
    
    
      If fsys.FileExists("C:\rohit program\exercise\SWD\" & filename) = True Then
                   MsgBox "file is open"
                 Else
                  MsgBox "file not found"
                 End If
                   
                   prevchannelname = ""
                   prevhomeid = ""
                   channelcount = 0
                   
        Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
           Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
                   Do Until txtstream.AtEndOfStream
                       searchline = txtstream.ReadLine
                          homeid = Mid(searchline, 1, 10)
                       If prevhomeid <> homeid Then
                                'do nothing`
                              outstream.WriteLine (homeid)
                            End If
                                 prevhomeid = homeid
                   Loop
                           
                               MsgBox " Process completed"
    End Sub
    any help would be sincerely appreciated.
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by Ronin
    ...
    NOW I AM NOT ABLE TO GET THIS i HAVE THE CODE pLEASE HAVE THE LOOK AT THE CODE
    Code:
    Option Explicit
    Dim fsys As New FileSystemObject
    Dim txtstream As TextStream
    Dim outstream As TextStream
    
    Private Sub cmdclick_Click()
    
     Dim j As Integer
     Dim searchline As String      ' reads each line of txtstream
     Dim filename As String        ' file name which is dynamically generated
     Dim homeid As String          ' to store 12 numbers
     Dim prevhomeid As String      ' to comapre with other numbers
     Dim channelname As String     ' to store channelname
     Dim prevchannelname As String ' to compare prevchannelname
     Dim channelcount As Integer   ' to give channelcount
    
    
    
      If fsys.FileExists("C:\rohit program\exercise\SWD\" & filename) = True Then
        MsgBox "file is open"
      Else
        MsgBox "file not found"
      End If
                   
      prevchannelname = ""
      prevhomeid = ""
      channelcount = 0
                   
      Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
      Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
      Do Until txtstream.AtEndOfStream
        searchline = txtstream.ReadLine
        homeid = Mid(searchline, 1, 10)
        If prevhomeid <> homeid Then
          'do nothing`
          outstream.WriteLine (homeid)
        End If
        prevhomeid = homeid
      Loop
       
      MsgBox " Process completed"
    End Sub
    Can you give us a clue as to what you don't like about your code? What does it do wrong, or not do?

    One or two things did occur to me when I glanced at the code...
    • Your code assumes the file is sorted. Is that a reasonable assumption in this case?
    • You are picking up the first 10 characters as the homeid, but you told us it was 12.
    • Assuming the file is sorted, you look as though you're probably picking up each homeid alright. Within each one, I guess you just need to do a similar check for previous channel and report each new one which shows up.

    Comment

    • vijaydiwakar
      Contributor
      • Feb 2007
      • 579

      #3
      Code:
       
      
      Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
             Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
                     Do Until txtstream.AtEndOfStream
                         searchline = txtstream.ReadLine
                            homeid = Mid(searchline, 1, 10)
      [B]'here check this homeid with the distinct homeid in array say prevHomeId()
      'if this home id doesnot exists then add this to array
      'then again run this loop here u will check home id with your arraylist
      ' increment the counter and finally u'll get the ans[/B]
                         If prevhomeid <> homeid Then
                                  'do nothing`
                                outstream.WriteLine (homeid)
                              End If
                                   prevhomeid = homeid
                     Loop
                             
                                 MsgBox " Process completed"
      End Sub
      Last edited by Killer42; Mar 10 '07, 10:46 AM. Reason: Trust me, NOBODY says "thy".

      Comment

      • Ronin
        New Member
        • Feb 2007
        • 25

        #4
        Originally posted by vijaydiwakar
        Code:
         
        
        Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
               Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
                       Do Until txtstream.AtEndOfStream
                           searchline = txtstream.ReadLine
                              homeid = Mid(searchline, 1, 10)
        [B]'here check this homeid with the distinct homeid in array say prevHomeId()
        'if this home id doesnot exists then add this to array
        'then again run this loop here u will check home id with your arraylist
        ' increment the counter and finally u'll get the ans[/B]
                           If prevhomeid <> homeid Then
                                    'do nothing`
                                  outstream.WriteLine (homeid)
                                End If
                                     prevhomeid = homeid
                       Loop
                               
                                   MsgBox " Process completed"
        End Sub
        Sir,you are right that i should use an array ,but my problem is that i do not know how to put the homeid value in arrays and compare them,basically i know what are arrays but ,how they store data or manipulate data, i have very little knowledge,perha ps any help on this would be very useful for me.
        Right know i am goind through a topic on arrays and lets see if i am able to do this.Thank you sir for your help.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by Ronin
          Sir,you are right that i should use an array ,but my problem is that i do not know how to put the homeid value in arrays and compare them,basically i know what are arrays but ,how they store data or manipulate data, i have very little knowledge,perha ps any help on this would be very useful for me.
          Right know i am goind through a topic on arrays and lets see if i am able to do this.Thank you sir for your help.
          Keep us posted on how it goes.

          However, as long as the items are definitely in sorted sequence, you don't need to use an array - your technique of checking against the previous item will work fine. You just need to extend it to handle the channels in a similar way.

          On the other hand, if they are not sorted, then yes, you will need to use something like an array to keep track of those which have already been handled.

          Comment

          • Ronin
            New Member
            • Feb 2007
            • 25

            #6
            Originally posted by Killer42
            Keep us posted on how it goes.

            However, as long as the items are definitely in sorted sequence, you don't need to use an array - your technique of checking against the previous item will work fine. You just need to extend it to handle the channels in a similar way.

            On the other hand, if they are not sorted, then yes, you will need to use something like an array to keep track of those which have already been handled.
            Sir, It seems I am not able to clearly define what i actually want. so let me try it once again

            Sir ,I have textfile which have name like "10117051.s wd" now this is a file which contains data of television audio Ratings.

            The data in this file have systematic pattern so that i can identify it.

            for example take a look at this line

            010110010001ADI TY1114700111470 001

            now the first 12 numbers ie 010110010001 are homeids followed channel name ie ADITY.

            Now sir yes,u r right that these file are sorted,but I do not want to sort these file

            I want a code which can give the count of unique channels that is watched by this id in specific format in a new textfile which should have the data like

            010110010001 8 where 8 is the number of unique channels watched by this id and so on for every unique homeid.

            now sir,my biggest concern is i am not good with arrays and how to use them,hence i am facing problem.

            if you could guide me, i would be very thankful.
            Thank you

            Comment

            • Ronin
              New Member
              • Feb 2007
              • 25

              #7
              Originally posted by Killer42
              Keep us posted on how it goes.

              However, as long as the items are definitely in sorted sequence, you don't need to use an array - your technique of checking against the previous item will work fine. You just need to extend it to handle the channels in a similar way.

              On the other hand, if they are not sorted, then yes, you will need to use something like an array to keep track of those which have already been handled.
              sir I m trying to implement arrays in my code but i am unable to do so,pls help me.here is the sample code that i m trying to do

              [code]
              Private Sub cmdclick_Click( )
              Dim j As Integer
              Dim searchline As String
              Dim filename As String
              Dim homeid As String
              Dim prevhomeid As String
              Dim channelname As String
              Dim prevchannelname As String
              Dim channelcount As Integer
              Dim channelarray() As String


              For j = 0 To lstmarket.ListC ount - 1
              If lstmarket.Selec ted(j) = True Then


              If Len(Trim(txtyea r.Text)) > 2 Or IsNumeric(Trim( txtyear.Text)) = False Then
              MsgBox "this should be 2 digit number "
              txtyear.Text = ""
              txtyear.SetFocu s
              Exit Sub
              End If

              If Trim(txtweek) <= 9 And Len(Trim(txtwee k)) < 2 Or IsNumeric(txtwe ek.Text) = False Then
              txtweek = "0" & Trim(txtweek)
              End If

              If Trim(txtday) > 7 Or IsNumeric(txtda y) = False Then
              MsgBox "Please enter number which is equal to or less than 7"
              txtday.Text = ""
              txtday.SetFocus
              Exit Sub
              End If

              filename = lstmarket.List( j) & Trim(txtyear) & Trim(txtweek) & Trim(txtday) & ".swd"
              MsgBox ("you have selected the following" & filename)
              End If
              Next j

              '********main code for listing all the channels that the house id watch*******

              If fsys.FileExists ("C:\rohit program\exercis e\SWD\" & filename) = True Then
              MsgBox "file is open"
              Else
              MsgBox "file not found"
              End If

              prevchannelname = ""
              prevhomeid = ""

              channelcount = 0

              Set outstream = fsys.OpenTextFi le("c:\channelc ount.txt", ForWriting, True)
              Set txtstream = fsys.OpenTextFi le("C:\rohit program\exercis e\SWD\" & filename, ForReading, False)
              Do Until txtstream.AtEnd OfStream
              searchline = txtstream.ReadL ine
              homeid = Mid(searchline, 1, 10)
              'I am tring put values into the array but it is not going in instead it is giving err like cannot assign an array
              'here is what i ma trying to do
              'channelarray(s earchline)=chan nelarray
              i's this right
              any examples or help would be very helpful

              'prevhomeid (homeid)
              If prevhomeid <> homeid Then
              'do nothing`
              channelname = Mid(homeid, 12, 5)
              If prevchannelname <> channelname Then
              channelcount = channelcount + 1
              End If
              outstream.Write Line (homeid)
              End If
              prevhomeid = homeid
              Loop

              MsgBox " Process completed"
              End Sub
              [\code]

              Comment

              • Ronin
                New Member
                • Feb 2007
                • 25

                #8
                have a look at this if this is the right way.

                Code:
                Option Explicit
                Dim fsys As New FileSystemObject
                Dim txtstream As TextStream
                Dim outstream As TextStream
                
                
                Private Sub cmdclick_Click()
                 Dim j As Integer
                 Dim searchline As String
                 Dim filename As String
                 Dim homeid As String
                 Dim prevhomeid As String
                 Dim channelname As String
                 Dim prevchannelname As String
                 Dim channelcount As Integer
                 Dim channelarray(100000) As String
                 Dim i As Integer
                 
                    For j = 0 To lstmarket.ListCount - 1
                     If lstmarket.Selected(j) = True Then
                  
                         
                           If Len(Trim(txtyear.Text)) > 2 Or IsNumeric(Trim(txtyear.Text)) = False Then
                              MsgBox "this should be 2 digit number "
                              txtyear.Text = ""
                              txtyear.SetFocus
                           Exit Sub
                           End If
                           
                           If Trim(txtweek) <= 9 And Len(Trim(txtweek)) < 2 Or IsNumeric(txtweek.Text) = False Then
                              txtweek = "0" & Trim(txtweek)
                           End If
                           
                           If Trim(txtday) > 7 Or IsNumeric(txtday) = False Then
                                MsgBox "Please enter number which is equal to or less than 7"
                                txtday.Text = ""
                                txtday.SetFocus
                           Exit Sub
                           End If
                               
                           filename = lstmarket.List(j) & Trim(txtyear) & Trim(txtweek) & Trim(txtday) & ".swd"
                              MsgBox ("you have selected the following" & filename)
                     End If
                   Next j
                             
                  '********main code for listing all the channels that the house id watch*******
                            
                             If fsys.FileExists("C:\rohit program\exercise\SWD\" & filename) = True Then
                               MsgBox "file is open"
                             Else
                              MsgBox "file not found"
                             End If
                               
                               prevchannelname = ""
                               prevhomeid = ""
                                 
                               channelcount = 0
                               
                    Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
                       Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
                               Do Until txtstream.AtEndOfStream
                                   searchline = txtstream.ReadLine
                                     homeid = Mid(searchline, 1, 10)
                                 For i = 0 To channelcount 
                                    If channelarray(i) <> homeid Then
                                      channelarray(i) = homeid
                                    End If
                                    If homeid <> channelarray(i) Then
                                      channelcount = channelcount + 1
                                   'channelcount = channelcount + 1
                                    End If
                                    
                                 Next i
                                    'prevhomeid (homeid)
                                   'If prevhomeid <> homeid Then
                                    'If channelarray(i) <> homeid Then
                                      
                                
                                            'do nothing`
                                       'channelname = Mid(homeid, 12, 5)
                                           'If prevchannelname <> channelname Then
                                              'channelcount = channelcount + 1
                                           'End If
                                       'outstream.WriteLine (homeid)
                                       outstream.WriteLine (homeid) & vbTab & channelcount
                                  ' End If
                                             'prevhomeid = homeid
                               Loop
                                       
                                           MsgBox " Process completed"
                End Sub
                still i am not able to get the desired output as mentioned above

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  I'm short on time, so I'm going to address one thing at a time. This piece of code...
                  Code:
                  For i = 0 To channelcount 
                    If channelarray(i) <> homeid Then
                      channelarray(i) = homeid
                    End If
                    If homeid <> channelarray(i) Then
                      channelcount = channelcount + 1
                    End If
                  Next i
                  ...has some serious problems.
                  • For starters, is this array supposed to hold channels, or homeids? If the former, then you've used it in the wrong place. If the latter, then the name is confusing.
                  • If you follow the execution you will find that it is simply going to place each homeid in turn into the first (the "zeroth") occurence of the array.
                  • The second condition tested (homeid <> channelarray(i)) can never possibly be True, since you have just just set the two variables equal.
                  That's enough for today's lesson. :)

                  Comment

                  • Ronin
                    New Member
                    • Feb 2007
                    • 25

                    #10
                    textfile operations

                    Originally posted by Killer42
                    I'm short on time, so I'm going to address one thing at a time. This piece of code...
                    Code:
                    For i = 0 To channelcount 
                      If channelarray(i) <> homeid Then
                        channelarray(i) = homeid
                      End If
                      If homeid <> channelarray(i) Then
                        channelcount = channelcount + 1
                      End If
                    Next i
                    ...has some serious problems.
                    • For starters, is this array supposed to hold channels, or homeids? If the former, then you've used it in the wrong place. If the latter, then the name is confusing.
                    • If you follow the execution you will find that it is simply going to place each homeid in turn into the first (the "zeroth") occurence of the array.
                    • The second condition tested (homeid <> channelarray(i)) can never possibly be True, since you have just just set the two variables equal.
                    That's enough for today's lesson. :)


                    thnks for the reply,now let me thnk once again I have a textfile which has some homeids and channelname .
                    like
                    010110010001DD8 507310050731000 1
                    010110010001DD8 507310050731000 1
                    010110010001ENA D 507300050730000 1
                    010110010001ETV 2 507320050733000 1
                    010110010001ETV 2 507360050736000 1
                    010110010001ETV 2 507370050808000 1
                    010110010003ETV 2 507360050736000 1
                    010110010003ETV 2 507370050808000 1
                    010110010004ETV 2 507360050736000 1
                    010110010004ETV 2 507370050808000 1
                    010110010004ETV 2 509410050941000 1
                    010110010004GEM I 511120051133000 1

                    now the first 10 digit is homeid next 2 is member id ie
                    0101100100 is homeid and 01 is memeber id and next is channelname and channelname cannot be more than 5 letters.

                    now i want to compare these homeid in such a way so that i can get the count of unique channels that are watched by these homeids in a new text file in the format

                    0101100100 3 where 010110010 is homeid and 3 is the count of unique channels that is watched by these id,so on for every id.

                    any help or idea how could i do it.sir i am not that at programming but i m still trying,i sincerely thank u for ur help.:)
                    Last edited by Ronin; Mar 16 '07, 08:33 AM. Reason: mistake

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      I'd suggest that to simplify things, rather than setting up two arrays (for homeid and channel) you just go with one. Here’s roughly how I think I would do it...

                      Read your file, and on each line you pull out the homeid and channel to a string (skip over the member id) by using Mid() function (and I suppose the Instr() function if the channel name can vary in length). So for example, after reading the first line you end up with this string: "0101100100 DD8"

                      Once you have this string, you loop through all the entries in your array until you find a match. If you don’t find one, add it to the end.

                      Keep on doing this until you hit the end of the file.

                      Once you have finished reading the file, your array will contain a complete list of all the unique homeid/channel combinations. (To move forward, you will probably want to sort them into order. If you’re not sure how to do that, try a bit of a search for VB sorting algorithms – it’s really quite simple. There are all sorts of sophisticated methods of sorting, but the simplest is just to keep looping through the array and swapping things that aren’t the right way around, until there aren’t any to swap. Otherwise known as the “bubble sort”.)

                      Assuming your array is in an ordered sequence, you can just do a FOR loop from start to finish. Add up the number of entries you find for each homeid, and you have your result.

                      I think this logic will produce the result you want. We’re discouraged from providing complete code solutions on TheScripts these days, as we want to try and ensure that people think about things and put in some real work themselves, rather than simply doing a copy/paste operation.

                      Comment

                      • Ronin
                        New Member
                        • Feb 2007
                        • 25

                        #12
                        Originally posted by Killer42
                        I'd suggest that to simplify things, rather than setting up two arrays (for homeid and channel) you just go with one. Here’s roughly how I think I would do it...

                        Read your file, and on each line you pull out the homeid and channel to a string (skip over the member id) by using Mid() function (and I suppose the Instr() function if the channel name can vary in length). So for example, after reading the first line you end up with this string: "0101100100 DD8"

                        Once you have this string, you loop through all the entries in your array until you find a match. If you don’t find one, add it to the end.

                        Keep on doing this until you hit the end of the file.

                        Once you have finished reading the file, your array will contain a complete list of all the unique homeid/channel combinations. (To move forward, you will probably want to sort them into order. If you’re not sure how to do that, try a bit of a search for VB sorting algorithms – it’s really quite simple. There are all sorts of sophisticated methods of sorting, but the simplest is just to keep looping through the array and swapping things that aren’t the right way around, until there aren’t any to swap. Otherwise known as the “bubble sort”.)

                        Assuming your array is in an ordered sequence, you can just do a FOR loop from start to finish. Add up the number of entries you find for each homeid, and you have your result.

                        I think this logic will produce the result you want. We’re discouraged from providing complete code solutions on TheScripts these days, as we want to try and ensure that people think about things and put in some real work themselves, rather than simply doing a copy/paste operation.

                        Hey that was really cool ,I really got the logic how to do it and thanks a ton for ur help

                        Comment

                        • Killer42
                          Recognized Expert Expert
                          • Oct 2006
                          • 8429

                          #13
                          Originally posted by Ronin
                          Hey that was really cool ,I really got the logic how to do it and thanks a ton for ur help
                          Glad to help. :)

                          It's a lot more satisfying for me, and helpful for you, if I can help you to understand your process rather than just posting a bunch of code for you to copy.

                          Comment

                          • Ronin
                            New Member
                            • Feb 2007
                            • 25

                            #14
                            mistake

                            Originally posted by Killer42
                            Glad to help. :)

                            It's a lot more satisfying for me, and helpful for you, if I can help you to understand your process rather than just posting a bunch of code for you to copy.

                            Hello sir,
                            I am writing it again,but as you have said i did the same thing,but my mam told

                            me that the logic is wrong and u need to check the logic.

                            so sir i m giving the code again and if u can modify the code it will be very helpful.

                            now as per my description we need to have the count of Unique channel that are being watched by different homeid in a new textfile something like this suppose the homeid 0101100100 has watched 20 unique channels then
                            output file should contain 0101100100 20.

                            now sir as per your logic I wrote the program and the results that i m able to generate is that i have the unique channelname that are being watched by this homeid something like this

                            0101100100GEMI
                            0101100100SITMU
                            0101100100DD8
                            0101100100ENAD
                            0101100100ETV2
                            0101100100GEMI
                            0101100100SITMU
                            0101100300DISNY
                            0101100300ENAD
                            0101100300GEMI
                            0101100300MAATV
                            0101100300NDT24

                            but we want the count of all unique channels watched by a particular homeid in a particular format

                            0101100100 8
                            0101100300 9 and so on

                            sir pls help me here is the code that i have written
                            Code:
                            
                            Option Explicit
                            Dim fsys As New FileSystemObject
                            Dim txtstream As TextStream
                            Dim outstream As TextStream
                            
                            
                            Private Sub cmdclick_Click()
                             Dim j As Integer
                             Dim searchline As String
                             Dim filename As String
                             Dim homeid As String
                             Dim prevhomeid As String
                             Dim channelname As String
                             Dim channelcount As Integer
                             Dim channelarray(0 To 100000) As String
                             Dim newchannelarray(0 To 100000) As String
                             Dim carray As String
                             Dim prevcarray As String
                             Dim i As Double
                             
                            
                               prevcarray = ""
                                For j = 0 To lstmarket.ListCount - 1
                                 If lstmarket.Selected(j) = True Then
                              
                                     
                                       If Len(Trim(txtyear.Text)) > 2 Or IsNumeric(Trim(txtyear.Text)) = False Then
                                          MsgBox "this should be 2 digit number "
                                          txtyear.Text = ""
                                          txtyear.SetFocus
                                       Exit Sub
                                       End If
                                       
                                       If Trim(txtweek) <= 9 And Len(Trim(txtweek)) < 2 Or IsNumeric(txtweek.Text) = False Then
                                          txtweek = "0" & Trim(txtweek)
                                       End If
                                       
                                       If Trim(txtday) > 7 Or IsNumeric(txtday) = False Then
                                            MsgBox "Please enter number which is equal to or less than 7"
                                            txtday.Text = ""
                                            txtday.SetFocus
                                       Exit Sub
                                       End If
                                           
                                       filename = lstmarket.List(j) & Trim(txtyear) & Trim(txtweek) & Trim(txtday) & ".swd"
                                          MsgBox ("you have selected the following" & filename)
                                 End If
                               Next j
                                         
                              '********main code for listing all the channels that the house id watch*******
                                        
                                         If fsys.FileExists("C:\rohit program\exercise\SWD\" & filename) = True Then
                                           MsgBox "file is open"
                                         Else
                                           MsgBox "file not found"
                                         End If
                                           'prevcarray = ""
                                           'prevchannelname = ""
                                           'prevhomeid = ""
                                          channelcount = 0
                                          'prevchannelarray(k) = ""
                                Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
                                 Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
                                           Do Until txtstream.AtEndOfStream
                                               searchline = txtstream.ReadLine
                                                 homeid = Mid(searchline, 1, 10)
                                                  channelname = Mid(searchline, 13, 5)
                                                   channelarray(i) = homeid & channelname
                            
                            [b]prev time u said that i have to use the for loop to check whether each homeid and channelname is there in the or not.[/b]
                            sir but i m not able to use the for loop to check in the array,i think
                                                    If channelarray(i) <> channelarray(i + 1) Then
                                                     outstream.WriteLine (channelarray(i))
                                                    End If
                                                     channelarray(i + 1) = channelarray(i)
                                                                     Loop
                                               MsgBox "process completed"
                                              
                            End Sub
                            sir pls help.
                            Last edited by Ronin; Mar 26 '07, 12:05 PM. Reason: mistake

                            Comment

                            • Killer42
                              Recognized Expert Expert
                              • Oct 2006
                              • 8429

                              #15
                              Well, you certainly do have some interesting logic there.

                              I don't have a lot of time right now, so I've banged together a modified version that I hope will help you with the process. I have not tested it, as I don't have any test data and can't spare the time to create some. Anyway, after making sure you've got a backup copy of your current code (in case mine makes things worse) just replace the contents of the cmdclick_Click routine with this, and see how it goes...
                              Code:
                                Dim j As Integer
                                Dim searchline As String
                                Dim filename As String
                                Dim homeid As String
                                Dim prevhomeid As String
                                Dim channelname As String
                                Dim channelcount As Integer
                                Dim HomeAndChannel As String  ' <-- Added this variable.
                                Dim Exists As Boolean         ' <-- And this one.
                                Dim ChannelsToReport As Long  ' <-- This one, too.
                                Dim channelarray(0 To 100000) As String
                                ' Dim newchannelarray(0 To 100000) As String
                                ' Dim carray As String
                                ' Dim prevcarray As String
                                'Dim i As Double
                                Dim i As Long
                              
                                ' prevcarray = ""
                                For j = 0 To lstMarket.ListCount - 1
                                  If lstMarket.Selected(j) = True Then
                                    If Len(Trim(txtYear.Text)) > 2 Or IsNumeric(Trim(txtYear.Text)) = False Then
                                      MsgBox "This should be 2 digit number "
                                      txtYear.Text = ""
                                      txtYear.SetFocus
                                      Exit Sub
                                    End If
                                
                                    If Trim(txtWeek) <= 9 And Len(Trim(txtWeek)) < 2 Or (IsNumeric(txtWeek.Text) = False) Then
                                      txtWeek = "0" & Trim(txtWeek)
                                    End If
                                           
                                    If Trim(txtDay) > 7 Or (IsNumeric(txtDay) = False) Then
                                      MsgBox "Please enter number which is equal to or less than 7"
                                      txtDay.Text = ""
                                      txtDay.SetFocus
                                      Exit Sub
                                    End If
                                
                                    filename = lstMarket.List(j) & Trim(txtYear) & Trim(txtWeek) & Trim(txtDay) & ".swd"
                                    MsgBox ("you have selected the following : " & filename)
                                  End If
                                Next j
                                             
                                '********main code for listing all the channels that the house id watch*******
                                            
                                If fsys.FileExists("C:\rohit program\exercise\SWD\" & filename) = True Then
                                  MsgBox "file is open"
                                Else
                                  MsgBox "file not found"
                                  Exit Sub
                                End If
                                'prevcarray = ""
                                'prevchannelname = ""
                                'prevhomeid = ""
                                channelcount = 0
                                'prevchannelarray(k) = ""
                                Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
                                Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
                                
                                
                                
                                ' ****************************************
                                ' Step 1: Build an array of all unique homeid/channel combinations
                                ' ****************************************
                                
                                Do Until txtstream.AtEndOfStream
                                  searchline = txtstream.ReadLine
                                  homeid = Mid(searchline, 1, 10)
                                  channelname = Mid(searchline, 13, 5)
                                  'channelarray(i) = homeid & channelname
                                  HomeAndChannel = homeid & channelname
                                
                                
                                  ' Check whether we have already seen this combination...
                                  Exists = False
                                  For i = 1 To channelcount
                                    If channelarray(i) = HomeAndChannel Then
                                      Exists = True
                                      Exit For
                                    End If
                                  Next
                                  
                                  ' If not, add it to the array.
                                  If Not Exists Then
                                    channelcount = channelcount + 1
                                    channelarray(channelcount) = HomeAndChannel
                                  End If
                                Loop
                                
                                
                                
                                ' ****************************************
                                ' Step 2: Sort the array. I'll leave this to you, as I'm short on time
                                ' ****************************************
                                
                                
                                
                                
                                
                                ' ****************************************
                                ' Step 3: Scan the array and report the number of channels per homeid
                                ' ****************************************
                                
                                ChannelsToReport = 1
                                prevhomeid = Left$(channelarray(1), 10)
                                For i = 2 To channelcount
                                  homeid = Left$(channelarray(i), 10)
                                  If homeid = prevhomeid Then
                                    ChannelsToReport = ChannelsToReport + 1
                                  Else
                                    outstream.WriteLine prevhomeid & " " & Format(ChannelsToReport)
                                    ChannelsToReport = 1
                                    prevhomeid = homeid
                                  End If
                                Next
                                MsgBox "process completed"

                              Comment

                              Working...