Results 1 to 4 of 4

Thread: get auto_increment field after addnew ?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 1999
    Posts
    309

    get auto_increment field after addnew ?

    If I add a record to a table (with addnew or whatever) and this table has an auto_incrementing field (as a unique identifier) how do I retrieve this identifier (without having to execute a new query) ?

  2. #2
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    If the db cursor is on the new record when you call the .Update function (I would think it would be, just look at .Fields("AutoID") or whatever you called it, right after the Update.
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 1999
    Posts
    309
    Nope this does not work. If I do this f.i.:

    objRS.Open "table",objConn,0,3,2
    objRS.AddNew
    objRS("Name") = strName
    objRS("Pass") = strPass
    Response.Write "ID = " & objRS("ID")
    objRS.Update
    Response.Write "ID = " & objRS("ID")

    I get:

    ID = ID =

  4. #4
    Addicted Member
    Join Date
    Oct 1999
    Location
    NY, USA.
    Posts
    240
    Here is an example of something that I recently used.

    PHP Code:


                                   
    'Save the data
                     Set tmpRS   = Server.CreateObject("ADODB.Recordset")
                     Set tmpConn = Server.CreateObject("ADODB.Connection")
                     tmpSQL      = "SELECT * FROM realestate_properties"
                     
                     tmpConn.ConnectionTimeout = DATABASE_CONNECTION_TIMEOUT
                     tmpConn.Open DATABASE_CONNECTION_STRING
                 
                     tmpRS.Open tmpSQL, tmpConn , adOpenKeyset, adLockOptimistic
                     
                     tmpRS.AddNew                 
                     tmpRS.Fields("realestate_company_id").Value               = gCompanyId
                     tmpRS.Fields("realestate_owner_firstname").Value          = PadSQL(get_form("ownerfirstname"))
                     tmpRS.Fields("realestate_owner_lastname").Value           = PadSQL(get_form("ownerlastname"))
                     tmpRS.Fields("realestate_owner_home_phone").Value         = PadSQL(get_form("ownerhomephone"))
                     tmpRS.Fields("realestate_owner_work_phone").Value         = PadSQL(get_form("ownerworkphone"))                 
                     tmpRS.Fields("realestate_owner_address").Value            = PadSQL(get_form("owneraddress"))
                     tmpRS.Fields("realestate_owner_city").Value               = PadSQL(get_form("ownercity"))
                     tmpRS.Fields("realestate_owner_state").Value              = PadSQL(get_form("ownerstate"))
                     tmpRS.Fields("realestate_owner_zipcode").Value            = PadSQL(get_form("ownerzipcode"))                 
                     tmpRS.Fields("realestate_property_address").Value         = PadSQL(get_form("propertyaddress"))
                     tmpRS.Fields("realestate_property_city").Value            = PadSQL(get_form("propertycity"))    
                     tmpRS.Fields("realestate_property_state").Value           = PadSQL(get_form("propertystate"))
                     tmpRS.Fields("realestate_property_zipcode").Value         = PadSQL(get_form("propertyzipcode"))                 
                    
     '
    Update
     tmpRS
    .Update
                     
    'get the new property id (auto-incremented field)
    get_property_id = tmpRS.Fields("realestate_id")
                                                          
    Set tmpRS   = Nothing
    Set tmpConn = Nothing 
    Omar
    [email protected]
    http://omar.caribwalk.com
    To God Be The Glory

    I see Tech People ...

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