Add Form Information to a Table - forms

Option Compare Database
Private Sub cmdAdd_Click()
CurrentDb.Execute "INSERT INTO Overtime(Todays_Date, Employee_Name, " & _
"Start_Date, End_Date,Comments) " & _
" VALUES(" & Me.txtCurrentday & ",'" & Me.txtName & "','" & _
Me.txtBegin & "','" & Me.txtEnd & "','" & Me.txtComment & "')"
Me.Refreshenter
cmdClear_Click
End Sub
Private Sub cmdClear_Click()
Me.txtCurrentday = ""
Me.txtName = ""
Me.txtBegin = ""
Me.txtEnd = ""
Me.txtComment = ""
Me.txtCurrentday.SetFocus
End Sub
Private Sub cmdClose_Click()
DoCmd.Close
End Sub
Hello, I have created a Form and a Table in Microsoft Access 2010. The Form is called pbicovertime it has five unbound text boxes which all have unique names and three buttons. I would like the information that has been entered in the Form to be added to the Table called Overtime when the Add button is pressed. The code above does add the data from the Form to the table, however I get a Run-timer error '3061": Too few parameters. Expected 1 error message after closing and reopening the database. So initially everything seemed to be working fine. All the information entered in the Form was being added to the correct column in my Overtime Table. The issue took place after closing and reopening the database. I am not really sure how to proceed from this point.
FYI this is my first time working with Forms in Access !

Open your table as a recordset and add a row. That will avoid complications based on required/missing quotes or date delimiters in the values you're adding.
Option Compare Database
Option Explicit ' <- add this
Private Sub cmdAdd_Click()
Dim db As DAO.database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("Overtime", dbOpenTable, dbAppendOnly)
With rs
.AddNew
!Todays_Date = Me.txtCurrentday
!Employee_Name = Me.txtName
!Start_Date = Me.txtBegin
!End_Date = Me.txtEnd
!Comments = Me.txtComment
.Update
.Close
End With
'Me.Refreshenter ' <- what is this?
cmdClear_Click
End Sub
If the original missing parameter error was because of a misspelled field name, this code will throw an error on one of the lines between AddNew and Update, so you should be able to quickly identify which name is misspelled.
Note: Always include Option Explicit in the Declarations sections of your code modules. And then run Debug->Compile from the VB Editor's main menu. Correct anything the compiler complains about before you spend time troubleshooting the code.
I don't know what Me.Refreshenter is. It looks like a misspelling of Me.Refresh. If so, that is something Option Explicit will warn you about. However, if you wanted Refresh, I suggest you substitute Me.Requery. The reason is that Refresh will pull in changes to any of the existing rows in the form's recordset, but not newly added rows. Requery gets new rows in addition to changes to existing rows.

I'm willing to bet it's this line that it's crashing on.
CurrentDb.Execute "INSERT INTO Overtime(Todays_Date, Employee_Name, " & _
"Start_Date, End_Date,Comments) " & _
" VALUES(" & Me.txtCurrentday & ",'" & Me.txtName & "','" & _
Me.txtBegin & "','" & Me.txtEnd & "','" & Me.txtComment & "')"
Specifically the Me.txtCurrentday, because it will be evaluated as straight text, and depending on how your PC is setup, it may be confusing SQL. e.g., it might look like this:
INSERT INTO Overtime(Todays_Date, Employee_Name, Start_Date, End_Date,Comments)
VALUES ( Dec 1, 2013, 'JoeSmith', 'Jan 1, 2013', 'Dec 31, 2013',
'Some important comment');
Dates you should encompass in #'s:
INSERT INTO Overtime(Todays_Date, Employee_Name, Start_Date, End_Date,Comments)
VALUES ( #Dec 1, 2013#, 'JoeSmith', #Jan 1, 2013#, #Dec 31, 2013#,
'Some important comment');
and it will go smoother. Also building up the SQL that way leaves you vulnerable to injections (either as an attack or error). Imagine if the comment was "This is Susie's Job", in which case that extra apostrophe would mess up the insert.

Related

Access pass-through query with parameters not updating with prompted parameters

An Access pass-through query works when using the default parameters. When used in an Access report, the prompts that are used returns records based on the default parameters in the ptq and not the answered prompts. Default data is being returned.
I have a SQL Server based stored procedure that works, uspWorkCentreReport, that uses #TheDate DATE, #WC VARCHAR(15), #Shift INT for parameters and returns, through a SELECT statement, these columns:
[JOB NUMBER], [REL #], [JOB NAME], QTY.
Here's the ALTER line of the stored procedure code:
ALTER PROCEDURE [dbo].[uspWorkCentreReport]
#TheDate DATE,
#WC VARCHAR(15),
#Shift INT
The Access pass-through query, ptq_uspWorkCentreReport, passes these default parameters '2019-05-30','PCOT',1 and uses a DSN-less ODBC connection that works to return default data. I forgot to try but I think it will return correct data with whatever default parameters I use to replace '2019-05-30','PCOT',1. EDIT - I tried it this morning and indeed any appropriate replacement parameters return the appropriate associated records. Here's the ptq's one line:
exec uspWorkCentreReport '2019-05-30','PCOT',1
I provide the ptq with default parameters based on Albert D. Kallal's SO reply.
I use an Access select query, qry_ptq_uspWorkCentreReport, to receive [JOB NUMBER],[REL #],[JOB NAME],QTY and pass the parameters TheDate, set to Date With Time, WC, set to Short Text, and Shift, set to Integer.
qry_ptq_uspWorkCentreReport uses the pass-through query. The parameters are set using Access' Parameters applet and not within the query fields. Running this select query prompts for the 3 parameters but only returns data based on the default parameters set in the ptq's one line. I did not think to look at the Access SQL statement but will do so when I get to work tomorrow morning. EDIT - Here's the SQL statement for qry_ptq_uspWorkCentreReport:
PARAMETERS TheDate DateTime, WC Text ( 255 ), Shift Short;
SELECT ptq_uspWorkCentreReport.[JOB NUMBER], ptq_uspWorkCentreReport.[REL #], ptq_uspWorkCentreReport.[JOB NAME], ptq_uspWorkCentreReport.QTY
FROM ptq_uspWorkCentreReport;
Of course the above three functions culminate in an Access report, rpt_qry_ptq_WorkCentreReport to make the records human readable.
I have used the same scenario for another report the takes From and To dates as parameters. When that report runs, the prompts take the dates and return records based on those dates and not the dates in the ptq. Here's that ptq:
exec uspMergeAandPJobs '2018-01-01','2019-01-01'
Indeed, I tried using
exec uspMergeAandPJobs '',''
And the report returns 0 records!
Not sure what I am missing and would appreciate any feedback. TIA.
I tried the following with the help of a tutor:
Sub Report_Load()
Dim strFromDate As String
Dim strToDate As String
Dim strWC As String
Dim intShift As Integer
Dim strSQL As String
strFromDate = InputBox("From Date and Time: ")
strToDate = InputBox("Enter To Date and Time: ")
strWC = InputBox("Enter Work Center: ")
intShift = InputBox("Enter Shift: ")
Dim qdf As DAO.QueryDef, rst As DAO.Recordset
Set qdf = CurrentDb.CreateQueryDef("")
qdf.SQL = "exec dbo.uspWorkCentreReport " & "'" & strFromDate & "', " & "'" & strToDate & "', " & "'" & strWC & "', " & intShift & ";"
qdf.Connect = "ODBC;DRIVER=ODBC Driver 13 for SQL Server;SERVER=OURS\NTSQL;Trusted_Connection=Yes;DATABASE=TablesCoE;ApplicationIntent=READONLY;"
qdf.ReturnsRecords = True
Set rst = qdf.OpenRecordset
rst.Close
Set rst = Nothing
Set qdf = Nothing
End Sub
After the prompts VBA spits up a Run-Time error 3129 - Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'. Neither of us were able to determine what was causing the error. In VBA the "qdf.SQL..." line is highlighted in yellow.
EDIT - Adding stored proc's SQL code:
ALTER PROCEDURE [dbo].[uspWorkCentreReport_TEST] #FromDate DATETIME,#ToDate DATETIME,#WC VARCHAR(15),#Shift INT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Build table variable SumTable structure
DECLARE #SumTable TABLE(matl_nbr VARCHAR(60),QTY DECIMAL(4,0),matl_dsc VARCHAR(50))
-- P jobs and their summed WorkCentre traversals using crosstab - each traversal is added up
INSERT INTO #SumTable(matl_nbr,matl_dsc,QTY)
SELECT SRC1.matl_nbr,SRC1.matl_dsc,
SUM(CASE WHEN SRC1.locn_to = #WC THEN 1 ELSE 0 END) AS QTY
FROM
(
SELECT matl_nbr,matl_dsc,locn_to
FROM mtrk_CompanyE.dbo.trxn_hstd th
WHERE (last_upd >= #FromDate AND last_upd <= #ToDate) AND
locn_to = #WC
)SRC1
GROUP BY matl_nbr,matl_dsc
-- These updates take all the summed WorkCentre (locn_to) columns and turn each into "1" for later summing
UPDATE #SumTable
SET QTY = 1
WHERE QTY >1
-- Shortening the material number from 123456_00_00_R1_00 to 1234560
UPDATE #SumTable
SET matl_nbr = LEFT(matl_nbr,6) + right(LEFT(matl_nbr,9),1)
SELECT LEFT(A.matl_nbr,6)[JOB NUMBER],SUBSTRING(A.matl_nbr,7,1)[REL #],matl_dsc AS [JOB NAME],QTY
FROM (SELECT matl_nbr,matl_dsc,
SUM(CASE WHEN QTY = 1 THEN 1 ELSE NULL END) AS QTY
FROM #SumTable
GROUP BY matl_nbr,matl_dsc)A
ORDER BY QTY DESC;
END
EDIT - Finished sub:
Private Sub Report_Open(Cancel As Integer)
Dim strFromDate As String
Dim strToDate As String
Dim strWC As String
Dim intShift As Integer
Dim strSQL As String
strFromDate = InputBox("Enter From Date and Time: ")
strToDate = InputBox("Enter To Date and Time: ")
strWC = InputBox("Enter Work Center: ")
intShift = InputBox("Enter Shift: ")
strSQL = "exec dbo.uspWorkCentreReport_TEST " & "'" & strFromDate & "', " & "'" & strToDate & "', " & "'" & strWC & "', " & intShift & ";"
CurrentDb.QueryDefs("ptq_uspWorkCentreReport").SQL = strSQL
DoCmd.OpenReport "rpt_qry_ptq_uspWorkCentreReport", acViewReport
Me.lblFromDate.Caption = strFromDate
Me.lblToDate.Caption = strToDate
Me.lblWC.Caption = strWC
Me.lblShift.Caption = intShift
End Sub
Your Access query has parameters:
PARAMETERS TheDate DateTime, WC Text ( 255 ), Shift Short;
and since they are defined in the query definition, Access asks for them when opening/running the query.
But these parameters are never used!
There is no way for Access to pass these parameters into the pass-through query that is the basis of the Access query. Again, a PT query is nothing more than a Connect string and a constant SQL string.
So when you run the Access query, it will always run the saved contents of the PT query, i.e.
exec uspWorkCentreReport '2019-05-30','PCOT',1
The parameters you entered are ignored.
What you need to do (as outlined in the answer you refer to):
create a form to collect the parameter values
dynamically create the SQL string for the PT query with VBA
assign that SQL to the PT query:
CurrentDb.QueryDefs("ptq_uspWorkCentreReport").SQL = strSql
(it is automatically saved)
and then you can run the report based on the Access query - or better: directly use the PT query as record source for the report.
Remove the parameters from the Access query, they are of no use for your situation. Or remove the query entirely, unless you need it to join the PT query with something else.
Edit for above edit:
If you get a runtime error, there is probably a syntax error in your .Sql. Build the SQL string in a variable, do Debug.Print strSql, and run that string in SSMS. You may need to change date formatting (depending on your locale settings).
Also: See my 3rd bullet. Defining a temporary querydef and opening a recordset doesn't work for a report. You must assign the .Sql of the existing query that is the record source of the report.
Addendum: if you need to create a new query, first set .Connect, and then .Sql, so Access knows it's a Pass-Through query.
Access SQL doesn't know exec.
Edit 2
You have an existing, working PT query ptq_uspWorkCentreReport, which returns records for one set of parameters, e.g.
exec uspWorkCentreReport '2019-05-30','PCOT',1
Use this query as record source for your report.
To run the report with different parameters, you must modify the query's SQL. You can do this manually in query design view, or with VBA.
I think Report_Load() is too late for modifying its record source (the PT query). Run the following sub, then open the Report.
Sub SetUspParameters()
Dim strFromDate As String
Dim strToDate As String
Dim strWC As String
Dim intShift As Integer
Dim strSQL As String
strFromDate = InputBox("From Date and Time: ")
strToDate = InputBox("Enter To Date and Time: ")
strWC = InputBox("Enter Work Center: ")
intShift = InputBox("Enter Shift: ")
strSQL = "exec dbo.uspWorkCentreReport " & "'" & strFromDate & "', " & "'" & strToDate & "', " & "'" & strWC & "', " & intShift & ";"
Debug.Print strSQL
' This line is all that's needed to modify the PT query
CurrentDb.QueryDefs("ptq_uspWorkCentreReport").SQL = strSQL
End Sub
In practice, you don't want to use 4 x InputBox, but a form.

Me.Filter Access Code Syntax

I am trying to filter a form that emails only the current record. I have tried to do the me.filter command but cannot seem to get the syntax correct. I have provided the code below. The Current Date field is a date field and the Discover, Tail, and FleetID fields are text fields. I was told to put in the me.filter code the primary keys of the table that the form is linked to so the pdf that is produced does not print all the records linked to the form. Please let me know if you see something with my code. Thanks in advance :)
On Error GoTo errhandle
Me.Filter = "CurrentDate= #" & Me!CurrentDate & "#" And "Discover= '" & Me!Discover & "'" And "Tail= '" & Me!Tail & "'" And "FleetID= '" & Me!FleetID & "'"
Me.FilterOn = True
DoCmd.SendObject acSendForm, "frmETIC", acFormatPDF, "email address", "", "", "Recovery Report", "Attached is the submitted Recovery Report"
exitErr:
Exit Sub
errhandle:
If Err.Number <> 2501 Then
MsgBox ("Email cancelled!")
End If
Resume exitEr
All those And-operators are meant to be for the filtering, so they need to be inside the filter-string. Otherwise they are used as boolean operators in VBA, which will cause a type mismatch error when used on strings.
Another issues with your code is the date in the filter string. You will not get the desired result unless the date is formatted properly for use in a SQL criteria.
Replace the line Me.Filter = ... with the following to fix both problems.
Me.Filter = "CurrentDate= #" & Format(Me!CurrentDate, "yyyy\-mm\-dd") & "# AND Discover= '" & Me!Discover & "' AND Tail= '" & Me!Tail & "' AND FleetID= '" & Me!FleetID & "'"
If this filter string does not return the expected results, put a Debug.Print Me.Filter on the next line. This will print the actual filter string into the Immediate Window and allow you to see if it contains the expected values. For further debugging create a new Query in Access, switch to SQL View and enter SELECT * FROM yourTable WHERE filterStringOutput as SQL. Run the query. If it does not return the expected records, remove the criteria one by one to find the one that is causing problems.

MS Chart Control showing blank when rowsource contains a WHERE Clause

First time poster and new to MS ACCESS. I am using MS ACCESS 2002-2003 database format.
I have a Mainform in which I have a tabbed control. In one of the tabs, I have a subform where I put another tabbed control. In one of those tabs, I have a combobox that has a list of categories and I am trying to pass that value to a MS Chart control using VBA. The chart will update (it will show all categories) as long as there is no WHERE clause . If I remove the variable from the WHERE clause and type a VALUE in, it still does not work. When I run Debug.Print strSQLPIE. The strSQLPIE value shows the SQL statement and the WHERE clause correctly. However, Chart keeps showing up as blank. If i copy that SQL statement and put it in a new query, the query runs fine.
I have netted the issue down to the WHERE clause of the SQL statement in the ROWSOURCE property of the chart control. What am i missing?
Here is the code:
Dim DB As Database
Dim rst As Recordset
Dim strSQLSB As String ' subform datasheet sql
Dim strSQLPIE As String ' chart control sql
Dim SelCatStr As String ' combobox for categories
Dim SelYrStr As String ' not being used for now
Dim SelYrMtStr As String ' not being used for now
SelCatStr = Me.PIEChart.Value ' combobox selected value
'SelYrStr = Me.SelectYear.Value ' not being used for now
'SelYrMtStr = Me.SelectYearMonth.Value ' not being used for now
strSQLSB = "SELECT UNIONMASTER.ByMonth, UNIONMASTER.Category, UNIONMASTER.Category1, UNIONMASTER.AdjustedAmount " & _
"FROM UNIONMASTER " & _
"WHERE UNIONMASTER.Category1= '" & SelCatStr & "';" THIS UPDATES THE DATASHEET IN ANOTHER SUBFORM IN THE SAME TAB CORRECTLY ON CHANGE OF COMBOBOX.
strSQLPIE = "SELECT UNIONMASTER.Category1, Sum(UNIONMASTER.AdjustedAmount) AS TotalSpent " & _
"FROM UNIONMASTER " & _
"WHERE UNIONMASTER.Category1= '" & Me.PIEChart.Value & "';"
I TRIED USING SelCatStr and Me.PIEChart.Value in the WHERE CLAUSE and it still down not work. I even tried typing in a value and it still does not work. The debug window shows the SQL correctly and the where clause correctly, but the chart shows up as blank as long as the WHERE clause is there.
Me.PIESubform.Form.RecordSource = strSQLSB
Me.[PIESubform].Form.Requery
Debug.Print strSQLPIE
Me.[CatChart].RowSource = strSQLPIE
Me.[CatChart].Requery
I think the problem has to do with the WHERE clause in the rowsource setting of the CHART. As i said, when I remove the where clause, the chart shows correctly for all categories, the moment i stick the WHERE clause, with a VALUE or with a variable, it shows blank.
The chart works if i use this:
strSQLPIE = "SELECT UNIONMASTER.Category1, Sum(UNIONMASTER.AdjustedAmount) AS TotalSpent " & _
"FROM UNIONMASTER " & _
"GROUP BY UNIONMASTER.Category1 ;"
Me.CatChart.RowSource = strSQLPIE
Me.CatChart.Requery
Any ideas?
You may missing 'GROUP BY' clause in your query. Your query should be like this.
strSQLPIE = "SELECT UNIONMASTER.Category1, " &_
"Sum(UNIONMASTER.AdjustedAmount) AS TotalSpent " & _
"FROM UNIONMASTER " & _
"WHERE UNIONMASTER.Category1= '" & Me.PIEChart.Value & "';" &_
"GROUP BY UNIONMASTER.Category1 ;"

Insert Into Access Table Form

I have a table I created and its purpose is to house a database of queries i have created over the years, I created a corresponding form to insert all the information but I am having trouble getting the code to work.
Private Sub cmd_go_Click()
Dim insertstring As String
insertstring = "INSERT INTO KWTable (KW, Source, Code) VALUES (" & text_key.Value & "," & combo_source.Value & "," & text_code.Text & ");"
DoCmd.RunSQL insertstring
End Sub
The three columns on the destination table are KW, Source, and Code the values being inserting into them are text_key (which are keywords that I type in so i can search them later when i need to reference certain things), combo_source.Value (which is a combo box with the list of databases where these codes and queries are saved, which i will select the right one when inserting into the table) and text_code ( which is the actual code itself of the query)
The code is supposed to insert the keywords (text) the source (combobox listing) and the code (text) into the KWTable. But when I click the add record button I get a "Runtime Error 424: Object Required" Error box and it has the whole insertstring line highlighted. I cannot troubleshoot where the error is. Any thoughts?
Option 1
As Remou said, you have no quotes around your text. I've added some single quotes around each of your fields and added some line breaks for easier reading. Does that work?
Private Sub cmd_go_Click()
Dim insertstring As String
insertstring = "INSERT INTO KWTable (KW, Source, Code) VALUES ('" & _
text_key.Value & "','" & _
combo_source.Value & "','" & _
text_code & "');"
DoCmd.RunSQL insertstring
End Sub
Option 2
I think this will handle storing single and double quotes in your table:
Private Sub cmd_go_Click()
Dim rst As recordset
Set rst = CurrentDb.OpenRecordset("KWTable ")
With rst
.addnew
.fields("KW")=text_key.Value
.fields("Source")=combo_source.Value
.fields("Code")=text_code
.update
End with
End sub

Getting ID of new record after insert

I'm just getting my head around insert statements today after getting sick of cheating with Dreamweaver's methods to do this for so long now (please don't laugh).
One thing I'm trying to figure out is how to get the ID value of a newly inserted record so I can redirect the user to that page if successful.
I have seen some examples which talk about stored procedures, but they're double dutch for me at the moment and I'm yet to learn about these, let alone how to use these from within my web pages.
Summary
How do I, using my code below retrieve the record ID for what the user has just inserted.
Workflow
Using a HTML form presented on an ASP page (add.asp), a user will submit new information which is inserted into a table of a database (treebay_transaction).
On pressing submit, the form data is passed to another page (add_sql.asp) which takes the submitted data along with additional information, and inserts it into the required table.
If the insert is successful, the id value of the new record (stored in the column treebay_transaction_id) needs to be extracted to use as part of a querystring before the user is redirected to a page showing the newly inserted record (view.asp?id=value).
Sample code - add_sql.asp
<!--#include virtual="/Connections/IntranetDB.asp" -->
...
<html>
<body>
<%
set conn = Server.CreateObject("ADODB.Connection")
conn.ConnectionString = MM_IntranetDB_STRING
conn.Open ConnectionString
...
sql="INSERT INTO treebay_transaction (treebay_transaction_seller,treebay_transaction_start_date,treebay_transaction_expiry_date,treebay_transaction_title,treebay_transaction_transaction_type,treebay_transaction_category,treebay_transaction_description,treebay_transaction_status)"
sql=sql & " VALUES "
sql=sql & "('" & CurrentUser & "',"
sql=sql & "'" & timestampCurrent & "',"
sql=sql & "'" & timestampExpiry & "',"
sql=sql & "'" & Request.Form("treebay_transaction_title") & "',"
sql=sql & "'" & Request.Form("treebay_transaction_transaction_type") & "',"
sql=sql & "'" & Request.Form("treebay_transaction_category") & "',"
sql=sql & "'" & Request.Form("xhtml1") & "',"
sql=sql & "'3')"
on error resume next
conn.Execute sql,recaffected
if err<>0 then
%>
<h1>Error!</h1>
<p>
...error text and diagnostics here...
</p>
<%
else
' this is where I should be figuring out what the new record ID is
recordID = ??
' the X below represents where that value should be going
Response.Redirect("index.asp?view.asp?id='" & recordID & "'")
end if
conn.close
%>
</body>
</html>
Run this after your execute statement and before you close your connection:
lsSQL = "SELECT ##IDENTITY AS NewID"
Set loRs = loConn.Execute(lsSQL)
llID = loRs.Fields("NewID").value
I pulled it from here:
http://www.kamath.com/tutorials/tut007_identity.asp
Build your sql variable as you have been. Let's make one trip to the DB instead of two. We'll use SCOPE_IDENTITY() right in the same statement(s) as the INSERT to avoid many trips to the database.
Add this when building your SQL statement:
sql=sql & "; SELECT SCOPE_IDENTITY() As NewTreebayTransactionID"
'now execute the insert and receive the ID in one Execute statement.
set newTransactionResults = conn.Execute(sql)
'here is our new ID.
recordID = newTransactionResults("NewTreebayTransactionID")
As soon as that's done:
sanitize your data inputs from your user
use parameterized statements
Set objConn = CreateObject("ADODB.Connection")
set rs = Server.CreateObject("ADODB.Recordset")
objConn.Open "DSN=connectionName"
rs.CursorLocation = 3
rs.Open "treebay_transaction", objConn, 3, 3
rs.AddNew fieldlist,values 'see link bellow to see how to fill this
rs.Update
bookmark = rs.absolutePosition ' First, store the location of you cursor
rs.Requery ' Next, update your recordset with the data from the database
rs.absolutePosition = bookmark ' Finally, change your cursor back
recordID = rs("ID")
rs.AddNew documentation: http://www.w3schools.com/ado/met_rs_addnew.asp
It depends on the database you are using, in SQL Server you can get the ##IDENTITY or SCOPE_IDENTITY() see: http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/
But one thing I want to warn you, the code above has SEVERE security vulnerabilities, namely SQL Injection attack, please stay away from concatenating strings that are coming from users, you should use command paramaters.
look to ##IDENTITY, SCOPE_IDENTITY or IDENT_CURRENT
This makes the assumption that your ID field is an IDENTITY INSERT field. Also consider ramifications of the various options listed, as each one acts and performs slightly differently.
http://sqlserverpedia.com/wiki/Functions_-_##IDENTITY,_SCOPE_IDENTITY,_IDENT_CURRENT
With the same transaction :
Const adUseServer = 2
Const adOpenKeyset = 1
Const adLockOptimistic = 3
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open "DSN=connectionName"
Set oRS = Server.CreateObject("ADODB.RecordSet")
oRS.CursorLocation = aduseserver
oRS.CursorType = adopenkeyset
oRS.LockType = adlockoptimistic
oRS.Open "treebay_transaction", oConn
if oRS.eof then
oRS.AddNew
oRS("treebay_transaction_seller") = CurrentUser
...
oRS.Update
recordID = oRS("treebay_transaction_id")
end if
oRS.Close
set oRS = nothing
oConn.Close
Set oConn = Nothing