CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Arrays

  1. #1
    Join Date
    Jun 2001
    Posts
    4

    Arrays

    I need to read a file having about 10000 records and store the records in an array.....I need to search in this array for a specific record and if found move the record to another array and then populate MSFlexgrid from the 2nd array....
    Please help me...I am a beginner in VB


  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Arrays

    in stead of using an array, I would use a recordset. You can create a recordset using ADO without connecting to a real database. A recordset offers you much more flexibility than an array.
    Firts thing to do is to create the recordset in memory. You will need to add a refference to ADO (Microsoft ActiveX Data Objects) to the project. This can be done via the project>refference menu. Then the code to create the recordset

    dim rst as new ADODB.Recordset
    rst.Fields.Append "Field1",adVarChar,50
    rst.Fields.Append "Field2",adVarChar,50
    rst.Fields.Append "Field2",adVarChar,50
    rst.open



    Now we have an open recordset, we can start populating it with records. We need to open the text file, read it in line by line, and decide which goes where in the recordset. I'll make the assumption that the fields in that textfile are seperated with a comma.

    dim ffile as integer
    dim strLine as string
    dim ArrFields() as string
    ffile = freefile
    open "myfile.txt" for input as #ffile

    do until eof(ffile)
    ' read the next line
    Line input #Ffile, strLine
    ' split the array in the fiels, note that this can only be done in VB6, VB5 doesn't support the split function
    ArrFields = Split(strLine,",")
    ' add the record to the recordset
    rst.addnew
    rst("field1") = ArrFields(0)
    rst("field2") = ArrFields(1)
    rst("field3") = ArrFields(2)
    rst.update
    loop



    Now we have filled the recordset, we can do some searching on that.

    ' when someone presses a button called cmdSearch
    private sub cmdSearch_Click()
    rst.movefirst
    rst.find "Field1='" & txtCriteria.text & "'"
    if not(rst.eof) then
    ' we found a record
    text1.text = rst("field1")
    text2.text = rst("field2")
    text3.text = rst("field3")
    end if
    end sub



    I think this is a good, and fast solution for the problem. using arrays in this case is much slower, because when you need to search it, you will have to loop through the entire array, which is way slower than searching a recordset.

    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    Jun 2001
    Posts
    4

    Re: Arrays

    Thanks for the detailed reply...My problem is that I can't use ADO since I am working on VB 5.0. Is it possible to use ADO with VB 5.0?
    I am working iwth VB 5.0 professional edition.Please tell me how to do this using VB 5.0?


  4. #4
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Arrays

    You can use ADO if you install the Data Access SDk, which can be downloaded from the microsoft website. The latest version is 2.6, but 2.1 will do (i believe this comes with products like Office and stuff, not sure though). Once installed, you should be able to add refference to it in VB, even in VB5.

    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  5. #5
    Join Date
    Jun 2001
    Posts
    1

    Re: Arrays

    U can do one thing . First of all save the file in ur format .then while reading the file read till end with the do ... while eof(1)....... Now when ur in the reading the records for searching simultaneously save it to the array that u'd declared. Now If u get the record(s) then use another array to store them. Now as u r finsished with reading the record. Populate the Flesgrid now with the code as :
    Loop
    msflexgrid1.row=0
    msflexgrid1.col=0
    msflexgrid1.text=array(0) ' use the variables instead
    end loop

    If u need the complete code then I'll give it u by the tomorrow... or u can mail me at :
    [email protected] or [email protected]


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

Featured