Results 1 to 3 of 3

Thread: How many Records in my File?

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Post

    You can use a collection instead of an array, and in doing so you don't have to worry about Rediming.
    Code:
        Dim sDummy As String
        Dim bFound As Boolean
        Dim strMyData As String
        Dim strPartNbr As String
        Dim MyCollection As New Collection
        
        ' Add code to read a line from your file
        ' into strMyData, and the part number into strPartNbr
        
        On Error Resume Next
        'If the text is in the collection, Err = 0, otherwise it's not
        sDummy = MyCollection.Item(strPartNbr)
        bFound = (Err = 0)
        If bFound Then
            Err.Clear
            MsgBox "Duplicate Part Number" & strPartNbr
        Else
            ' This adds the data with a Key value of strPartNbr
            MyCollection.Add strMyData, strPartNbr
        End If
    After you have all the unique records in the collection you can loop thru the collection as follows:
    Dim intRec as Integer

    For intRec = 1 to MyCollection.Count ' Collections start at 1
    MsgBox MyCollection.Item(intRec)
    Next

    ------------------
    Marty
    What did the fish say when it hit the concrete wall?
    > > > > > "Dam!"

    [This message has been edited by MartinLiss (edited 02-09-2000).]

  2. #2
    New Member
    Join Date
    Feb 2000
    Location
    London, UK
    Posts
    1

    Post

    Hi,

    I've just started coding in VB having been a mainframe programmer for 4yrs so I'm having a spot of bother.

    I need to validate Part Numbers in a File - they must all be unique.

    In order to test this I think I have to load all Part Numbers - 1st field in each record -into an array.

    Problem is the file will never have the same amount of records so I don't know how big my array should be. In order to specify array size I have to determine amount of records in file.

    If I do it the way I'm thinking I'll have to read the file 3 times!!!!
    Once to get no. of records - second to load Part Numbers into array- and third to continue validating the rest of the fields.

    Can anyone help - this is driving me up the wall!!!!

    Many Thanks in anticipation....

    Paulie...

  3. #3
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Post

    Hi Paulie.

    In VB you can create a Dynamic Array that can be easily adjusted for the number of records in your file.

    Put this code in the Click event of a command button:
    Code:
    dim myArray() as String
    dim iCounter as Integer
    
    open "c:yourfile.txt" for input as #1
    
    do until EOF(#1)
       Redim Preserve myArray(iCounter)
       Line Input #1, myArray(iCounter)
       iCounter = iCounter + 1
    loop
    
    Close #1
    Once to get no. of records - second to load Part Numbers into array- and third to continue validating the rest of the fields.
    This will take care of the first 2 (by the way the value of iCounter will be the Number of records in the file.). I'm sure with a little manipulation of the code above, you could "validate the rest of fields" also while reading through the file only once.

    All the best.

    ------------------
    OneSource
    The truth may be out there, but it's in here too!
    .


    [This message has been edited by OneSource (edited 02-09-2000).]

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