Results 1 to 9 of 9

Thread: sorting (vb6)

  1. #1
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    sorting (vb6)

    I am making a simple database, I would like the access database to automatically be sorted after i input the new data, what command do I have to put in my button that will do this?

    Thanks

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: sorting (vb6)

    In Microsoft Access, you don't sort the raw data. Records in a table are always in the order they were added to the table.

    If you set a primary key on a table, the records will be displayed sorted by the primary key by default. You can also create a query that sorts the records the way you want, and use that to as record source for your form.

    If you want the records displayed in a form to be sorted immediately after you enter a new record, that can be very confusing to the user. You can requery the record source of the form, but this means that the record just added "jumps".

  3. #3
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: sorting (vb6)

    You don't sort databases, you sort queries or recordsets. Are you working in Access or in VB6?
    Charlotte

  4. #4
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: sorting (vb6)

    I am working in VB6 bit the datascroll i am using connects to a Access database. The thing is that once the program closes and is reopen the records are automatically sorted anyway, I want to mimic this in my code so that when the user enters the name of the new file it is automatically sorted so it is easier to find.

    I have tried to use this command but it does not work

    dtScroll.recordset.sort

    where dtscroll is the name of the datascroll member that is attached the the access database.

    thanks

  5. #5
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: sorting (vb6)

    I'm sorry but I have not idea what a datascroll is. Is that a custom control or are you using the term to describe something like the data repeater?
    Charlotte

  6. #6
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: sorting (vb6)

    sorry, my bad. It is called 'Data' if you were to mouseover it in the toolbox. It is next to the image control and ole control in my toolbox.

    Thanks

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: sorting (vb6)

    The Refresh method of the data control will re-read the records in the RecordSource. Make sure that the RecordSource has the desired sort order, for instance by using a query

    SELECT ... FROM ... ORDER BY ...

    and force it to re-read the records with

    dtScroll.Refresh

    in your code.

  8. #8
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: sorting (vb6)

    Sorry, I am still a beginner and donot know how to query all that well, could you please provide me with a small example.

    Thanks

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: sorting (vb6)

    Your Data control dtScroll has a RecordSource property. You can set this to one of the following:

    (1) The name of a table in your Access database
    (2) The name of a stored query in your Access database
    (3) A SQL string that gets data from your Access database.

    Probably, the RecordSource property is set to the name of a table at the moment, let's say tblSourceData. You want your date to be sorted by a field or a combination of fields, let's say by Company and within Company by LastName.

    The SQL string that returns the records of tblSourceData by Company and LastName is:

    <pre>SELECT * FROM tblSourceData ORDER BY Company,LastName;</pre>

    The easiest way to use this is to replace the table name in the RecordSource of the dtScroll control by this SQL string.

    (You can also create a query in the Access database with this SQL string, save it and set the RecordSource of the dtScroll control to the name of this query. If you have a lot of data, this is more efficient.)

    You probably have some code to add a new record. At the end of this code, you can add an instruction

    dtScroll.Refresh

    This forces the data control to re-read the records from the RecordSource. You can also put a command button on your form with caption "Refresh" and wih this instruction in its Click event handler.

    HTH,
    Hans

Posting Permissions

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