Connecting Tech Pros Worldwide Forums | Help | Site Map

Getting error on VBA routine

Member
 
Join Date: Dec 2006
Location: Oklahoma
Posts: 114
#1: Mar 3 '07
Hi to all. Trying to get a onclick VBA routine to run, but I'm getting an error message when I click on the button that is used to run the VBA code to change/update a memo field in a table. This routine is only called one time after the table is reloaded monthly. There is data in this memo field that needs to be converted, exam "<crlflf>" should be changed to "Chr(13) Chr(10) Chr(10)".

The error message I receive is "Update or CancelUpdate without AddNew or Edit". Hope someone can help me and thanks in advance.

Here is the code for the onclick:
================================================== ===
Expand|Select|Wrap|Line Numbers
  1. Private Sub FixMemoField_Click()
  2. On Error GoTo Err_FixMemoField_Click
  3.  
  4. Dim rs As DAO.Recordset
  5. Set rs = CurrentDb.OpenRecordset("Translated_memo")
  6.  
  7. While Not rs.EOF
  8. rs.Update
  9. rs!memofieldname = Replace(rs!trans_memo, "<crlf>", Chr(13) & Chr(10))
  10. rs!memofieldname = Replace(rs!trans_memo, "<crlflf>", Chr(13) & Chr(10) & Chr(10))
  11. rs.Update
  12. rs.MoveNext
  13. Wend
  14.  
  15. Exit_FixMemoField_Click:
  16.     Exit Sub
  17.  
  18. Err_FixMemoField_Click:
  19.     MsgBox Err.Description
  20.     Resume Exit_FixMemoField_Click
  21.  
  22. End Sub



Member
 
Join Date: Dec 2006
Location: Oklahoma
Posts: 114
#2: Mar 3 '07

re: Getting error on VBA routine


I took another look at the code and saw that "rs.Update" was used 2 times n the code. I deleted the 1st occurance of it and tried to run it.

I now get the following message "Item not found in this collection". Anyone have an idea of what is happening.
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,722
#3: Mar 4 '07

re: Getting error on VBA routine


Go back to the original version but replace the first rs.Update with an rs.Edit.
So, you should have :
Expand|Select|Wrap|Line Numbers
  1.   ...
  2.   rs.Edit
  3.   'Various lines of updating
  4.   rs.Update
  5.   rs.MoveNext
  6.   ...
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,214
#4: Mar 4 '07

re: Getting error on VBA routine


Quote:

Originally Posted by ljungers

Hi to all. Trying to get a onclick VBA routine to run, but I'm getting an error message when I click on the button that is used to run the VBA code to change/update a memo field in a table. This routine is only called one time after the table is reloaded monthly. There is data in this memo field that needs to be converted, exam "<crlflf>" should be changed to "Chr(13) Chr(10) Chr(10)".

The error message I receive is "Update or CancelUpdate without AddNew or Edit". Hope someone can help me and thanks in advance.

Here is the code for the onclick:
================================================== ===

Expand|Select|Wrap|Line Numbers
  1. Private Sub FixMemoField_Click()
  2. On Error GoTo Err_FixMemoField_Click
  3.  
  4. Dim rs As DAO.Recordset
  5. Set rs = CurrentDb.OpenRecordset("Translated_memo")
  6.  
  7. While Not rs.EOF
  8. rs.Update
  9. rs!memofieldname = Replace(rs!trans_memo, "<crlf>", Chr(13) & Chr(10))
  10. rs!memofieldname = Replace(rs!trans_memo, "<crlflf>", Chr(13) & Chr(10) & Chr(10))
  11. rs.Update
  12. rs.MoveNext
  13. Wend
  14.  
  15. Exit_FixMemoField_Click:
  16.     Exit Sub
  17.  
  18. Err_FixMemoField_Click:
  19.     MsgBox Err.Description
  20.     Resume Exit_FixMemoField_Click
  21.  
  22. End Sub

Expand|Select|Wrap|Line Numbers
  1. Dim rs As DAO.Recordset
  2. Set rs = CurrentDb.OpenRecordset("Translated_memo")
  3.  
  4. Do While Not rs.EOF
  5.    If Not IsNull(rs![memofieldname]) Then
  6.        rs.Edit
  7.           rs!memofieldname = Replace(rs!memofieldname, "<crlf>", Chr(13) & Chr(10))
  8.           rs!memofieldname = Replace(rs!memofieldname, "<crlflf>", Chr(13) & Chr(10) & Chr(10))
  9.        rs.Update
  10.    End If
  11.    rs.MoveNext
  12. Loop
  13.  
  14. rs.Close
Member
 
Join Date: Dec 2006
Location: Oklahoma
Posts: 114
#5: Mar 9 '07

re: Getting error on VBA routine


I corrected the script as shown in last post. I added a command button to my form so that when the ascii text file that is imported into Access each month, this routine can be run. When I click on it the disk drive light flickers and it looks as nothing is happening. I never get the msg box showing that it completed.

=========================================
Private Sub FixMemoField_Click()
On Error GoTo Err_FixMemoField_Click

Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("Translated_memo")

Do While Not rs.EOF
If Not IsNull(rs![trans_memo]) Then
rs.Edit
rs!trans_memo = Replace(rs!trans_memo, "<crlf>", Chr(13) & Chr(10))
rs!trans_memo = Replace(rs!trans_memo, "<crlflf>", Chr(13) & Chr(10) & Chr(10))
rs.Update
End If
rs.MoveNext
Wend

MsgBox ("The updates have completed!")

Exit_FixMemoField_Click:
Exit Sub

Err_FixMemoField_Click:
MsgBox Err.Description
Resume Exit_FixMemoField_Click

End Sub
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,214
#6: Mar 9 '07

re: Getting error on VBA routine


Quote:

Originally Posted by ljungers

I corrected the script as shown in last post. I added a command button to my form so that when the ascii text file that is imported into Access each month, this routine can be run. When I click on it the disk drive light flickers and it looks as nothing is happening. I never get the msg box showing that it completed.

=========================================
Private Sub FixMemoField_Click()
On Error GoTo Err_FixMemoField_Click

Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("Translated_memo")

Do While Not rs.EOF
If Not IsNull(rs![trans_memo]) Then
rs.Edit
rs!trans_memo = Replace(rs!trans_memo, "<crlf>", Chr(13) & Chr(10))
rs!trans_memo = Replace(rs!trans_memo, "<crlflf>", Chr(13) & Chr(10) & Chr(10))
rs.Update
End If
rs.MoveNext
Wend

MsgBox ("The updates have completed!")

Exit_FixMemoField_Click:
Exit Sub

Err_FixMemoField_Click:
MsgBox Err.Description
Resume Exit_FixMemoField_Click

End Sub

Replace Wend with Loop:
Expand|Select|Wrap|Line Numbers
  1. Do While Not rs.EOF
  2.     If Not IsNull(rs![trans_memo]) Then
  3.         rs.Edit
  4.             rs!trans_memo = Replace(rs!trans_memo, "<crlf>", Chr(13) & Chr(10))
  5.             rs!trans_memo = Replace(rs!trans_memo, "<crlflf>", Chr(13) & Chr(10) & Chr(10))
  6.         rs.Update
  7.     End If
  8.     rs.MoveNext
  9. Wend <== DO NOT USE!
  10. Loop
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,722
#7: Mar 9 '07

re: Getting error on VBA routine


If you want to use Wend then it comes in the format below.
Expand|Select|Wrap|Line Numbers
  1. While {condition}
  2.   ...
  3. Wend
Otherwise, ADezii's Do...Loop is a perfectly adequate alternative.
The Do...Loop format is actually more flexible as you can use 'While' OR 'Until', and you can either put them with the 'Do' or with the 'Loop'.
Member
 
Join Date: Dec 2006
Location: Oklahoma
Posts: 114
#8: Mar 11 '07

re: Getting error on VBA routine


I forgot to thank you ADezii and NeoPa for your help. That VBA routine works now and performs the way it should.

Thanks to both of you.
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,214
#9: Mar 11 '07

re: Getting error on VBA routine


Quote:

Originally Posted by ljungers

I forgot to thank you ADezii and NeoPa for your help. That VBA routine works now and performs the way it should.

Thanks to both of you.

Glad to help ya.
Reply