PDA

View Full Version : Solved: Delete last character in field based on criteria



markatvic
11-08-2011, 06:16 AM
I've done some vb.net programming but am new to vba. I hate to ask to be spoon fed a solution, but I am under a time constraint. I essentially want to delete the last character in a field only if that last character is an "M", "T" or an "R". Would putting this statement

Left([fieldName], Len([fieldName]) -1)

inside a loop and conditional statement essentially do it? I am unsure of the vba syntax though. Thanks for any help.

orange
11-08-2011, 07:31 AM
Here is a short vba proc to do what you are asking.

Note: This will change the data in the table if that is what you want

Sub testNov8()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("YourTablename")
Do While Not rs.EOF ' start a loop
If Right(rs!FieldName, 1) = "M" Or _
Right(rs!FieldName, 1) = "R" Or _
Right(rs!FieldName, 1) = "T" Then
rs.Edit ' indicate you want to edit the record in the recordset
rs!FieldName = Left(rs!FieldName, Len(rs!FieldName) - 1)
rs.Update ' commit the update
End if
rs.MoveNext ' move to next record in rs
Loop

markatvic
11-08-2011, 08:57 AM
Orange, that worked perfectly. Thanks!