Results 1 to 2 of 2

Thread: I need help on using these codes

  1. #1

    Thread Starter
    Registered User
    Join Date
    Mar 2013

    I need help on using these codes

    I dont really have an idea on where I should insert this code
        Dim NewID as Long  'this will contain the number
          objRS.Open "SELECT @@Identity", objConn, adOpenForwardOnly, adLockReadOnly, adCmdText
          NewID = objRS.Fields(0).Value  '0 specifies that we want the first field
          Set objRS = Nothing
    This is the original LINK of the thread where I found that code above. in titled Database - How can I get the AutoNumber/Identity value for the record just added?

    Im STUCKED, please help me guys!!!

  2. #2
    Fanatic Member
    Join Date
    Mar 2009

    Re: I need help on using these codes

    I dont know what syntax you use when adding records. I make a conmection and a command object then build the command string containing the sql INSERT statment and then i call command.executenonequery to insert the new record.

    Then, directly after that, in the same connection, i build another command string with "SELECT @@Identity". Then i call myID = command.ExecuteScalar

    Obviously a little more code to handle the eventuality that executescalar returns null if the previous insert failed.

    That SELECT @@Identity looks strange but its just an sql query that gets you the id of the autonumber field that was just added.
    Last edited by IanS; May 14th, 2013 at 04:55 AM.

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