Question : Get autonumber ( unique ID ) of newly inserted record, then insert value into a relating table, ms access 2003

Hi,
Im trying to grab the autonumber of a newly inserted record in ms access 2003. The code I found currently does this though there may be a more efficent way. What I've had trouble doing is storing this autonumber as a value, and inserting it in a relating table as the relationship between  the two. If the code for getting the autonumber is a little long winded please advise on a cleaner way.

Many Thanks
Dan
Code Snippet:
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:
<%
Dim rsIDValue, message
 
Sub getRecordSet
Dim conn, command, connection
 
command = "INSERT INTO tbl_customers (cst_FirstName, cst_Surname, cst_Address1, cst_Address2, cst_County, cst_Postcode, cst_Phone, cst_Email, cst_Agreement)  VALUES ('"&Request.form("textFirstName")&"','"&Request.form("textSurname")&"','"&Request.form("textAddress1")&"','"&Request.form("textAddress2")&"','"&Request.form("textCounty")&"','"&Request.form("textPostcode")&"','"&Request.form("textPhone")&"','"&Request.form("textEmail")&"','"&Request.form("textAgreement")&"')"
 
on error resume next
 
  ' Create an ActiveX Data Object for the database connection
  Set conn = Server.CreateObject("ADODB.Connection")
  if err then
    Response.Write "There was an error with adodb connection
" & vbLF '* developer only * Response.Write err.number & "
" & err.description & "
" & err.source Response.End else connection = "********" ' Try to open a connection to the database conn.open connection if err then Response.Write "There was an error opening the database connection string
" & vbLF '* developer only * Response.Write err.number & "
" & err.description & "
" & err.source Response.End else ' For MS SQL comment out next two lines conn.execute(command) command="SELECT @@IDENTITY;" Set rsIDValue = conn.execute(command) if err then Response.Write "There was an error opening the recordset to retrieve the requested data.
" & vbLF '* developer only * Response.Write err.number & "
" & err.description & "
" & err.source Response.End else ' we have our recordset or results, lets go. Exit Sub end if set rsIDValue = nothing conn.close end if Set conn = nothing end if on error goto 0 End Sub call getRecordSet() ' Was the subroutine successful? (Did we get a recordset object) if isObject(rsIDValue) then ' And do we have records? if NOT (rsIDValue.bof AND rsIDValue.eof) then if err then Response.Write "There is no record to write to child table.
" & vbLF '* developer only * Response.Write err.number & "
" & err.description & "
" & err.source Response.End else end if message = rsIDValue(0) Dim conn, command, connection command = "INSERT INTO tbl_customerforms ( (frmCustID is the field which requires the value), frm_FormID, frm_WebID, frm_Branch, frm_EnquiryDate, frm_CustComm) VALUES ('"&Request.form("textFormID")&"','"&Request.form("textWebID")&"','"&Request.form("textBranch")&"','"&Request.form("textEnquiryDate")&"','"&Request.form("textCustComm")&"')" on error resume next ' Create an ActiveX Data Object for the database connection Set conn = Server.CreateObject("ADODB.Connection") if err then Response.Write "There was an error with adodb connection
" & vbLF '* developer only * Response.Write err.number & "
" & err.description & "
" & err.source Response.End else connection = "******" ' Try to open a connection to the database conn.open connection if err then Response.Write "There was an error opening the database connection string
" & vbLF '* developer only * Response.Write err.number & "
" & err.description & "
" & err.source Response.End else ' For MS SQL comment out next two lines conn.execute(command) end if end if rs.close () Set rs = nothing end if end if %>

Answer : Get autonumber ( unique ID ) of newly inserted record, then insert value into a relating table, ms access 2003

I believe I see where you're going ... Assuming that you mean you must insert the value into tblCustomerForms.FrmCustID, then you'd change your Insert statement as such:

command = "INSERT INTO tbl_customerforms (FrmCustID, frm_FormID, frm_WebID, frm_Branch, frm_EnquiryDate, frm_CustComm)  VALUES (" & rstValueID.Fields(0) & ",'"&Request.form("textFormID")&"','"&Request.form("textWebID")&"','"&Request.form("textBranch")&"','"&Request.form("textEnquiryDate")&"','"&Request.form("textCustComm")&"')"

This assumes that the FrmCustID value is Numeric ... also, your other column names include an underscore character (i.e. "frm_SomeName") so make sure of your spelling when trying this.
Random Solutions  
 
programming4us programming4us