473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How do I open a form to the record just created?

Seth Schrock
2,965 Expert 2GB
I'm using the following vba code to create a new record in one of my tables:

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnCreateInvoice_Click()
  2. Dim dbBilling As DAO.Database
  3.     Dim rstInvoices As DAO.Recordset
  4.  
  5.     Set dbBilling = CurrentDb
  6.     Set rstInvoices = dbBilling.OpenRecordset("tblInvoices")
  7.  
  8.     rstInvoices.AddNew
  9.     rstInvoices("CustomerID").Value = Forms!frmCustomer!CustomerID
  10.     rstInvoices("BeginDate").Value = Forms!frmCustomer!txtStartDate
  11.     rstInvoices("EndDate").Value = Forms!frmCustomer!txtEndDate
  12.     rstInvoices.Update
The primary key for tblInvoices is an autonumber field titled InvoiceID. After this code is run, I want to open frmInvoices (based on tblInvoices) to the record just created. I believe that I could do this by having the form open to the last record. I just want to make sure that this is a fool proof method. I don't know of a way to get the InvoiceID for the record that was created, otherwise I would just to the record that way. So my idea was to add the following code below the above code:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "frmInvoices"
  2.     DoCmd.GoToRecord , , acLast
Is this a good way to do this?
Nov 28 '11 #1

✓ answered by TheSmileyCoder

Sorry, should have been able to spot the issue from #5, but guess I wasn't paying enough attention.

The issue, is that after the recordset has updated, it doesn't stay on the new record, which I thought it did. Therefore:
Expand|Select|Wrap|Line Numbers
  1.     rstInvoices.AddNew 
  2.     rstInvoices("CustomerID").Value = Forms!frmCustomer!CustomerID 
  3.     rstInvoices("BeginDate").Value = Forms!frmCustomer!txtStartDate 
  4.     rstInvoices("EndDate").Value = Forms!frmCustomer!txtEndDate 
  5.     rstInvoices.Update 
  6.     lngInvoiceID = rstInvoices!InvoiceID 
causes lngInvoiceID to be the ID of the first record (or possibly whichever record the recordset was pointing to when we started the new record).

The solution howerver is simple, we just put the assingment in between the AddNew and update.

Expand|Select|Wrap|Line Numbers
  1.     Dim lngInvoiceID As Long 
  2.     rstInvoices.AddNew 
  3.     rstInvoices("CustomerID").Value = Forms!frmCustomer!CustomerID 
  4.     rstInvoices("BeginDate").Value = Forms!frmCustomer!txtStartDate 
  5.     rstInvoices("EndDate").Value = Forms!frmCustomer!txtEndDate 
  6.     lngInvoiceID = rstInvoices!InvoiceID    
  7.     rstInvoices.Update 
and then proceed to open the form as previously described.

15 2411
TheSmileyCoder
2,322 Expert Mod 2GB
The goto last record is not foul proof, since it would depend on the sorting applied to your recordset. There is a reason for tables to have an ID field, and you should always use that.

I have rewritten parts of your code to show how to use such an id.

Expand|Select|Wrap|Line Numbers
  1.     rstInvoices.AddNew 
  2.     rstInvoices("CustomerID").Value = Forms!frmCustomer!CustomerID 
  3.     rstInvoices("BeginDate").Value = Forms!frmCustomer!txtStartDate 
  4.     rstInvoices("EndDate").Value = Forms!frmCustomer!txtEndDate 
  5.     rstInvoices.Update 
  6.     Dim lngInvoiceID as long
  7.     lngInvoiceID=rsInvoices!InvoiceID
  8.  
  9.     Docmd.OpenForm "frmInvoices",,,"InvoiceID=" & lngInvoiceID
  10.  
If you have any questions please ask again :)
Nov 28 '11 #2
Seth Schrock
2,965 Expert 2GB
I just tried it and it is going to the first record in the table instead of the record just created. I'm lost when it comes to recordset commands so I don't know what is wrong.
Nov 28 '11 #3
TheSmileyCoder
2,322 Expert Mod 2GB
A few things to try:

If the form is allready open when you run the docmd.OpenForm, access will not Re-open it, but simply set the focus to the form. This however does not requery the form, and while most recordsets will show changes to existing records, they will not automatically load new records. You will need to requery the form for that.

I dont know what your level of VBA expertise are, but have you debugged to make sure that your variable is passed properly?
For more info on debugging VBA check: Debugging VBA


Im guessing its the first of the two being the issue, allthough its just a guess since I dont have that much information at hand. You can then try:
Expand|Select|Wrap|Line Numbers
  1. Forms!frm_Invoice.Requery
  2. Docmd.OpenForm "frmInvoices",,,"InvoiceID=" & lngInvoiceID 
This assumes the form will always be open, when you run your code, if thats not the case you need to add code to check whether the form is open.
Nov 28 '11 #4
Seth Schrock
2,965 Expert 2GB
I tried it making sure that the form was closed and that didn't work. On to the debugging... I have read some about it in a book, but never done it for real.

Here is some information about the contents of tblInvoices. There are currently four records with the following PKs: 4, 10, 11, and 12 (due to deleting previous test records). When I debug my code, I set a break point at the
Expand|Select|Wrap|Line Numbers
  1. lngInvoiceID=rstInvoices!InvoiceID
line. I then stepped into the line. I then put ? lngInvoiceID in the Imeediate window and it comes back with 4 (the PK for the first record in the table). Here is a copy of the code that I'm running currently:
Expand|Select|Wrap|Line Numbers
  1. Private Sub btnCreateInvoice_Click()
  2. Dim dbBilling As DAO.Database
  3.     Dim rstInvoices As DAO.Recordset
  4.  
  5.     Set dbBilling = CurrentDb
  6.     Set rstInvoices = dbBilling.OpenRecordset("tblInvoices")
  7.  
  8.     rstInvoices.AddNew
  9.     rstInvoices("CustomerID").Value = Forms!frmCustomer!CustomerID
  10.     rstInvoices("BeginDate").Value = Forms!frmCustomer!txtStartDate
  11.     rstInvoices("EndDate").Value = Forms!frmCustomer!txtEndDate
  12.     rstInvoices.Update
  13.     Dim lngInvoiceID As Long
  14.  
  15.     lngInvoiceID = rstInvoices!InvoiceID
  16.  
  17.     DoCmd.OpenForm "frmInvoices", , , "InvoiceID=" & lngInvoiceID
  18.  
  19.     DoCmd.OpenReport "rptInvoice", acViewPreview
  20.     DoCmd.SetWarnings False
  21.     DoCmd.OpenQuery "uqryAssignInvoiceID"
  22.     DoCmd.SetWarnings True
  23.  
  24.     Me.sfrmInvoices.Requery
Nov 28 '11 #5
TheSmileyCoder
2,322 Expert Mod 2GB
Im not really spotting what could be the issue here. Your code to me looks fine.

Is your frm_Invoices based on the table tblInvoices or a query, and if its a query does that query include the InvoiceID field?

What is uqryAssignInvoiceID used for? Please confirm that your InvoiceID is an autonumber as first mentioned.


PS. That would pretty much be the same method of debugging I would start off with for a similar problem.
Nov 28 '11 #6
Seth Schrock
2,965 Expert 2GB
Currently, frmInvoices is based on tblInvoices, but I plan on switching it to a query so that I can combine the information from two tables. It will contain the InvoiceID.

InvoiceID is an autonumber. uqryAssignInvoiceID is a long story. Basically, it just assigns an InvoiceID to the services bought so that they are assigned to that Invoice ID. To get the correct Invoice ID, I have the criteria set to find the Invoice ID of the record that has the BeginDate, EndDate, and CustomerID match the values on frmCustomer. This was my way around not knowing how to retrieve the InvoiceID of the record just created.
Nov 28 '11 #7
TheSmileyCoder
2,322 Expert Mod 2GB
Sorry, should have been able to spot the issue from #5, but guess I wasn't paying enough attention.

The issue, is that after the recordset has updated, it doesn't stay on the new record, which I thought it did. Therefore:
Expand|Select|Wrap|Line Numbers
  1.     rstInvoices.AddNew 
  2.     rstInvoices("CustomerID").Value = Forms!frmCustomer!CustomerID 
  3.     rstInvoices("BeginDate").Value = Forms!frmCustomer!txtStartDate 
  4.     rstInvoices("EndDate").Value = Forms!frmCustomer!txtEndDate 
  5.     rstInvoices.Update 
  6.     lngInvoiceID = rstInvoices!InvoiceID 
causes lngInvoiceID to be the ID of the first record (or possibly whichever record the recordset was pointing to when we started the new record).

The solution howerver is simple, we just put the assingment in between the AddNew and update.

Expand|Select|Wrap|Line Numbers
  1.     Dim lngInvoiceID As Long 
  2.     rstInvoices.AddNew 
  3.     rstInvoices("CustomerID").Value = Forms!frmCustomer!CustomerID 
  4.     rstInvoices("BeginDate").Value = Forms!frmCustomer!txtStartDate 
  5.     rstInvoices("EndDate").Value = Forms!frmCustomer!txtEndDate 
  6.     lngInvoiceID = rstInvoices!InvoiceID    
  7.     rstInvoices.Update 
and then proceed to open the form as previously described.
Nov 28 '11 #8
Seth Schrock
2,965 Expert 2GB
That makes perfect sense. I will try it when I get to work tomorrow and let you know the results.
Nov 28 '11 #9
NeoPa
32,556 Expert Mod 16PB
Unfortunately the value of rstInvoices!InvoiceID is only set at the point of the call to rstInvoices.Update :-(

May I ask why you are trying to recreate what Access already does for you by creating your record in code? It's generally a recipe for confusion (although there are possible reasons that could make sense in limited circumstances).

I suspect the ID could be retrieved in either situation by navigating to the last record currently available in the existing recordset by the way.
Nov 28 '11 #10
TheSmileyCoder
2,322 Expert Mod 2GB
NeoPa Unfortunately the value of rstInvoices!InvoiceID is only set at the point of the call to rstInvoices.Update :-(
Sorry, NeoPa, you are incorrect. Autonumber fields retrieve their value at the point of RecordSet.AddNew or possibly when the first value is entered into any field. Should be easy to test which of the two are true, if one should be interested.
Nov 28 '11 #11
NeoPa
32,556 Expert Mod 16PB
You're correct Smiley (of course). I was indeed mistaken and that should work well.

PS. Adding data onto a form in the New Record position is equivalent to the .AddNew method. Two sides of the same coin.
Nov 28 '11 #12
TheSmileyCoder
2,322 Expert Mod 2GB
When you start adding data to a forms new record, the Insert event happens, which I believe is equivalant to the .AddNew.

It is actually a common misunderstanding that the Insert event happens when you save a new record, however it happens at the point of first dirtying a new record
Nov 29 '11 #13
Seth Schrock
2,965 Expert 2GB
It worked perfectly. Thank-you very much.
Nov 29 '11 #14
TheSmileyCoder
2,322 Expert Mod 2GB
Thats nice to hear. :)
Nov 29 '11 #15
NeoPa
32,556 Expert Mod 16PB
Smiley:
It is actually a common misunderstanding that the Insert event happens when you save a new record, however it happens at the point of first dirtying a new record
Indeed. I was actually aware of that, having discovered it only quite recently. What I hadn't realised was that the creation of the AutoNumber value was tied to that. It does makes sense I suppose.
Nov 29 '11 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: allyn44 | last post by:
Hello, I have built a serch form for users to edit records. I only want them to pull up the record they need, and I want to check for nulls. There should not be dupes becasue the underlying...
2
by: bobh | last post by:
In Access97 on a bound form, client wants a button that will duplicate the currently displayed record and give it a new meeting nbr. I have done it except for being able to display the new record...
16
by: usenet | last post by:
I want to be able to use the record numbers of a sub-form, are they available anyhow in VB (Access 2003)? I want to use the sub-form record number as *part* of the primary key for the table...
2
by: erick-flores | last post by:
Hello all Form A (pk) & Form B (fk) I want to display my Form B empty whenever is the first time, that the pk form Form A, is enter. I click a button to open Form B. BUT if the pk from Form A...
4
by: gary.cobden | last post by:
Hi I have a form/subform where a new record is created on the subform by a button click. After the new record is created, I then need to go directly and display the new record in the subform....
3
by: kev | last post by:
Hi folks, I have a form for registration (frmRegistration) whereby i have two buttons. One is Save which saves record using the OnClick property. I used wizard to create the save button. The...
3
by: ApexData | last post by:
I am using a continuous form for display purposes. Above this form, a single record is displayed so that when the user presses my NewButton they can enter a NewRecord which gets added to the...
14
by: keri | last post by:
Hi, Simple version of the question..... How do I use the where clause of the open form command to show an account with a matching ID to be displayed when the form is opened? Eg. I select a...
1
by: Paul Brady | last post by:
First, apologies iif this is an old problem. I do read this group, but I may have missed it. When I open a form in A2K to put data into a record (or create a new record), all is well provided I...
4
by: Rotorian | last post by:
Hello, This is a design question not a "how to" I would like know if it can be done before searching for a non existant answer :). If a record is created via a form, can a tab be created on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.