Solved

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

Posted on 2008-06-25
12
224 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

740 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