I have been working with a database where to identify the category the organisation's members fall into they have been adding various characters to the end, some standard, some varying and completely indecipherable and for all intents useless, anyway I want to be able to use the info of what category to they are in for targeted marketing etc and so I built a module to move the last x number of characters defined by the user from box a to box b. Here it is:
It allows me to choose the character(s) to move one at a time and so avoid filling this new field with parts of the number and useless characters. So far as I can see it works pretty well, just wondering if there are any glaring problems someone could clue me in to before I run it on this database? Also, can one Exit Function if the InputBox's Cancel button is hit? Finally, I'm not all that clued up on the advanced features of sql but it seems to me that a query might have been able to do this just as well and maybe with a bit less code. Is that the case? Finally, think this is worth sending to the repository? Thanks for your comments.
Code:
Option Compare Database
Dim db As Recordset
Dim LastChr As String
Dim CardNo As String
Dim ChrNo As Integer
Dim Chr As String
Public Function MemberTypeFix()
Set db = CurrentDb.OpenRecordset("Members", dbOpenDynaset)
Call RemoveLastChrSpaces 'If the last Character is a space it throws off the function, this deletes them. Code below
TheStart:
Chr = InputBox("Enter characters exactly as they appear", "What character(s) do you want to move?")
ChrNo = Len(Chr)
[db].MoveFirst
Do While Not db.EOF
If Not IsNull(db![CARD NO]) Then
LastChr = Right(db![CARD NO], ChrNo)
If LastChr = Chr Then
CardNo = db![CARD NO]
CardNo = Left(CardNo, Len(CardNo) - ChrNo)
db.Edit
db![MemberType] = LastChr & db![MemberType]
db![CARD NO] = CardNo
db.Update
End If
End If
db.MoveNext
Loop
Call RemoveLastChrSpaces
If (MsgBox("Done! More suffixes to move?", vbYesNo) = vbYes) Then
GoTo TheStart
End If
End Function
Public Function RemoveLastChrSpaces()
Set db = CurrentDb.OpenRecordset("Members", dbOpenDynaset)
[db].MoveFirst
Do While Not db.EOF
If Not IsNull(db![CARD NO]) Then
LastChr = Right(db![CARD NO], 1)
If LastChr = " " Then
CardNo = db![CARD NO]
CardNo = Left(CardNo, Len(CardNo) - 1)
db.Edit
db![CARD NO] = CardNo
db.Update
End If
End If
db.MoveNext
Loop
End Function
It allows me to choose the character(s) to move one at a time and so avoid filling this new field with parts of the number and useless characters. So far as I can see it works pretty well, just wondering if there are any glaring problems someone could clue me in to before I run it on this database? Also, can one Exit Function if the InputBox's Cancel button is hit? Finally, I'm not all that clued up on the advanced features of sql but it seems to me that a query might have been able to do this just as well and maybe with a bit less code. Is that the case? Finally, think this is worth sending to the repository? Thanks for your comments.