Link to home
Start Free TrialLog in
Avatar of LISA GEORGE
LISA GEORGE

asked on

run time error 3157..update on linked table failed

I have continuous form with certain rows. as i want to sort ordering of rows i am using up and down buttons after update event
i have text box called sort which holds primary key field of back end sql table. when i click on up button its throwing error
run time error 3157update on a linked table woorkorderpayitem failed. i am using this vba code in then up button click event
Dim rst As Object
    Dim previousValue As Long
    Dim CurrentValue As Long
    Dim blnStop As Boolean
    If IsNull(Me.sort) Then Exit Sub

    Set rst = Me.RecordsetClone
    blnStop = False
    With rst
        .MoveFirst
        If Not .EOF And Not .BOF Then
            While Not .EOF And Not blnStop
                If .Fields("WorkOrderPayItemID") = Me.sort Then . (workorderpayitemID is primary key field and sort is name of text box holding that primary key)
                    CurrentValue = Me.sort
                    .Edit
                    .Fields("WorkOrderPayItemID") = previousValue
                    .Update . 'throwing error at this line
                    .MovePrevious
                    .Edit
                    .Fields("WorkOrderPayItemID") = CurrentValue
                    .Update
                    blnStop = True
                End If
                previousValue = .Fields("WorkOrderPayItemID")
                .MoveNext

            Wend

        End If
    End With
    With rst
End With
Set rst = Nothing
    Me.Requery
   End Sub

it throwing error at .update line
can some one help me with this vba code where i can move row up based on primary key field
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Out of 19 questions you accepted help on only 2 ...are you sure that is something that you cannot handle ?
The code you are using is from my contribution.....but i never saw any points from you....
Avatar of LISA GEORGE
LISA GEORGE

ASKER

@john yes i have used code which you posted but i need to make some changes for that code
as i need to use field with unique number
i tried vba code to get row number in different ways but none of that was as expected
so i am trying different approaches rather than sticking to one(now thought to use primary key field but as it's throwing errors i posted for help)
yes i posted few questions and as i have exaclty used same code in my forms i marked them as help
rest of the answers i got didn't suit my scenario so i went for different approach
thank you
For 50 points as author's marked help is far far way from accepting a solution ...given the fact that i have provided complete code along with sample Access application to understand why your implementation wasn't working...not to mention the PMs.
I am pointing out the whole situation because its doubtful that even if you get some assistance you will appreciate it...
All the experts here are pure volunteers and the only reward they get their knowledge/experience/effort is the rewarding of points...if you don't "see" that ...then .....i leave the rest to make their own remarks...
@john i really appreciate that you helped me with code
also if i didn't marked any question as solution that doesn't mean that i didn't appreciate their work
it's just didn't fit in my scenario and i find some other solution
in the above question also i just found my own solution but different approach
also i when i got my solution i really appreciated their help and gave points
and please stop pointing me on this
thank you
Why all this trouble? I provided you with validated, clever, and bug-less code and a full demo and a guide to implement it:

Continuous forms rows with up and down buttons for row ordering
@gustav thank you for your code
user wants that upanddown buttons for each row as my continuous form doesn't have too many rows(max 15 to 20)
and this continuous form is subsub form in mainform
so the code which John gave me suits but the main problem i had is to get row number in order to use that code
once again i appreciate your help and thank you for your time
But … if so, all you need is to move the two buttons to the detail section …

User generated image
as done in the attached and revised demo.
RowUpDownDemo2.accdb
oh that's  so kind of you
i will try this code and see how it goes
thank you gustav
@gustav
i noticed that priority field is number data type in your form
can you kindly explain me how you are getting that priority number incremented by 1 everytime?
Is preinserted/pre calculated in the table that the form uses.
like in my own example i posted in another question you made..
That happens here:

Private Sub ButtonDown_Click()

    PriorityAdd 1

End Sub

Private Sub ButtonUp_Click()

    PriorityAdd -1
    
End Sub

' <snip>

Private Sub PriorityAdd(ByVal Increment As Long)

    Dim CurrentPriority As Long
    Dim MaxPriority     As Long
        
    CurrentPriority = Me!Priority.Value
    MaxPriority = Me.RecordsetClone.RecordCount
    
    If Sgn(Increment) < 0 And CurrentPriority > 1 Or _
        Sgn(Increment) > 0 And CurrentPriority < MaxPriority Then
        
        Me!Priority.Value = CurrentPriority + Increment
        Me.Dirty = False
        RowPriority Me.Priority
    End If
    
End Sub

Open in new window

and then here (in function RowPriority):

    ' Rebuild priority list.
    Set Records = Form.RecordsetClone
    Records.MoveFirst
    While Not Records.EOF
        If Records.Fields(IdFieldName).Value <> RecordId Then
            NewPriority = NewPriority + 1
            If NewPriority = PriorityFix Then
                ' Move this record to next lower priority.
                NewPriority = NewPriority + 1
            End If
            If Nz(Records.Fields(FieldName).Value, 0) = NewPriority Then
                ' Priority hasn't changed for this record.
            Else
                ' Assign new priority.
                Records.Edit
                    Records.Fields(FieldName).Value = NewPriority
                Records.Update
            End If
        End If
        Records.MoveNext
    Wend

Open in new window

@john @gustav if that priority value is precalculated/preinserted  in table then i need to  write vba code to get that value on the fly for new records inserted
but if we can get that priority value from vba code which gutsav mentioned then it will be pretty easy for me to use that code
also FYI that priority value should be done by grouping in my form
i can post you picture of my form how it looks like so you can better have a view what i am talking about
appreciate your help on this..thank you guys
This is the easy part
On the Open event
Private Sub Form_Open(Cancel As Integer)
Dim rst As DAO.Recordset
Set rst = Me.RecordsetClone
counter = 1
With rst
While Not .EOF
.Edit
    .Fields("OrderingNo") = counter
.Update
.MoveNext
counter = counter + 1

Wend
End With
Me.Requery
End Sub

Open in new window

@john thanks

as i can't access my system now will try this tomorrow and see how it goes
I need to  write vba code to get that value on the fly for new records inserted

That - and also for deleting a record - is taken care of here (in the form) with one line of code:

Private Sub Form_AfterDelConfirm(Status As Integer)

    ' Rebuild priority of all records having lower priority (= higher numeric value).
    RowPriority Me.Priority

End Sub

Private Sub Form_AfterInsert()

    ' Rebuild priority of all records having lower priority (= higher numeric value).
    RowPriority Me.Priority
    
End Sub

Private Sub Priority_AfterUpdate()

    ' Rebuild priority of all records having lower priority (= higher numeric value).
    RowPriority Me.Priority
    
End Sub

Open in new window

The last piece allows the user to enter the priority directly, which is faster than clicking a button, say, twenty times.

Note please, that the code in the form is minimal as all the code not related to the form is moved to the separate module.
@gutsav i tried your code in my form but it's not displaying any priority number values in the text box

@john i also tried your code in open event of the form but still i didn't get the values

don't know reasons why all your codes are not working in my form

as i am already using john code for up and down buttons i better stick to that code as it's working fine for my form
but i got stuck in one issue
At first something i noticed late last night...somehow....while i was copying code to prepare the sample i send you ...it got pasted twice (?)
So replace this
  .Edit
 .Fields("WorkOrderPayItemID") = previousValue
 .Update . 'throwing error at this line
.MovePrevious
 .Edit
.Fields("WorkOrderPayItemID") = CurrentValue
                    .Update

Open in new window

with this
  .Edit
.Fields("WorkOrderPayItemID") = previousValue
 .Update . 'throwing error at this line
 .MovePrevious

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
it's not displaying any priority number values in the text box

It has to be bound to the priority field in the table.

But study my demo. It works. Then adjust your form accordingly.
@gustav i already did that but don't know why that happened

thank you for your time gutsav

@john i tried your code now and it's working fine as expected..thank you for your response

thanks a ton guys it's working as expected
here is the code which i used in  my form
Private Sub cmd_down_Click()

    Dim rst As Object
    Dim previousValue As Long
    Dim CurrentValue As Long
    Dim blnStop As Boolean
    On Error GoTo Err_Handler

    If IsNull(Me.RowNo) Then Exit Sub

    Set rst = Me.RecordsetClone
    blnStop = False
    With rst
        .MoveLast
        If Not .EOF And Not .BOF Then
            While Not .BOF And Not .EOF And Not blnStop
           
                If .Fields("RowNo") = Me.RowNo Then
                    CurrentValue = Me.RowNo
                    .Edit
         
                    .Fields("RowNo") = previousValue
                    .Update
                    .MoveNext
       
                    .Edit
                    .Fields("RowNo") = CurrentValue
                    .Update
                    blnStop = True
                End If
                previousValue = .Fields("RowNo")
                .MovePrevious

            Wend

        End If
    End With
    With rst

    End With
    Set rst = Nothing
    Me.Requery
ExitSub:
Exit Sub
Err_Handler:
Me.RowNo = CurrentValue
Resume ExitSub
End Sub

Private Sub cmd_up_Click()
    Dim rst As Object
    Dim previousValue As Long
    Dim CurrentValue As Long
    Dim blnStop As Boolean
    If IsNull(Me.RowNo) Or Me.RowNo = 0 Or Me.RowNo = 1 Then Exit Sub

    Set rst = Me.RecordsetClone
    blnStop = False
    With rst
        .MoveFirst
        If Not .EOF And Not .BOF Then
            While Not .EOF And Not blnStop
                If .Fields("RowNo") = Me.RowNo Then
                    CurrentValue = Me.RowNo
                    .Edit
                    .Fields("RowNo") = previousValue
                    .Update
                    .MovePrevious
                    .Edit
                    .Fields("RowNo") = CurrentValue
                    .Update
                    blnStop = True
                End If
                previousValue = .Fields("RowNo")
                .MoveNext

            Wend

        End If
    End With
    With rst

    End With
Set rst = Nothing
    Me.Requery
End Sub

Private Sub PayItemID_AfterUpdate()
    If IsNull(Me.RowNo) = True Then
        Me.RowNo.Value = Me.CurrentRecord
    End If
End Sub
but the thing is once i did reordering and reopen the form it's showing in the order which i modified earlier
but in database it's not saving in the order which i modified
still showing in same order how i intially added payitems in my form

as i want the report of the form so i need to save the order as well

it's not saving in the order  because i am using current record property to get RowNo value?
how can i save this in table in the same order which i modified?
you guys are awesome and had helped me a ton in resolving this issue.
thank you guys
hi i figured out how to display them in sorting order from report side
just used that rowno field as sorting for my report (though it saved in table in different order)
thank you