Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2008-06-25
12
Medium Priority
?
233 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 800 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

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 800 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
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.
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…

618 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