Results 1 to 7 of 7
  1. #1
    Perceptus's Avatar
    Perceptus is offline Expert
    Windows 7 64bit Access 2003
    Join Date
    Nov 2012
    Location
    Knoxville, Tennessee
    Posts
    659

    Lightbulb Using a Priority and updating other records

    Not sure if this belonged in the Query or Module or Programming forum.


    I have a table (tblPriorityTest) with 3 fields. ID, Description, Priority. Priorities are to range between 1-9000.

    I have a form where I have each field named to the field it represents.

    In the before update event i have.


    Code:
    Private Sub Priority_BeforeUpdate(Cancel As Integer)
    PriorityUpdate Me.Priority
    End Sub
    And the Priority Update Code.
    Code:
    Private Function PriorityUpdate(ByVal tbPriority As TextBox)
        Dim oldValue As Integer: oldValue = tbPriority.oldValue
        Dim newValue As Integer: newValue = tbPriority
        Dim SQL As String
        'Dim intChange As Integer: intChange = 0
        If oldValue = 0 Or newValue = 0 Or oldvale = newValue Then Exit Function
        
        If newValue < oldValue Then
            intChange = oldValue - newValue
            SQL = "Update tblPriorityTest Set [Priority]=[Priority]-" & IIf(intChange <= 0, Abs(intChange), intChange) & " Where [Priority]<" & oldValue
            SQL = SQL & " and ID <>" & id & ""
            SQL = SQL & " and [Priority]<9000"
                  
        ElseIf newValue > oldValue Then
    
    
            intChange = newValue - oldValue
            SQL = "Update tblPriorityTest Set [Priority]=[Priority]+" & intChange & " Where [Priority]>" & oldValue
            SQL = SQL & " and ID <>" & id & ""
            SQL = SQL & " and [Priority]<9000"
        End If
        
        Debug.Print SQL
        
        CurrentDb.Execute SQL
        
        'intChange = CurrentDb.RecordsAffected
    End Function
    my data look like

    id: Description: Priority:
    1 1 1
    2 2 2
    3 3 3
    4 4 4
    5 5 5
    6 6 6
    7 7 7
    When I change the priority of ID#5 to a 7. I want all the numbers after it to be changed.

    It sorta works. sorta not. Am I doing this the wrong way? Suggestions.

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    in other words when you change item 5's priority to 7 you want ID 6 priority to become 5 and id 7's priority to become 6?

    What exactly is your code doing that 'sorta works, sorta not'.

  3. #3
    Perceptus's Avatar
    Perceptus is offline Expert
    Windows 7 64bit Access 2003
    Join Date
    Nov 2012
    Location
    Knoxville, Tennessee
    Posts
    659
    Changing a lower number to a higher number works. but changing a higher number to a lower number doesn't.

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    try this:

    Code:
    If newValue < oldValue Then         
        intChange = oldValue - newValue         
        SQL = "Update tblPriorityTest Set [Priority]=[Priority]-" & IIf(intChange <= 0, Abs(intChange), intChange) & " Where [Priority]<=" & oldValue

  5. #5
    Perceptus's Avatar
    Perceptus is offline Expert
    Windows 7 64bit Access 2003
    Join Date
    Nov 2012
    Location
    Knoxville, Tennessee
    Posts
    659
    This still produced a negative number.

    I think the error in my ways is the Inline IF statement. I forgot that subtracting a negative is the same as adding a positive

  6. #6
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Where are you on this, did you solve the problem?

    If not could you supply a sample database so I don't have to re-write something.

  7. #7
    Perceptus's Avatar
    Perceptus is offline Expert
    Windows 7 64bit Access 2003
    Join Date
    Nov 2012
    Location
    Knoxville, Tennessee
    Posts
    659
    I only get to work on this problem every other tuesday and thursday. Its still on my list. In my next post i will provide a sample.

    Thank you for your patience. Hope you had a good 4th.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Updating adding records by using email
    By Andyjones in forum Access
    Replies: 1
    Last Post: 01-29-2013, 07:19 AM
  2. Updating Multiple Records at Once Using a Form?
    By CaffeinatedOfficeWorker in forum Forms
    Replies: 5
    Last Post: 07-22-2012, 11:39 AM
  3. Updating records using visual basic
    By kmw in forum Programming
    Replies: 11
    Last Post: 05-11-2012, 12:55 PM
  4. Updating multiple records
    By Meg in forum Access
    Replies: 1
    Last Post: 01-09-2012, 11:12 AM
  5. HELP for updating selected records
    By explorer19 in forum Programming
    Replies: 7
    Last Post: 06-01-2011, 01:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums