Solved

Access command button: Add a New Record that has the same value as the current record.

Posted on 2008-06-25
12
219 Views
Last Modified: 2013-11-28
My form is based on a table that has many order numbers (not unique) and many item numbers (unique).  My access form is a single form where I can view one item number at a time.  Id like to introduce a new command button which adds a new record that has the same order number as the record Im currently on.  How can I achieve this?
0
Comment
Question by:T1080
  • 5
  • 5
  • 2
12 Comments
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Access MVP) earned 200 total points
ID: 21870435
Private Sub btnAddSameOrderNumber_Click()

Dim vOrderNum
vOrderNum = me![OrderNumber]
docmd.GoToRecord,,acNewRec
me![OrderNumber]=  vOrderNum

End Sub

Change to your field names accordingly ...

mx
0
 

Author Comment

by:T1080
ID: 21870758
MX:
That code works, but It looks like I need to do a Docmd.Requery after that code.  My only problem is that after the requery I need to be brought back to the same item# that I created.  any thoughts?

Troy
0
 
LVL 75
ID: 21870774
Well, this:

me![OrderNumber]=  vOrderNum

will start - ie create, but not Save ... the new record.  IE, that will cause a 'new record' to become Dirty.

Now, if you want to save it immediately, then after that line of code, you need:

Me.Dirty = False   ' ** this will save the 'new' record

No need for a Requery.

mx
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:T1080
ID: 21870934
Great, I will add that piece of code.  My underlying table is linked to a query and will only update with a requery.  Simply running Docmd.requery will bring me back to the first record.  How do I get the Docmd.Requery to find the new record that was created via the unique item #?
0
 
LVL 75
ID: 21870943
" My underlying table is linked to a query and will only update with a requery."

That should not make any difference.  The Me.Dirty = False will save the new record *and* keep you on that record, which is what you want, right ?

mx
0
 

Author Comment

by:T1080
ID: 21870949
MX:
Correct, I do want the record saved.  But since I created a new record some of the values on my form are blank because they are not in the underlying query.  Me.Dirty, Refresh, nor Reclac solve my problem, the only solution that works is Docmd.Requery.  But after the requery how do I get back to the record I created since the requery brings me back to the first record.

Thanks,

Troy
0
 
LVL 58

Assisted Solution

by:harfang
harfang earned 200 total points
ID: 21871372
> some of the values on my form are blank because they are not in the underlying query

This is a strange statement; I wonder what query you are using, as I can't imagine anything that would fit this description.

Anyway, to complement mx's answer, this is how you can return to a record after a requery.

(°v°)
    Dim IWasHere As String
 
    IWasHere = "TheID=" & Me.TheID
    Me.Requery
    Me.Recordset.FindFirst IWasHere

Open in new window

0
 
LVL 75
ID: 21871430
"But since I created a new record some of the values on my form are blank because they are not in the underlying query"

But, is that because they were not input after the Initial Save ... which you could do, then Save again ... or Save once after inputting in all the values.

Not really clear on the setup here ....

mx
0
 

Author Comment

by:T1080
ID: 21871880
harfang:
The line of code: IWasHere = "TheID=" & Me.TheID
I find it interesting that this code refers to TheID in quotes and then refers to the control.
Can you explain why this code is structured in such fashion?  I'd like to know the logic behind it so I can learn the code instead of copy it.

MX:
I agree that I have not explained my set up very well.  To give you a general idea, my form is based on a query which uses a subset of queries.  I simplified the structure for example purposes.  I will try your suggestions tomorrow and will let you know how it goes.

Thanks,

Troy
0
 
LVL 75
ID: 21871904
"based on a query which uses a subset of queries.  "

Ahh, well then it would seem that that query would not be updateable ?

mx
0
 
LVL 58

Expert Comment

by:harfang
ID: 21874245
> IWasHere = "TheID=" & Me.TheID

If you are on record 23, the string evaluates to "TheID=23", which is a perfectly formed WHERE clause, suitable for filtering of finding a record.

(°v°)
0
 

Author Closing Comment

by:T1080
ID: 31470794
Thanks for your input.  Your solutions worked great.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question