I'm using the following vba code to create a new record in one of my tables: - Private Sub btnCreateInvoice_Click()
-
Dim dbBilling As DAO.Database
-
Dim rstInvoices As DAO.Recordset
-
-
Set dbBilling = CurrentDb
-
Set rstInvoices = dbBilling.OpenRecordset("tblInvoices")
-
-
rstInvoices.AddNew
-
rstInvoices("CustomerID").Value = Forms!frmCustomer!CustomerID
-
rstInvoices("BeginDate").Value = Forms!frmCustomer!txtStartDate
-
rstInvoices("EndDate").Value = Forms!frmCustomer!txtEndDate
-
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: - DoCmd.OpenForm "frmInvoices"
-
DoCmd.GoToRecord , , acLast
Is this a good way to do this?
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: - rstInvoices.AddNew
-
rstInvoices("CustomerID").Value = Forms!frmCustomer!CustomerID
-
rstInvoices("BeginDate").Value = Forms!frmCustomer!txtStartDate
-
rstInvoices("EndDate").Value = Forms!frmCustomer!txtEndDate
-
rstInvoices.Update
-
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. - Dim lngInvoiceID As Long
-
rstInvoices.AddNew
-
rstInvoices("CustomerID").Value = Forms!frmCustomer!CustomerID
-
rstInvoices("BeginDate").Value = Forms!frmCustomer!txtStartDate
-
rstInvoices("EndDate").Value = Forms!frmCustomer!txtEndDate
-
lngInvoiceID = rstInvoices!InvoiceID
-
rstInvoices.Update
and then proceed to open the form as previously described.
15 2411
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. -
rstInvoices.AddNew
-
rstInvoices("CustomerID").Value = Forms!frmCustomer!CustomerID
-
rstInvoices("BeginDate").Value = Forms!frmCustomer!txtStartDate
-
rstInvoices("EndDate").Value = Forms!frmCustomer!txtEndDate
-
rstInvoices.Update
-
Dim lngInvoiceID as long
-
lngInvoiceID=rsInvoices!InvoiceID
-
-
Docmd.OpenForm "frmInvoices",,,"InvoiceID=" & lngInvoiceID
-
If you have any questions please ask again :)
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.
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: - Forms!frm_Invoice.Requery
-
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.
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 - 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: - Private Sub btnCreateInvoice_Click()
-
Dim dbBilling As DAO.Database
-
Dim rstInvoices As DAO.Recordset
-
-
Set dbBilling = CurrentDb
-
Set rstInvoices = dbBilling.OpenRecordset("tblInvoices")
-
-
rstInvoices.AddNew
-
rstInvoices("CustomerID").Value = Forms!frmCustomer!CustomerID
-
rstInvoices("BeginDate").Value = Forms!frmCustomer!txtStartDate
-
rstInvoices("EndDate").Value = Forms!frmCustomer!txtEndDate
-
rstInvoices.Update
-
Dim lngInvoiceID As Long
-
-
lngInvoiceID = rstInvoices!InvoiceID
-
-
DoCmd.OpenForm "frmInvoices", , , "InvoiceID=" & lngInvoiceID
-
-
DoCmd.OpenReport "rptInvoice", acViewPreview
-
DoCmd.SetWarnings False
-
DoCmd.OpenQuery "uqryAssignInvoiceID"
-
DoCmd.SetWarnings True
-
-
Me.sfrmInvoices.Requery
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.
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.
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: - rstInvoices.AddNew
-
rstInvoices("CustomerID").Value = Forms!frmCustomer!CustomerID
-
rstInvoices("BeginDate").Value = Forms!frmCustomer!txtStartDate
-
rstInvoices("EndDate").Value = Forms!frmCustomer!txtEndDate
-
rstInvoices.Update
-
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. - Dim lngInvoiceID As Long
-
rstInvoices.AddNew
-
rstInvoices("CustomerID").Value = Forms!frmCustomer!CustomerID
-
rstInvoices("BeginDate").Value = Forms!frmCustomer!txtStartDate
-
rstInvoices("EndDate").Value = Forms!frmCustomer!txtEndDate
-
lngInvoiceID = rstInvoices!InvoiceID
-
rstInvoices.Update
and then proceed to open the form as previously described.
That makes perfect sense. I will try it when I get to work tomorrow and let you know the results.
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.
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.
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.
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
It worked perfectly. Thank-you very much.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |