change/swap order of two records in access

Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default change/swap order of two records in access

    How can I efficiently swap order of two records sorted by an autonumber.

    Access db. Primary key is an autonumber. I'm given a record ID corresponding
    with the primary key.
    I need to swap all record content of that record ID with either the previous
    record or next record.

    My first thoughts (of a novice mind you) are to select the two records in
    one query and temporary hold
    the content in variables or an array, and then update each record with said
    variables/array using some for loop.

    Can I select two or more records within a number range in one query?
    Can I select, temporarily store, and loop update in one open
    connection/execute? (I may be confusing terms in this question, I don't mean
    the initial connection to db)

    Any suggestion how to tackle this?

    Thanks,
    Rey




    rey Guest

  2. Similar Questions and Discussions

    1. swap the reading order in a PDF
      hi, is it possible to change the reading order in a PDF. 1. i want to change reading order 1 to 2 and 2 to 1. 2. so when i will save it as XML...
    2. Where can I change/swap an image file in director
      I need to make some updates on a director project and I am very novice with the director. I have defined Photoshop as the external editors for jpg...
    3. Updating records in order (into an order)
      I'm storing questions for a user-defined quiz. I can store them in the order they are entered without any problem. But... The user needs to have...
    4. Records in Ascending Order
      I am using Dreamwaver MX 6.1 with PHP and a MySQL database. I have a page that displays upcoming events and they are arranged by date in ascending...
    5. swap values between records
      CREATE TABLE Sometable (keycol CHAR(1) PRIMARY KEY, colx INTEGER NOT NULL) INSERT INTO Sometable VALUES ('a',1) INSERT INTO Sometable VALUES...
  3. #2

    Default Re: change/swap order of two records in access

    Note, primary key may not be consecutive due to deletions.

    Thanks,
    Rey


    rey Guest

  4. #3

    Default Re: change/swap order of two records in access

    While I'm curious as to why you need to do this, I guess it's not my
    concern. Here's one way, that is pretty much what you were saying.

    YourConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
    Source=c:\path\file.mdb;"
    Dim oADO, oRS1, oRS2
    Dim iRec1, iRec2
    iRec1 = 1
    iRec2 = 2
    Set oADO = Server.CreateObject("ADODB.Connection")
    oADO.Open YourConnectionString
    Set rs = oADO.Execute("SELECT [ID],[SomeData] FROM [SomeTable] WHERE [ID]
    IN (" & iRec1 & "," & iRec2 & ") ORDER BY [ID]")
    If Not rs.EOF Then
    aVals = rs.GetRows()
    rs.Close : Set rs = Nothing
    oADO.Execute "UPDATE [SomeTable] SET [SomeData]='" & aVals(1,0) & "' WHERE
    [ID]=" & aVals(0,1)
    oADO.Execute "UPDATE [SomeTable] SET [SomeData]='" & aVals(1,1) & "' WHERE
    [ID]=" & aVals(0,0)
    End If
    oADO.Close
    Set oADO = Nothing


    If you use a method such as this, you should read this.
    [url]http://www.asp101.com/articles/chris/asptransactions/default.asp[/url]

    Ray at home



    "rey" <dojocre8@yahoo.com> wrote in message
    news:cf6dnUm7Jbpn5TCiRVn-jw@comcast.com...
    > How can I efficiently swap order of two records sorted by an autonumber.
    >
    > Access db. Primary key is an autonumber. I'm given a record ID
    corresponding
    > with the primary key.
    > I need to swap all record content of that record ID with either the
    previous
    > record or next record.
    >
    > My first thoughts (of a novice mind you) are to select the two records in
    > one query and temporary hold
    > the content in variables or an array, and then update each record with
    said
    > variables/array using some for loop.
    >
    > Can I select two or more records within a number range in one query?
    > Can I select, temporarily store, and loop update in one open
    > connection/execute? (I may be confusing terms in this question, I don't
    mean
    > the initial connection to db)
    >
    > Any suggestion how to tackle this?
    >
    > Thanks,
    > Rey
    >
    >
    >
    >

    Ray at Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139