Solved

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

Posted on 2008-06-25
12
216 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
Comment Utility
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
Comment Utility
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

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
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
 

Author Comment

by:T1080
Comment Utility
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

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
" 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
Comment Utility
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 58

Assisted Solution

by:harfang
harfang earned 200 total points
Comment Utility
> 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

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
"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
Comment Utility
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

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
"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
Comment Utility
> 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
Comment Utility
Thanks for your input.  Your solutions worked great.
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
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…
Familiarize people with the process of utilizing SQL Server views 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 Microsoft Access…
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 …

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now