Getting ID of new record after insert - tsql

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

Related

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 ;"

Add Form Information to a Table

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.

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

Access 2010 - Cannot edit textbox in unbound form which is populated by recordset

I am using Access 2010 with linked tables from SQL Server 2008. I have a form that I am populating using a recordset and the control source of the textbox in this form is set to a field from the recordset. I find that although I can navigate through all the 16 records on the form, and the form loads correctly, I am unable to edit the Notes text box. It needs to be editable. The textbox has Enabled=True and Locked=False. The AllowEdits property of the form is set to true. All the tables in the query have primary keys. So, is it my query then - since it has right and inner joins in it? So the issue is that I cannot type in the textbox.
Just a little background, I tried using a query as the recordsource for this form, but found that Access' AutoSave feature inserted incomplete records into my Result table, in addition to the updates and inserts done by my Save button event. If the only way to circumvent this is to ask the user whether he/she would like to save changes every time he navigates, then that would be way too frustrating for the end user. So, I have had to use an unbound form where I use VBA to populate it using a ADO recordset.
Incidentally, I can edit the DocID and DocumentType columns, it's the fields from the query that cannot be changed (QCNote)
Here is the code from my Form_Open event. I also have a Form_Current event that disables the Submit button for inapplicable categories.
Private Sub Form_Open(Cancel As Integer)
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cn = CurrentProject.AccessConnection
Set rs = New ADODB.Recordset
With rs
Set .ActiveConnection = cn
DocID = [Forms]![QCDocAttributes]![DocID]
DocumentType = [Forms]![QCDocAttributes]![Document Type]
strSQL = "SELECT " & DocID & " AS DocID,'" & DocumentType & "' AS DocumentType, QC_QCDecisionPoint.Description, QC_QCDecisionPoint.QCDecisionPointID , QC_QCResultDecisionPoint.QCNote FROM QC_QCResultDecisionPoint RIGHT JOIN ((QC_QCAttribute INNER JOIN QC_QCAttributeDecisionPointAsc ON QC_QCAttribute.QCAttributeID = QC_QCAttributeDecisionPointAsc.QCAttributeID) INNER JOIN QC_QCDecisionPoint ON QC_QCAttributeDecisionPointAsc.QCDecisionPointID = QC_QCDecisionPoint.QCDecisionPointID) ON QC_QCResultDecisionPoint.QCDecisionPointID = QC_QCDecisionPoint.QCDecisionPointID WHERE (((QC_QCAttribute.Description)= '" & [Forms]![QCDocAttributes]![AttributesDropdown] & "' ));"
.Source = strSQL
.LockType = adLockOptimistic
.CursorType = adOpenKeyset
.Open
End With
Set Me.Recordset = rs
Set rs = Nothing
Set cn = Nothing
End Sub