Modules & VBA :: SQL String Does Not Retrieve Primary Key Newly Added Record
Jul 2, 2014
I have an Access 2010-form which inserts a record in a MS SQL 2008-database by using an ADODB-connection. I need to retrieve the primary key of the newly added record.
With code at the bottom I create a SQL-string which is stored in variable strSQL. If I execute the SQL-string directly in a MS SQL 2008 Query Window, the record is inserted and the MS SQL 2008-result pane shows a column "ID" with the primary key of the newly added record.
If I execute same SQL-string in MS Access 2010 the record is inserted. However, the code fails on Debug.Print rs![ID] with error "Item cannot be found in the collection corresponding to the requested name or ordinal". Same error appears if I use Debug.Print rs.Fields(0). I've enable the reference "Microsoft ActiveX Data Objects 2.8 Library". It looks as the recordset rs is closed as soon the command "Set rs = cn.Execute(strSQL)" finished.
Why I can't retrieve the Primary Key with VBA-code below?
Code:
Private Sub cmdSave_Click()
dim cn As ADODB.Connection
dim rs As ADODB.Recordset
set cn=New ADODB.Connection
[Code] .....
View Replies
ADVERTISEMENT
Jun 25, 2014
I have a form that lets you add records to my table, it works well and I can see the records in my table but then once I use my search form, which also is working well, the new records won't come up when I do a search, it just keeps searching through the records that were there when I created the search form.
View 4 Replies
View Related
Oct 13, 2015
Basically I have one table with all relevant information needed, etc, and I have the original spreadsheet data inputted to the database. The original data appears fine when a search is done, eg, the user searches for something using a form that then uses a query and ultimately brings up the information that matches. In the queries, I use the same basic criteria
"LIKE "*" & [Forms]![FormName]![FieldName] & "*" "
My question is, now that I have a form to add a new record to the table so a user can add to it, what would be the best way to get the added record to appear during the search, like the original records? I have used " IS Not Null" at the end of the criteria within the query, but it tends to bring up all the new records no matter what the user searches.
View 3 Replies
View Related
Apr 21, 2013
I created a database. exported some Excel data into Access tables, created a number of lookups, default values etc. and created a pretty simple query.
My problem is that after importing and tweaking the data, when I add new records, my queries do not pick them up!
I have tried:
1. saving, closing, opening and re-running the queries.
2. putting an Nz expression for each field in the query as I read that null values may cause a problem.
3. wrote the query again, field by field to see if all records were received.
4. Exported the table back to Excel and imported to a new Access table in my database
None of this works.
View 7 Replies
View Related
Sep 16, 2014
I'm trying to add a search function the searches with ever letter I add to the string in the search box. if the string is not in the recordset then vbred the textbox.
Here's my code:
Private Sub txtGroupNr_KeyPress(KeyAscii As Integer)
Set RstRecSet = Nothing
Set db = CurrentDb
On Error Resume Next
If IsNull(txtGroupNr) Or txtGroupNr = "" Then
' MsgBox "Please enter a Group Number to use as the search criteria", _
[Code] ....
View 4 Replies
View Related
Jun 24, 2014
I have a combo box that pulls account name data from tblAcctInfo. the combo box has an OnChange event which updates a textbox, txtAcctAddr.
when i have a new account that i would like to be listed in the combo box, i use a form, frmAcctAdd, to add a record to tblAcctInfo.
what i would like to do, is:
1) when i type a new value in the combo box that isn't in the list, have that string value pre-populate in the frmAcctAdd.
2) when i have added the new account info into frmAcctAdd and then saved the record, i would like the new value to pre-populate in the combo box, with the txtAcctAddr textbox also updated via the OnChange event (or maybe a different event is more appropriate?).
I have created a long version of this which requires a lot more user interaction (1-user typing in a new value into the combo box, 2-user RE-typing the SAME value into a data entry form, frmAcctAdd, 3-user saving the new record, 4-user re-clicking the combo box and selecting the newly added value) but i am trying to streamline the data entry with auto-populated fields.
View 6 Replies
View Related
Nov 5, 2014
I am adding new record into subform via recordsetclone method. The problem is that record is added but on save it does not appear in the table. If add this record manual using subform everything works. When record added manually update of the record works fine.
C
'Add Wastage value to flooring area section
Private Sub Wastage_AfterUpdate()
Dim rsFlArea As DAO.Recordset
Dim Wastage As Double
Dim Item As String
Set rsFlArea = Me.OrderFloorAreaEdit.Form.RecordsetClone
[Code] .....
View 6 Replies
View Related
Sep 19, 2006
Hokay, firstly my apologies if this is the wrong subforum, but since my question revolves around the behaviour of one of my forms I guessed the thread should go here.
I have a very simple little database which I use to log RMAs (Returned Merchandise Authorizations). The database consists of three forms:
Form A - The switchboard. The main menu, works fine.
Form B - The View/Edit window. Allows me to look at the records in the database and alter them if necessary, works fine.
Form C - The Add window. Has the same form layout as B, but allows for adding records only. This is the one causing problems.
Now the situation is that when I enter Form C, the box for the RMA number gets autofilled out - which is correct - thus creating a new record. However, what I want to achieve is a button on the form that will allow me to quit back to the main menu without saving the record that has been created by opening the form. This is for situations where the form has been accidentally opened, or where a form has been started, but is not required to be finished.
View 2 Replies
View Related
Oct 6, 2006
Hi All,
I have a form from with a button that exports data in to excel using the following on click code
DoCmd.OutputTo acOutputQuery, "qrySoftPDR2", acFormatXLS, "FOBPDR.xls", True 'open in Excel
The problem is that if I go in to an existing record it works fine.
However if I have just input the record then it comes out blank. I'm guessing there is some sort of record update code I can use, but I've been unable to figure oput what it may be,
Thanks again
View 1 Replies
View Related
Sep 6, 2014
I got this function which is supposed to find a record based on the primary key, and then give three fields a value if they should be empty.
Public Function DoesXrefExist(faPersonID As Long, faRoleCode As String, faDorpID As Long, faDorpCode As String)
If (DLookup("PersonID", "PEOPLE", "PersonID =" & faPersonID) > 0) Then
End Function
Thats as far as i have got...for the next part.I get the feeling that i need to use the recordset object that exists somewhere.How do i access that recordset object? I have not been using any DAO or ADO, just straight VBA. I know i can create recordsets and stuff using DAO/ADO, but there must be a recordset that has been created already by the Dlookup command...
View 5 Replies
View Related
Apr 14, 2015
I launch a 'CreateNewRecords' form from a 'MainForm' form. When the 'CreateNewRecords' form closes, the new record is visible on 'Main Form'.The underlying query is unsorted, so this code in the AfterUpdate event of 'CreateNewRecords' puts the cursor on the last record displayed on 'MainForm', which is the newly created record:
Forms!frmMainForm.Requery
With Forms!frmMainForm.RecordsetClone
.MoveLast
Forms!frmMainForm.Bookmark = .Bookmark
End With
However, I intend to sort the underlying query, which means that a newly created record may appear in the middle of the records displayed on 'MainForm'.
What I want to have happen is that after a new record is created by 'CreateNewRecords', the underlying query is requeried so that all current records are displayed on 'MainForm' but also that the cursor rests on the newly created record (rather than default to the first record).
View 7 Replies
View Related
Jul 29, 2013
Can I look up and verify data on a "second" form based on a selected record from first (still open) form.
I am trying to allow users to select a User Name from a combo box list and then open "Change Password" form when they select "Change Password" for that selected user name.
My problem is that I can't figure out how to associate and verify the data tied to the user name selected on the previous (Login) form ( I am trying to validate the old password tied to that selected record).
I have the first login form created, and it's working just fine. I also have the change password form created (and it's displaying the user name selected from the first form using:
Code:
Private Sub Form_Load()
With Forms![frmLogin]![cboUserName]
Me.txtPwdChgUserID = .Column(2, .ListIndex)
End With
EndSub
I also have the code written to validate and confirm old password, new password and validate new password (when the save button is clicked). I have yet to update the password with the new password (still trying to figure that out).
Attached zip file has screen shots of the two forms.
View 3 Replies
View Related
Apr 24, 2015
After a lot of reading and consolidating VBA codes for audit trail.How My Audit Trail Works..A module was made for a function named as "Changes" .Then inserted into before update event of a form where I will do the editing of the records.Then I made a table named as Audit.Inside this table I made all the fields I needed such as:
*AuditRecordId[autonumbered]
*FormName[The name of the form for editing]
*Index[The record ID of the record being edited]
*ControlName[The Field being edited]
*DateChanged[Date change was done]
*TimeChanged[Time change was done]
*PriorInfo[for the old value of data being changed]
*NewInfo[For the new value of data changed]
*CurrentUser[The user base on log in form that was set to Global into another module]
*Reason[The reason for changing for future references]
And Here is the Function Code:
Code:
Option Compare Database
Option Explicit
Function Changes()
Dim db As Database
Dim rs As Recordset
Dim strSQL As String
Dim strCtl As String
Dim strReason As String
[code]....
This audit trail function is valid only for one(1) form, due to the limitation of
Code:
Screen.ActiveForm.Controls("SUP ID").Value
where "SUP ID" is the primary key of the record being updated/Change, so if there are Five(5) tables that needs audit trail, there will be also Five(5) forms, as well as Five(5) Function Changes namely; Changes(), Changes1(),Changes2(),etc... because all the table do have their own sets of primary Key.
Is there a shortcut, in such a way that the "rs!Index" will automatically return a value, equivalent to the Primary Key/Record Id of the record being updated/change, given that there are different updating forms for each table to be updated?
View 5 Replies
View Related
Jun 13, 2013
I am provided a spreadsheet that contains multiple rows of similar data; each row/record represents a different stage in the process of financial transactions (requisition, purchase order, & voucher payment). Each financial transaction has these three records, with the amounts in one of three columns (pre-encumbrance, encumbrance, and expense), depending on the process.
What I am really after is the fuller, more detailed description that is apparently only available for the two records I don't want to import into the database (which is tracking only expenses and not the other two stages of the process). There is apparently no way to cross-reference these multiple rows due to the way the original database was designed (and we apparently have no control over this).
After importing the spreadsheet into Access, I would like to match the partial text string (truncated description) to the full description in another record, and update the record with the truncated description to the full description. To make mattes more complicated, I will also have to match values in the "pre-enc" or "enc" field with the "exp" field across these three records to make sure the correct descriptions are being matched because the truncated description will match multiple distinct records with the longer description.
TypeDescrPreEncEncExp
VOUJsmith-Instructor, 12/16/13$0.00$0.00$45.00
POJSmith-Instructor, 12/16/13, Course1, Parking($45.00)$0.00$0.00
REQJSmith-Instructor, 12/16/13, Course1, Parking$0.00$45.00$0.00
VOUJsmith-Instructor, 12/16/13$0.00$0.00$221.13
POJSmith-Instructor, 12/16/13, Course1, Lodging($221.13)$0.00$0.00
REQJSmith-Instructor, 12/16/13, Course1, Lodging$0.00$221.13$0.00
View 5 Replies
View Related
Jul 14, 2015
I am trying to capture a newly arrived mail in outlook with respective subject line. The below code works for me on 2010 outlook but when new mail arrive in outlook 2013 ,Mrthod Items_ItemAdd do not get called.
Code:
Option Explicit
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
' (1) default Inbox
[Code]...
View 10 Replies
View Related
Feb 28, 2013
I've created two tables, one containing order data, the other additional order data. Not every order has additional order data.
First i've created them with no specific relationship and filling in data via form worked fine. If i added additional data, a new record in the additional order data table was created automatically.
Later i changed those tables to a "one to one" relationship by setting the long int field that links to the order data table to no duplicates. I just did it because i thought that's how it should bew. But since then i can't add additional order data via the form anymore, but get the error "Record(s) cannot be added; No corresponding record on the 'one' side" instead. I could just revert back to the one to many relationship, but it bothers me.
View 8 Replies
View Related
Jun 7, 2013
MS-Access VBA code to separate numbers and string from an alphanumeric string.
Example:
Source: 598790abcdef2T
Output Required: 598790
Source: 5789065432abcdefghijklT
Output Required: 5789065432
View 13 Replies
View Related
Mar 25, 2007
:confused: I am trying to add a "now" field to my table and records.
I currently have 2234 records in my table (old records) and I would like to be able to query only new records, or records within a date perameter. I have tried the "Now()" function but I am getting an error in the field when I go to add new records.
I know this is something simple that is eluding me and it is frustrating. I am currently scouring the forums for an answer, but if someone has the answer before I find it in here, could you pls help me out. I am in the process of rebuilding my database and would like to get everything done before I move to the next level.
Thanks,
Irish
View 4 Replies
View Related
Feb 4, 2005
Good morning all!
I have an ODBC link in an Access 2002 d/b to an AS400 table (with the ODBC refresh interval set to 10 seconds) and as the data changes, the data in form view changes just fine and dandy, thank you very much.
However, when a new record gets added to the AS400 physical file, the recordset doesnt update to show this, even if I use records>refresh. I have to close the form down and re-open it to see the new record.
Is there a method to achieve this?
Thanks!
Gordon
View 3 Replies
View Related
Apr 7, 2006
Hello,
I have created a booking system for loaning of camera kits.
I have created an Add Form which lets you add all of the information of the booking, I have also utilised a command button which opens up another form with the option to print a report based on the current record.
However, when I click on the command button to open up the other form, the record just added is not displayed, the first record in the table is shown..
So basically, is there a way to open up the form to the LAST record in the table, OR the specified record in the add form?
I'm sure there is a simple solution to this,
Thanks,
Dave
View 7 Replies
View Related
Mar 19, 2006
Hello,
Please tell me if there is a way to retrieve a value from a field in the previous form record in a query that don't have ID (autonumber), using data/time ascending.The function PrevRecVal() from QrySampl can only be used for query that include ID.
Thanks
View 8 Replies
View Related
Nov 29, 2005
I have a form where there are many users to enter or update data. I need to capture the username who created a record , then , on form load , I want system to check for username if it is same as the creator of a record , then allow update , otherwise don’t allow update , but allow only adding new records. How can I do this
View 3 Replies
View Related
Sep 8, 2006
Okay, here's the query as simplified as possible:
Month - Days - PreviousMonth
Month and Days are pulled from a table called Calendar. How do I get PreviousMonth to display the Month from the previous record?
I was looking at a ranking formula, and it seems like it compares the field to the previous one, but I am not sure how to apply it to my situation. The ranking formula I was looking at:
(Select Count(*) from Data Where [ProductA1] > [Data1].[ProductA1];)+1
Thanks in advance for the help.
View 3 Replies
View Related
May 4, 2005
I have an 'add new order' form which opens with all the fields blank and focus set to a combo box. The first thing the user must do is select a contract name or number from an unbound combo box. I then want the form to update and show all the relevant information in the form fields.
In the row source of the combo box I select all 3 fields from the same table (Project_Info). Only the contract number is visible all others are set to 0cm width in combo box properties.
In the afterupdate event I have the following code to set the field to the values from the select query
My code looks like this:
[code]
Private Sub ProjectContractNum_AfterUpdate()
Dim sOrder_idSource As String
Dim rs As Object
ProjectName = ProjectContractNum.Column(1)
proj_status = ProjectContractNum.Column(2)
End Sub
[end code]
My SQL in rowSource looks like this:
SELECT Project_Info.proj_name, Project_Info.proj_contract_no, Project_Info.proj_status FROM Project_Info;
Access tells me that I don't have a join key in my record set.
message reads as follows:-
Run-time error '2147352567 (80020009)':
Cannot add record(s);Join key of table Project_Info' not in record set.
Debug highlights this line:
proj_status = ProjectContractNum.Column(2)
'proj_status' is the name of the text box and the control source.
If I just use just the first 2 fields (project number and project name) these two work!!
What am I doing wrong?
Thx, Kev.
View 3 Replies
View Related
Jun 14, 2006
Hi everyone,
I have a form with a date field on it. I would like the date field to automatically enter today's date, which it does now, but also I would like it to retrieve the previous record's date and if it is different from today's date, I want the form to use the previous date as the default value.
Does anyone know of how I would go about this?
Thanks,
Chris
View 5 Replies
View Related
Aug 22, 2014
Table has a relationship with master table. Joined on TractID primary table - auto number, TractID child table - number. This works as it should when adding a new record.
What I am trying to do is create a new key for each new record added in the child table with an ID that looks like this: TractID.A, TractID.B, TractID.C etc. for each new record added in the child table. if so where do I look, how to accomplish it?
View 6 Replies
View Related