473,398 Members | 2,380 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,398 software developers and data experts.

How do I use buttons to reorder a list of tasks?

Seth Schrock
2,965 Expert 2GB
I have a form that I use to track jobs. Each job as a list of tasks for which I use a subform in Data Sheet view. I would like to be able to have buttons that can change the order of the tasks. All I really need are "Move Up" and "Move Down" buttons, but "Move to Top" and "Move to Bottom" would be nice. I have a "TaskOrder" field which I can populate manually, but when you want to move a task up five levels it gets hard to change all of the numbers. I'm guessing that I would need an update query to change the values and then requery the form, but I can't figure out what logic to use.
Aug 4 '12 #1

✓ answered by twinnyfo

Seth,

Just a few minor tweaks, to make it work properly.... and to maintain good coding practices....

Expand|Select|Wrap|Line Numbers
  1. Function MoveFirst(intCurrent As Integer) 
  2. On Error GoTo EH 
  3.     Dim db1 As Database 
  4.     Dim rst1 As Recordset 
  5.     Dim db2 As Database 
  6.     Dim rst2 As Recordset 
  7.     Dim strSQL As String 
  8.     Dim intRecords as Integer
  9.     Dim intCounter as Integer
  10.     strSQL = "SELECT * FROM tblTask WHERE TaskOrder = " & intCurrent & ";" 
  11.     Set db1 = CurrentDb() 
  12.     Set rst1 = db1.OpenRecordset(strSQL, dbOpenDynaset) 
  13.     strSQL = "SELECT * FROM tblTask WHERE TaskOrder <> " & intCurrent & " ORDER BY TaskOrder;"
  14.     'Need to sort the list by the current TaskOrder
  15.     'No need to have any other criteria
  16.     Set db2 = CurrentDb() 
  17.     Set rst2 = db2.OpenRecordset(strSQL, dbOpenDynaset)
  18.     'I had db1 in my original code..... (Sorry), although not sure it would matter....
  19.     If Not rst2.EOF Then
  20.         rst2.MoveLast
  21.         intRecords = rst2.RecordCount
  22.         rst2.MoveFirst
  23.     Else
  24.         intRecords = 0
  25.     End If
  26.     'Good practice to always double check for an empty recordset before we count the records!
  27.  
  28.     With rst1 
  29.         .Edit 
  30.         !TaskOrder = 1 
  31.         .Update 
  32.     End With 
  33.  
  34.     'This code updates all the other tasks from 2 to x
  35.     For intCounter = 1 to intRecords 
  36.         With rst2
  37.             .Edit 
  38.             !TaskOrder = intCounter + 1
  39.             .Update 
  40.             .MoveNext
  41.         End With 
  42.     Next intCounter
  43.  
  44.     rst1.Close 
  45.     rst2.Close 
  46.     db1.Close 
  47.     db2.Close 
  48.     Exit Function 
  49. EH: 
  50.     MsgBox Err.Number & " " & Err.Description 
  51.     Exit Function 
  52. End Function
  53.  
I hope these tweaks help you out.....

17 2047
Rabbit
12,516 Expert Mod 8TB
Just get the two unique ids of the rows you want to move and update them.
Aug 4 '12 #2
Seth Schrock
2,965 Expert 2GB
How do I get the PK of the other record? Getting the PK of the current record is easy. It is just the PK of the other record that I don't know how to do.
Aug 4 '12 #3
Rabbit
12,516 Expert Mod 8TB
That depends on data structure.
Aug 5 '12 #4
Seth Schrock
2,965 Expert 2GB
Here is the query that I have supporting the form:

Expand|Select|Wrap|Line Numbers
  1. SELECT TaskID, Task, TaskComplete, JobID, TaskOrder
  2. FROM tblTask
  3. ORDER BY TaskOrder
TaskID is the PK
Task is Text
TaskComplete is Yes/No
JobID is the Foreign Key
TaskOrder is a Number field and is what needs changed.

Is that all you need?
Aug 6 '12 #5
twinnyfo
3,653 Expert Mod 2GB
Seth,

How about code similar to this. This sends the current task order to the function and updates the table.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Function MoveUp(intCurrent As Integer)
  5. On Error GoTo EH
  6.     Dim db1 As Database
  7.     Dim rst1 As Recordset
  8.     Dim db2 As Database
  9.     Dim rst2 As Recordset
  10.     Dim strSQL As String
  11.     strSQL = "SELECT * FROM tblTask WHERE TaskOrder = " & intCurrent & ";"
  12.     Set db1 = CurrentDb()
  13.     Set rst1 = db1.OpenRecordset(strSQL, dbOpenDynaset)
  14.     strSQL = "SELECT * FROM tblTask WHERE TaskOrder = " & intCurrent - 1 & ";"
  15.     Set db2 = CurrentDb()
  16.     Set rst2 = db1.OpenRecordset(strSQL, dbOpenDynaset)
  17.     With rst1
  18.         .Edit
  19.         !TaskOrder = rst1!TaskOrder - 1
  20.         .Update
  21.     End With
  22.     With rst2
  23.         .Edit
  24.         !TaskOrder = rst2!TaskOrder + 1
  25.         .Update
  26.     End With
  27.     rst1.Close
  28.     rst2.Close
  29.     db1.Close
  30.     db2.Close
  31.     Exit Function 
  32. EH:
  33.     MsgBox Err.Number & " " & Err.Description
  34.     Exit Function 
  35. End Function 
  36.  
With only a few mods, you could move the task down the order or move it to the top.

Let me know if this helps......
Aug 6 '12 #6
Seth Schrock
2,965 Expert 2GB
BINGO! Now, what would be the "few mods" that would make it so that I can move to top? I think that I could make the mathamatical changes to switch the two records from the middle to the top and vice versa, but how to move, say number 6, to number 1, the previous number 1 to number 2, 2 to 3, 3 to 4, 4 to 5, and 5 to 6 is what I'm not sure of.
Aug 6 '12 #7
twinnyfo
3,653 Expert Mod 2GB
Seth,

Your db2/rst2 would include ALL records EXCEPT the target record. Then set your target record to 1, and then cycle through all the other records, setting their values to 2, 3, 4, etc.

Do the opposite for moving to the end (you need to do all the record counting in the code.)
Aug 6 '12 #8
Seth Schrock
2,965 Expert 2GB
Okay, here is that I tried out. It does work, but I want to make sure that I'm doing things properly.

Expand|Select|Wrap|Line Numbers
  1. Function MoveFirst(intCurrent As Integer)
  2. On Error GoTo EH
  3.     Dim db1 As Database
  4.     Dim rst1 As Recordset
  5.     Dim db2 As Database
  6.     Dim rst2 As Recordset
  7.     Dim strSQL As String
  8.     strSQL = "SELECT * FROM tblTask WHERE TaskOrder = " & intCurrent & ";"
  9.     Set db1 = CurrentDb()
  10.     Set rst1 = db1.OpenRecordset(strSQL, dbOpenDynaset)
  11.     strSQL = "SELECT * FROM tblTask WHERE TaskOrder <> " & intCurrent & " AND TaskOrder <" & intCurrent
  12.     Set db2 = CurrentDb()
  13.     Set rst2 = db1.OpenRecordset(strSQL, dbOpenDynaset)
  14.     rst2.MoveLast
  15.  
  16.     With rst1
  17.         .Edit
  18.         !TaskOrder = 1
  19.         .Update
  20.     End With
  21.  
  22.     Do While intCurrent <> 1
  23.         With rst2
  24.             .Edit
  25.             !TaskOrder = intCurrent
  26.             .Update
  27.             .MovePrevious
  28.         End With
  29.         intCurrent = intCurrent - 1
  30.     Loop
  31.  
  32.     rst1.Close
  33.     rst2.Close
  34.     db1.Close
  35.     db2.Close
  36.     Exit Function
  37. EH:
  38.     MsgBox Err.Number & " " & Err.Description
  39.     Exit Function
  40. End Function
  41.  
Also, is there a way to make it so that the record that I'm moving remains the one selected? Currently, in both the MoveUp and MoveFirst functions, if I select the fifth record when I click the button, the fifth record remains highlighted. I'm not sure if I need to make this a separate question or not.
Aug 6 '12 #9
Seth Schrock
2,965 Expert 2GB
Also, I just noticed that I will get an error if I click either the Move to First or Move Up buttons when the selected task is already = 1. I would assume that the reverse direction will have the same problem. I have solved the Move First and Move Up buttons by checking the value of intCurrent to see if it equals 1. If it does, then the function isn't run. My problem is, how do I check to see if it is the lowest order? Would I check for EOF or use a Do While Not is EOF loop?
Aug 6 '12 #10
twinnyfo
3,653 Expert Mod 2GB
Seth,

Just a few minor tweaks, to make it work properly.... and to maintain good coding practices....

Expand|Select|Wrap|Line Numbers
  1. Function MoveFirst(intCurrent As Integer) 
  2. On Error GoTo EH 
  3.     Dim db1 As Database 
  4.     Dim rst1 As Recordset 
  5.     Dim db2 As Database 
  6.     Dim rst2 As Recordset 
  7.     Dim strSQL As String 
  8.     Dim intRecords as Integer
  9.     Dim intCounter as Integer
  10.     strSQL = "SELECT * FROM tblTask WHERE TaskOrder = " & intCurrent & ";" 
  11.     Set db1 = CurrentDb() 
  12.     Set rst1 = db1.OpenRecordset(strSQL, dbOpenDynaset) 
  13.     strSQL = "SELECT * FROM tblTask WHERE TaskOrder <> " & intCurrent & " ORDER BY TaskOrder;"
  14.     'Need to sort the list by the current TaskOrder
  15.     'No need to have any other criteria
  16.     Set db2 = CurrentDb() 
  17.     Set rst2 = db2.OpenRecordset(strSQL, dbOpenDynaset)
  18.     'I had db1 in my original code..... (Sorry), although not sure it would matter....
  19.     If Not rst2.EOF Then
  20.         rst2.MoveLast
  21.         intRecords = rst2.RecordCount
  22.         rst2.MoveFirst
  23.     Else
  24.         intRecords = 0
  25.     End If
  26.     'Good practice to always double check for an empty recordset before we count the records!
  27.  
  28.     With rst1 
  29.         .Edit 
  30.         !TaskOrder = 1 
  31.         .Update 
  32.     End With 
  33.  
  34.     'This code updates all the other tasks from 2 to x
  35.     For intCounter = 1 to intRecords 
  36.         With rst2
  37.             .Edit 
  38.             !TaskOrder = intCounter + 1
  39.             .Update 
  40.             .MoveNext
  41.         End With 
  42.     Next intCounter
  43.  
  44.     rst1.Close 
  45.     rst2.Close 
  46.     db1.Close 
  47.     db2.Close 
  48.     Exit Function 
  49. EH: 
  50.     MsgBox Err.Number & " " & Err.Description 
  51.     Exit Function 
  52. End Function
  53.  
I hope these tweaks help you out.....
Aug 6 '12 #11
Seth Schrock
2,965 Expert 2GB
I think that I understand most of the tweaks, however I do have a question about the criteria in the second query. Lets say I have 10 tasks, and I'm moving task 5 to be number 1. If I don't have the TaskOrder < intCurrent criteria, then it would pull 1-4, 6-10 in that query. Wouldn't that cause a problem? I don't see anything in the code that would make it not touch 6-10. Am I missing something?
Aug 6 '12 #12
twinnyfo
3,653 Expert Mod 2GB
Concerning your previous post, I would use a the DMax function, to see the highest value of TaskOrder in the table. Then compare the values.

Concerning your follow-up question, remember if your current task is number 5, yes, it might make some sense to limit to only those records less than the current task. If you have a bunch of records, it will reduce the time it takes to iterate through those records. Wise choice and good catch! My code would be redundant and just make 6-10 equal to 6-10. I very gladly stand corrected!
Aug 6 '12 #13
twinnyfo
3,653 Expert Mod 2GB
In fact, that should be the ONLY criteria for the SQL statement:

Expand|Select|Wrap|Line Numbers
  1.     strSQL = "SELECT * FROM tblTask WHERE TaskOrder < " & intCurrent & ";"  
  2.  
Aug 6 '12 #14
Seth Schrock
2,965 Expert 2GB
Duh... At least I'm not the only one who didn't catch that the first time.

Also, on the DMax() function, does it return an integer or string? I have declared intLastNumber as integer and then did DMax for TaskOrder in rst2 and I'm getting an data type mismatch error on that line. Here is the code (line 18 is where the DMax() is):
Expand|Select|Wrap|Line Numbers
  1. Function MoveLast(intCurrent As Integer)
  2. On Error GoTo EH
  3.     Dim db1 As Database
  4.     Dim rst1 As Recordset
  5.     Dim db2 As Database
  6.     Dim rst2 As Recordset
  7.     Dim strSQL As String
  8.     Dim intRecords As Integer
  9.     Dim intCounter As Integer
  10.     Dim intLastNumber As Integer
  11.  
  12.     strSQL = "SELECT * FROM tblTask WHERE TaskOrder = " & intCurrent & ";"
  13.     Set db1 = CurrentDb()
  14.     Set rst1 = db1.OpenRecordset(strSQL, dbOpenDynaset)
  15.     strSQL = "SELECT * FROM tblTask WHERE TaskOrder >" & intCurrent & " ORDER BY TaskOrder"
  16.     Set db2 = CurrentDb()
  17.     Set rst2 = db2.OpenRecordset(strSQL, dbOpenDynaset)
  18.     intLastNumber = DMax("TaskOrder", rst2)
  19.  
  20.     If intCurrent <> intLastNumber Then
  21.         If Not rst2.EOF Then
  22.             rst2.MoveLast
  23.             intRecords = rst2.RecordCount
  24.             rst2.MoveFirst
  25.         Else
  26.             intRecords = 0
  27.         End If
  28.  
  29.         With rst1
  30.             .Edit
  31.             !TaskOrder = intRecords
  32.             .Update
  33.         End With
  34.  
  35.         For intCounter = intCurrent To intRecords
  36.             With rst2
  37.                 .Edit
  38.                 !TaskOrder = intCounter + 1
  39.                 .Update
  40.                 .MoveNext
  41.             End With
  42.  
  43.         Next intCounter
  44.     Else
  45.         MsgBox ("Task cannot be moved to a lower order")
  46.     End If
  47.  
  48.  
  49.     rst1.Close
  50.     rst2.Close
  51.     db1.Close
  52.     db2.Close
  53.     Exit Function
  54. EH:
  55.     MsgBox Err.Number & " " & Err.Description
  56.     Exit Function
  57. End Function
Aug 6 '12 #15
twinnyfo
3,653 Expert Mod 2GB
To use DMax(), use the following syntax:

Expand|Select|Wrap|Line Numbers
  1. intLastNumber = DMax("[TaskOrder]", "tblTask")
  2.  
It should only be looking for numeric values. Unless you expect hundreds of thousands of tasks, TaskOrder should have a data type of integer (not a long integer).
Aug 6 '12 #16
Seth Schrock
2,965 Expert 2GB
Okay, one last issue. When I click the button, it says "no current record", but it does work. Here is my code:
Expand|Select|Wrap|Line Numbers
  1. Function MoveLast(intCurrent As Integer, intJobID As Integer)
  2. On Error GoTo EH
  3.     Dim db1 As Database
  4.     Dim rst1 As Recordset
  5.     Dim db2 As Database
  6.     Dim rst2 As Recordset
  7.     Dim strSQL As String
  8.     Dim intRecords As Integer
  9.     Dim intCounter As Integer
  10.     Dim intLastNumber As Integer
  11.  
  12.     strSQL = "SELECT * FROM tblTask WHERE JobID = " & intJobID & "AND TaskOrder = " & intCurrent & ";"
  13.     Set db1 = CurrentDb()
  14.     Set rst1 = db1.OpenRecordset(strSQL, dbOpenDynaset)
  15.     strSQL = "SELECT * FROM tblTask WHERE JobID = " & intJobID & "AND TaskOrder >" & intCurrent & " ORDER BY TaskOrder"
  16.     Set db2 = CurrentDb()
  17.     Set rst2 = db2.OpenRecordset(strSQL, dbOpenDynaset)
  18.     intLastNumber = DMax("TaskOrder", "qryTaskReorder")
  19.  
  20.     If intCurrent <> intLastNumber Then
  21.         If Not rst2.EOF Then
  22.             rst2.MoveLast
  23.             intRecords = rst2.RecordCount
  24.             rst2.MoveFirst
  25.         Else
  26.             intRecords = 0
  27.         End If
  28.  
  29.         With rst1
  30.             .Edit
  31.             !TaskOrder = intLastNumber
  32.             .Update
  33.         End With
  34.  
  35.         For intCounter = intCurrent To intLastNumber
  36.             With rst2
  37.                 .Edit
  38.                 !TaskOrder = intCounter
  39.                 .Update
  40.                 .MoveNext
  41.             End With
  42.  
  43.         Next intCounter
  44.     Else
  45.         MsgBox ("Task cannot be moved to a lower order")
  46.     End If
  47.  
  48.  
  49.     rst1.Close
  50.     rst2.Close
  51.     db1.Close
  52.     db2.Close
  53.     Exit Function
  54. EH:
  55.     MsgBox Err.Number & " " & Err.Description
  56.     Exit Function
  57. End Function
  58.  
I think that it is because of line 40 trying to go to the next record the last time and there is no record to go to. The reorder works, I just need to figure out a way to keep it from going to the next record the last time. Would it work to surround line 40 with the following?
Expand|Select|Wrap|Line Numbers
  1. If Not rst2.EOF Then
  2. .MoveNext
  3. End If
Aug 6 '12 #17
Seth Schrock
2,965 Expert 2GB
Okay. I figured it out. I added the following line 34 of Post #17

Expand|Select|Wrap|Line Numbers
  1. intLastNumber = intLastNumber - 1
This caused the next loop to run one less time. Thanks Twinnyfo for all your help.
Aug 10 '12 #18

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

Similar topics

10
by: tgh003 | last post by:
So lets assume I have a list of tasks in db table (table looks like: ID, task, sort) And I want to reorder the task list without updating all the records in the table. I dont want to run a sql...
2
by: greenflame | last post by:
I am trying to reorder elements of a list and I am stuck as to what might be the best way to approach this. I have a (main) list of elements and another (ordering) list (which is may shorter, but...
3
by: kenia | last post by:
Hi everyone! I'm using the DOM standard to create a set of radio buttons, but I can't select any of them. It means that they appear in the page, but when I click on any of them nothing seems to...
0
by: yuven | last post by:
Hi i want the coding for reorder list using ajax asp.net . pls help me to flow my project
0
by: yuven | last post by:
hi <ajaxToolkit:ReorderList ID="ReorderList1" runat="server" DataSourceID="ObjectDataSource1" DragHandleAlignment="Left" ItemInsertLocation="Beginning" DataKeyField="ItemID"...
0
oll3i
by: oll3i | last post by:
import javax.swing.*; import java.awt.event.*; import java.util.ArrayList; import java.util.List; import java.util.concurrent.*; import java.lang.reflect.*; public class Exec1 extends JFrame...
0
by: shapper | last post by:
Hello, I have an enum as follows: Public Enum Feature Title Content Date Search End
12
by: Ashvini Shanbhag | last post by:
Hi, i m new in javascript. i have the below problem. Plz help. There are two main radio buttons and list of radio button under each one . On selecting of the first radio button a list of radio...
3
by: semomaniz | last post by:
I have a ajax reorderlist that displays the contents with out any problems but when i drag and reorder the list nothing happens, i get a postback and the same initial list is displayed, seems like my...
1
by: jpatchak | last post by:
Hello, I have an AJAX Reorder List control, which is working fine. I want to add an "Edit" hyperlink to the ItemTemplate (which I have done) that opens an AJAX Modal Window when it's clicked. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.