Auto Identity Record from Table

Hi All,

I have a function that creates a new customer record however, I need it to return the customer ID.

I have attached the current code which I thought was okay however, I am getting an error on line 27. The identity field is the customer ID and is an auto number field.

Can anyone help me work this through?


Public Function createCustomer(uTxtNewCompanyName As String, uTxtLastName As String, uTxtFirstName As String, uTxtEmailAddress As String, _
uTxtJobTitle As String, uTxtBusinessPhone As Long, uTxtMobilePhone As Long, uTxtFaxNumber As Long, uTxtAddress As String, _
uTxtCity As String, uTxtStateProvince As String, uTxtZIPPostalCode As Long, uTxtCountryRegion As String, uTxtWebPage As String) As Long
    'Creates a new customer in system
    'RETURNS the new customer id
    Dim rs As New ADODB.Recordset
    Dim Conn As ADODB.Connection
    Set Conn = CurrentProject.AccessConnection
    Dim strSQL As String
    Dim vCreationDTS As Date
    Dim vLastUpdateDTS As Date
    vCreationDTS = Now
    vLastUpdateDTS = Now

    strSQL = "INSERT INTO TBL_CUSTOMER (Company, LastName, FirstName, EmailAddress, JobTitle, BusinessPhone, MobilePhone, " & _
    "FaxNumber, Address, City, StateProvince, PostCode, CountryRegion, WebPage, CreationDTS) " & _
    "SELECT '" & uTxtNewCompanyName & "' AS Company, '" & uTxtLastName & "' AS LastName, '" & uTxtFirstName & "'" & _
    " AS FirstName, '" & uTxtEmailAddress & "' AS EmailAddress, '" & uTxtJobTitle & "' AS JobTitle, " & uTxtBusinessPhone & _
    " AS BusinessPhone, " & uTxtMobilePhone & " AS MobilePhone, " & uTxtFaxNumber & " " & _
    " AS FaxNumber, '" & uTxtAddress & "' AS Address, '" & uTxtCity & "' AS City, '" & uTxtStateProvince & "' AS StateProvince, " & uTxtZIPPostalCode & "" & _
    " AS PostCode, '" & uTxtCountryRegion & "' AS CountryRegion, '" & uTxtWebPage & "' AS WebPage, '" & vCreationDTS & "' AS CreationDTS"
    rs.Open strSQL, Conn, adOpenKeyset, adLockOptimistic
    Set rs = Nothing

    rs.Open strSQL, Conn, adOpenKeyset, adLockOptimistic

    createCustomer = rs(0)
    Set rs = Nothing

End Function

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try the SELECT without the TableName.

I believe that returns the last inserted value on your current connection:


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Also, have you verified that the record is actually getting inserted as expected?

I generally use conn.Execute for executable queries (as opposed to  Not saying it's wrong as I haven't tried it... just not a syntax I'm familiar with.
creativefusionAuthor Commented:
Hi mbizup

Works perfect, thanks muchly.

So you say that the rs.Open strSQL, Conn, adOpenKeyset, adLockOptimistic should be changed to conn.Execute strSQL, Conn, adOpenKeyset, adLockOptimistic?

That is how I would have written it.


<Works perfect>

That is ultimately what we're after.  Your syntax is different from mine.  You say tomato; I say tomahto... but apparently it all works :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.