473,396 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Re-Apply Sort After New Record

mjoachim
I have a form that loads with all data currently in an employee table. I am trying to allow the user to duplicate a record and then have the form re-apply the sort so that the newly created record is in order with the rest rather than being at the bottom of the form.

Here is my current copy/paste code:

Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunCommand acCmdSelectRecord
  2. DoCmd.RunCommand acCmdCopy
  3. DoCmd.GoToRecord , , acNewRec
  4. DoCmd.RunCommand acCmdPaste
I have tried adding these to my code to complete the sort, but they haven't worked:

Expand|Select|Wrap|Line Numbers
  1. Me.OrderBy "Employee Number"
  2. Me.OrderByOn
Expand|Select|Wrap|Line Numbers
  1. DoCmd.SetOrderBy "Employee Number"
I have also tried the usual me.requery and me.refresh. What am I missing?? How can I have the form re-sort the data after adding the new record?

Also, any suggestions on a way to set/keep focus on the newly added record after the sort is complete? I have a unique record ID field available.
Jul 10 '18 #1
8 3587
zmbd
5,501 Expert Mod 4TB
If your sort is already set in the form properties then
try me.Requery
After the user enters the new record. This can either be in your code that handles the new record entry or in the form's Form_AfterUpdate() event.

Note, the me.Refresh may not be enough depending on how your code/query is setup because the form basically takes a snapshot of the associated dataset and the refresh only updates the existing records as they are shown in the form. The requery forces the form to go back to the database and take a new look at the dataset.MS Article: Refresh or requery data
Jul 10 '18 #2
Adding me.requery after my new record code doesn't work.

Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunCommand acCmdSelectRecord
  2. DoCmd.RunCommand acCmdCopy
  3. DoCmd.GoToRecord , , acNewRec
  4. DoCmd.RunCommand acCmdPaste
  5.  
  6. Me.Requery
The new record is still at the bottom of the form.
Jul 10 '18 #3
zmbd
5,501 Expert Mod 4TB
Is your sort order set in the form or by VBA

Have you post your entire "new record" code?

Explicitly set the sort order on prior to the requery:
Me.OrderByOn = True
Jul 10 '18 #4
The sort order is set in the form properties. I stand partially corrected though as I now have it working using me.requery. Above is the entire code for Copy Record button and it wasn't working initially, but I cleared out a bunch of unnecessary code in other areas of the form that may have been conflicting with it and now it works.

Now, though, regardless of which record I am on when I click my copy button, the top record has the focus afterword. Suggestions on how to bring focus back to the new record? Would using something like DoCmd.GoToRecord work?
Jul 10 '18 #5
zmbd
5,501 Expert Mod 4TB
I'm glad that things are working, and yes, it's not uncommon for stray code to cause some unintended effects.

We do normally ask that each thread have a single question; however, we may be able to stretch this a tad :)

When you ask "...bring focus back to the new record?" do you mean to move the record index to the "New Record" portion of the form or do you mean to move the index to the newly created record?

The first is quite straight forward:
DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
This will move the index to the row that typically has the "(New)" value for a field with "autonumber" as the datatype.


The second will take a bit more code and would need to have a bit more complex code and would use the "...unique record ID..." you mentioned earlier.
Jul 11 '18 #6
twinnyfo
3,653 Expert Mod 2GB
@zmbd:
The first is quite straight forward:
DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
This will move the index to the row that typically has the "(New)" value for a field with "autonumber" as the datatype.
I believe this method will take the form to a "new" record, not just the "newest" record. Since we have requeried the form, the recordset is now set and that record just created is no longer a new record.

The alternate solution, to use the unique record ID, is the way to go. You will have to determine that uniqe record ID and then go to that record after the Form is requeried.
Jul 11 '18 #7
zmbd
5,501 Expert Mod 4TB
@twinnyfo:
You are correct.
I was asking OP to clarify if they wanted to go to the add a new record or to the recently created record which mentioned at the end of the post would be most easily found using the Primary Key for the record.
Jul 11 '18 #8
twinnyfo
3,653 Expert Mod 2GB
Z,

Yes, I re-read your post more carefully this time. All clear now. Thanks!
Jul 11 '18 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Nova's Taylor | last post by:
Hi folks, I am a newbie to Python and am hoping that someone can get me started on a log parser that I am trying to write. The log is an ASCII file that contains a process identifier (PID),...
6
by: Lowell Kirsh | last post by:
I'm trying to emulate the sorted() method introduced in python 2.4. The only difference is that it takes a sequence as one of its arguments rather than being a method of the sequence class. Does my...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
17
by: perryche | last post by:
I have 5records, e.g. Rc1, Rc2, Rc3..., when user open up a form, I want it to open to a particular record (say Rc3) then when user chooses the Record Selector , it will go to Rc2 and , it will go...
6
by: Becker | last post by:
Very simple, I have a datagrid on a windows form. I load some rows in it. I click a column header and it sorts ascending. I click it again it sorts descending. This is great, but what I want to...
2
by: Mac via DotNetMonster.com | last post by:
Hi all, I have a datagrid on a form which I have set the datasource (through the form designer) to be a dataset that I have added to the form, also through the form designer. I then have added a...
4
by: Tony WONG | last post by:
i have a number of forms with fax numbers to come up into arrays and then combine to string. after that i design the flow 1. break the string to array now the string looks like this...
3
by: Vic Spainhower | last post by:
Hello, I have an HTML table that is being constructed from a MySQL table and displays a form that includes a check box on 1 of the fields on the form for each record. I have included in this PHP...
3
by: soapshack | last post by:
I'm trying to capture the values that are currently being Autofiltered by the user so I can re-apply them after I'm done with the code. Basically, if I can capture these four or five main column's...
0
Seth Schrock
by: Seth Schrock | last post by:
Introduction There have been a few times recently that I have had to make it so that users could easily change the order of records. This comes in handy when planning tasks and you don't...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.