Solved

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

Posted on 2008-06-25
12
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
12 Comments
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

717 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