473,387 Members | 1,536 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,387 software developers and data experts.

Re numbering a field when changing another record

27
I have a simple data base with items that need to be worked on. Each record has a unique number assigned as a priority. The numbers are 1-100 for example, in the order that they are to be worked on. I want to be able to change record 50 form 50th priority to another priority (manualy) say to priority 5 and then change all records previously 5-49 to 6-50 priority and allows for the new priority 5. Any help on how to do this would be greatly appriceated. By the way I am using Access 2003.
Feb 26 '09 #1
6 1824
NeoPa
32,556 Expert Mod 16PB
So, am I right in thinking you have the priority order (rather than value) stored as a value in each record?

More questions.
Is there always a set number of records in the table?
Do you need to handle lowering the priority order value too?

This is working against the concept of databases but can be done. Don't expect something like this to be too straightforward though. Databases were never intended to be used this way.
Feb 26 '09 #2
Randoz
27
The priority number is based on how many records there are. It is a unique field and changes when ever another record is changed to a higher priority. The total number of records can increase or decrease depending on when jobs are completed or new ones are added. The Priority field must be able to be manualy changed for a record and then change all other priority fields in the other records so all jobs can be completed in order.
Feb 27 '09 #3
ADezii
8,834 Expert 8TB
@Randoz
If I understand you correctly, this code can act as a Template from which you can work. The manner in which I read the problem, the solution was not quite that obvious as my colleague NeoPa seems to indicate. The Algorithm deals with 100, Unique, Priorities ranging from 1 to 100. When you pass the Original Priority and the New, requested Priority, to the Function, it makes the appropriate Priority Shift (+1) within the Specified Range, then the Final Shift from Original to New via the retention of the original Primary Key Value. I'll post the code, but it is much simpler downloading the Attachment and visually observing the Priority Shifts within the Range. I've also included backup copies of the Main Table, since you will need the Original Values in the Table (tblItems) for each Test. Good Luck, and let us know how you make out.
Expand|Select|Wrap|Line Numbers
  1. Public Function fReassignPriorities(bytOriginalPriority As Byte, bytNewPriority As Byte) As Boolean
  2. On Error GoTo Err_fReassignPriorities
  3. Dim MyDB As DAO.Database
  4. Dim rstPriority As DAO.Recordset
  5. Dim lngPK As Long
  6.  
  7. fReassignPriorities = False         'Initialize to False
  8.  
  9. 'Obtain the Primary Key Value for the Record containing the Original Priority
  10. lngPK = DLookup("[Item_ID]", "tblItems", "[Priority] = " & bytOriginalPriority)
  11.  
  12. 'Priority Values must be >=1 and <=100
  13. If (bytOriginalPriority < 1 Or bytOriginalPriority > 100) Or (bytNewPriority < 1 Or bytNewPriority > 100) Then
  14.     Exit Function
  15. ElseIf bytOriginalPriority = bytNewPriority Then    'Priorities cannot be equal
  16.     Exit Function
  17. End If
  18.  
  19. 'If we get here, we have 2 Priority Values that are in the prioper
  20. 'Range (1 to 100) and not equal to one another
  21. If bytOriginalPriority > bytNewPriority Then    'Moving Priority UP (^)
  22.   Set MyDB = CurrentDb()
  23.   Set rstPriority = MyDB.OpenRecordset("tblItems", dbOpenDynaset)
  24.     With rstPriority
  25.       Do While Not .EOF
  26.         If ![Priority] >= bytNewPriority And ![Priority] < bytOriginalPriority Then
  27.           .Edit
  28.             ![Priority] = (![Priority] + 1)   'Shift Priorities within Range DOWN
  29.           .Update
  30.         End If
  31.         .MoveNext
  32.       Loop
  33.     End With
  34. Else
  35.   'something for you to do
  36. End If
  37.  
  38. rstPriority.Close
  39. Set rstPriority = Nothing
  40.  
  41. 'The Original Priority now becomes the New Priority, there are now 2
  42. 'Records with the Original Priority Value, because the Priority of
  43. 'the Prior Record was pushed up. That's why we need the lngPK value.
  44. CurrentDb.Execute "Update tblItems Set tblItems.[Priority] = " & bytNewPriority & _
  45.                   " Where tblItems.[Item_ID] = " & lngPK
  46.  
  47. fReassignPriorities = True
  48.  
  49. Exit_fReassignPriorities:
  50.   Exit Function
  51.  
  52. Err_fReassignPriorities:
  53.   MsgBox Err.Description, vbExclamation, "Error in fReassignPriorities()"
  54.   fReassignPriorities = False
  55.     Resume Exit_fReassignPriorities
  56. End Function
Expand|Select|Wrap|Line Numbers
  1. 'Move Priority 50 to 5, and re-adjust other Priorities
  2. If fReassignPriorities(50, 5) Then
  3.   MsgBox "Success"
  4. Else
  5.   MsgBox "False"
  6. End If
Feb 27 '09 #4
DonRayner
489 Expert 256MB
Here is a solution using SQL. It will handle changing the priority up or down and the marking of an item as complete.

TableName change to the name of the table containing your records
FieldName change to the name of the priority field in your table
ControlName change to the name of the control on your form
Completed a boolean (yes/no) field in your table and on your form.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Public intVar As Integer
  3.  
  4. Private Sub Form_BeforeUpdate(Cancel As Integer)
  5. If Me.NewRecord Then
  6.     Me.ControlName = DMax("[FieldName]", "TableName") + 1
  7.     Exit Sub
  8. End If
  9.  
  10. Dim stSQL
  11.  
  12. If Me.Completed Then
  13.     stSQL = "UPDATE TableName SET TableName.FieldName = [FieldName]-1" & _
  14.             " WHERE (((Table1.FieldName)>" & intVar & " And Table1.FieldName<>0));"
  15.     Me.ControlName = 0
  16. ElseIf intVar > Me.ControlName Then
  17.     stSQL = "UPDATE TableName SET TableName.FieldName = [FieldName]+1" & _
  18.             " WHERE (((TableName.FieldName)<" & intVar & " And (TableName.FieldName)>" & Me.ControlName - 1 & "));"
  19. ElseIf intVar < Me.Model Then
  20.     stSQL = "UPDATE TableName SET TableName.FieldName = [FieldName]-1" & _
  21.             " WHERE (((TableName.FieldName)>" & intVar & " And (TableName.FieldName)<" & Me.ControlName + 1 & "));"
  22. Else
  23.     Exit Sub
  24. End If
  25.     intVar = Me.ControlName
  26.     DoCmd.RunSQL stSQL
  27.     Me.ControlName = intVar
  28.  
  29. End Sub
  30.  
  31. Private Sub Form_Current() 
  32. If Me.NewRecord or Me.ControlName = 0 Then               
  33.     Me.ControlName.Locked = True
  34.     Me.Completed.Locked = true
  35.     Exit Sub
  36. Else
  37.     Me.ControlName.Locked = False
  38.     Me.Completed.Locked = False
  39. End If
  40. intVar = Me.ControlName
  41. End Sub
Feb 27 '09 #5
FishVal
2,653 Expert 2GB
@DonRayner
It's certainly a case when SQL is a preferable choice.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current() 
  2. If Me.NewRecord or Me.ControlName = 0 Then               
  3.     Me.ControlName.Locked = True
  4.     Me.Completed.Locked = true
  5.     Exit Sub
  6. Else
  7.     Me.ControlName.Locked = False
  8.     Me.Completed.Locked = False
  9. End If
  10. intVar = Me.ControlName
  11. End Sub
I would suggest a slightly more elegant coding.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2.  
  3. Dim blnLockState as Boolean
  4.  
  5. With Me
  6.     blnLockState = .NewRecord or .ControlName = 0
  7.     .ControlName.Locked = blnLockState
  8.     .Completed.Locked = blnLockState
  9.     intVar = .ControlName
  10. End With
  11.  
  12. End Sub
Feb 27 '09 #6
NeoPa
32,556 Expert Mod 16PB
Some SQL to move a record from position 50 to position 5 is relatively straightforward and can easily be built up and exectued from within the code of a form. It's not a concept that I'd recommend, but should you wish to proceed anyway, the SQL would be of the form :
Expand|Select|Wrap|Line Numbers
  1. UPDATE [YourTable]
  2. SET [Priority]=[Priority]+1
  3. WHERE [Priority] Between 5 And 49
After running this of course, would be the time to set the [Priority] value of the selected record to 5.

NB. The +1 on line #2 should be a -1 when moving a record to a lower (higher numbered) proiority. This should be handled in the code.
Feb 27 '09 #7

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

Similar topics

5
by: Grim Reaper | last post by:
I know this should be easy (I think), but I can not figure out how to do it. Basically, I have 4 tables (2 tables with 2 subordinate tables) that have an INQ_ID key that was created by using...
5
by: Charles McCaffery | last post by:
I have written a database with auto-numbering and now wish to remove alkl of my test data and set the auto-numbering back to one. How do I do this please? Charles McCaffery.
4
by: cliff williams | last post by:
how can I set a form so that when i open it , it appears with the next sequential number. I want to set the number pattern and not use autonumber
3
by: David B | last post by:
I am creating invoices for an app I am busy with. The transactions for the invoice come from 2 tables which store Sales and Facilities Hire. The current arrangement is that I create a temp...
2
by: Wayne Aprato | last post by:
I posted this yesterday and it seems like a moderator has thrown it in another thread. This is a totally different question to the one asked in that thread, so I'm posting it again. It is not a...
1
by: Sabine Oebbecke | last post by:
Hi there, Need some help again ... I have a continuous form which shows the attendees of a competition as per their placing in the competition. So, the first record is the winner, the second...
20
by: Prakash | last post by:
Hi ! I have a field "sub_tran_no" in my form in continuous view. When the user presses a button "Re-Number", I'd like to: 1) Save the current record pointer position 2) Save the current...
3
by: Chris | last post by:
Before I started to create table, etc to track unique form field record number assigments I thought I'd check to see if there is now a better way to do this in .NET. I have a parent form (table)...
4
by: buncey | last post by:
Hi Is there a method of changing the auto numbering sequence whilst retaining the exisiting records along with thier original ID numbers? What I am trying to say is, I have a table with records...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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,...

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.