Problem sorting tables in visual basic

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RichardJB
    New Member
    • Nov 2006
    • 2

    Problem sorting tables in visual basic

    Hi,

    I am using visual basic in access 2003 and am having a problem with sorting tables which I can't seem to find a solution to, and was hoping that someone might be able to help.

    I have a table containing policy numbers and various bits of information about each policy. It is necessary to go through the table in policy order and do various calculations to each row.

    So, I have used an SQL query to create a copy of the table sorted by policy number as follows:

    Dim STR As String

    STR = " SELECT Policy.* INTO PolicyCopy FROM Policy ORDER BY PolicyNumber; "
    DoCmd.RunSQL (STR)


    I now open the table PolicyCopy as follows:

    Dim db As Database
    Dim PolicyRecordset As Recordset

    Set db = CurrentDb
    Set PolicyRecordset = db.OpenRecordse t("PolicyCopy ", dbOpenTable)


    Then I go through the PolicyRecordset by using Movefirst and Movenext's.

    However, sometimes the program will crash because the PolicyCopy table is not in the correct order. Sometimes the top few rows, usually about a couple of hundred, will be dumped into the middle of the table. I wondered if anyone knew how to get around this problem

    Many thanks
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Originally posted by RichardJB
    Hi,

    I am using visual basic in access 2003 and am having a problem with sorting tables which I can't seem to find a solution to, and was hoping that someone might be able to help.

    I have a table containing policy numbers and various bits of information about each policy. It is necessary to go through the table in policy order and do various calculations to each row.

    So, I have used an SQL query to create a copy of the table sorted by policy number as follows:

    Dim STR As String

    STR = " SELECT Policy.* INTO PolicyCopy FROM Policy ORDER BY PolicyNumber; "
    DoCmd.RunSQL (STR)


    I now open the table PolicyCopy as follows:

    Dim db As Database
    Dim PolicyRecordset As Recordset

    Set db = CurrentDb
    Set PolicyRecordset = db.OpenRecordse t("PolicyCopy ", dbOpenTable)


    Then I go through the PolicyRecordset by using Movefirst and Movenext's.

    However, sometimes the program will crash because the PolicyCopy table is not in the correct order. Sometimes the top few rows, usually about a couple of hundred, will be dumped into the middle of the table. I wondered if anyone knew how to get around this problem

    Many thanks
    Hi. You have the answer really. Just need to put the two statements together.

    Set PolicyRecordset = db.OpenRecordse t("SELECT * FROM Policy ORDER BY PolicyNumber")

    The table type recordset is default.

    And away you go :)

    Comment

    • RichardJB
      New Member
      • Nov 2006
      • 2

      #3
      Originally posted by willakawill
      Hi. You have the answer really. Just need to put the two statements together.

      Set PolicyRecordset = db.OpenRecordse t("SELECT * FROM Policy ORDER BY PolicyNumber")

      The table type recordset is default.

      And away you go :)

      Thats wonderful, thanks very much

      Comment

      • jpezhil
        New Member
        • Sep 2006
        • 9

        #4
        I Think into Statement Does Not Allow Order By Class
        So please remove Order by policyNumber statement

        STR = " SELECT * INTO PolicyCopy FROM Policy "

        Dim db As Database
        Dim PolicyRecordset As Recordset
        Set db = CurrentDb
        Set PolicyRecordset = db.OpenRecordse t("select * from policycopy order by policynumber")

        Thanking U
        R.Jayaprakash

        Comment

        Working...