Selecting last record ID in Access

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Selecting last record ID in Access

    Good day everyone.

    I'm having a problem selecting the ID (autonumbered primary key in table) from
    the last record inserted in MS Access. I've searched the forums looking for
    ways to do this and attempted the following:

    SELECT LAST_INSERT_ID() AS strOrderID
    This returns a "Undefined function" error as I believe this function is for
    MYSQL

    I've also found and tried:
    SELECT @@identity AS strOrderID

    This does not return an error but returns 0 as the ID. This tells me this is
    on the right track, but I'm missing something.

    Thanks in advance.....

    Rottwild_One Guest

  2. Similar Questions and Discussions

    1. direct access to db record via url
      Hello all, Just getting started with ColdFusion. I need to setup a visio document that passes the appropriate values to ColdFusion and shows the...
    2. Access Record Locking
      Hi my access database has created another file called access record locking and I was just wondering why it has done this? Thanks.
    3. INSERTing record into Access via CFC?
      I feel confident people are routinely doing this, but I have not been able to find a simple, concise tutorial on it... I have a Cold Fusion...
    4. Selecting one particular field in the same Access database table?
      Is there a way of selecting one particular field in the same Access database table? eg. database is - ID: 1 ...
    5. Record selecting
      Is there away to have a form use a combo box for say that when the part number is selected, that part number's detail (i.e. cost, unit measurement,...
  3. #2

    Default Re: Selecting last record ID in Access

    <cfquery ...>
    Insert blah blah blah
    </cfquery>

    <cfquery ...>
    select max(myIDCol) from myTable
    </cfquery>

    Trevor
    [url]http://www.burnette.us[/url]
    TSB Guest

  4. #3

    Default Re: Selecting last record ID in Access

    Trevor,

    Thanks for the quick response. This option does work but I've read several
    places it is possible to return the wrong ID if multiple users are adding
    records (as unlikely it would seem), which is why I did not try this approach.

    After reading my subject line again though, your post is EXACTLY what I asked
    for. I guess I should have asked how do I return the ID of a record being
    inserted?

    I can use this method for development at this point, but I'll need to figure
    out how to get the ID when inserting the record. Is there a SELECT NEXT or
    SELECT NEW that would allow me to return the ID?

    Thanks again Trevor.......

    Rottwild_One Guest

  5. #4

    Default Re: Selecting last record ID in Access

    There are a number of approaches for non-identity database engines. The one I
    lived with for a while (until we thankfully upgraded to SQL Server) was to add
    a GUID field to the table, stuff it with CF's CreateUUID() function, and then
    SELECT the ID number for the record with that GUID value.

    HTH,

    philh Guest

  6. #5

    Default Re: Selecting last record ID in Access

    Phil,

    Thanks for the response. I've thought about generating a UID and using that
    for the ID field itself instead of using Access autonumbering (I'd have to
    check the implications of doing such), or like you suggested, using it to
    retrieve the ID. That may be the only way to do it if we can't retrieve the ID
    during the insert.

    Thanks again everyone.

    Rottwild_One Guest

  7. #6

    Default Re: Selecting last record ID in Access

    I use both the max ID and compare the fields that make it unique otherwise (if
    there are any).
    For example, I might have a product table and I don't allow duplicate Product
    Titles. So I would do the max(ID) where product_title = '#Product_Title#'. The
    GUID is probably the most reliable way though if you don't have anything other
    than the ID that might make it unique.

    Donnettacb98 Guest

  8. #7

    Default Re: Selecting last record ID in Access

    Use TSB's example inside <cftransaction> </cftransaction>.
    lifeinkorea Guest

  9. #8

    Default Re: Selecting last record ID in Access

    cftransaction won't help. If you are limited to using select max(), make sure
    you make your where clause as extensive as possible.

    If you think you still might bring back the wrong record because of high
    activity, store something about who entered the record (the IP address for
    example) and include that in your where clause.

    Dan Bracuk 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