Question : Delete / Replace Apostrophes on Data Entry

This should be an easy one, but it's *urgent* and I can't figure it out. I have a problem when I enter new data that includes an apostrophe (').

If, for example I enter this into a text box:

      Frederick's

Then I get the following VB Error:

     Error 3075: Syntax Error (missing operator) in query expression 'companyname = 'Frederick's"

I kind of understand why it's happening and as a quick resolution I just want to stop any apostophe's entering my data. So, I can either:

1: Allow users to enter it, but replace it with a space or delete it when it is updated, or
2: Pop up an "apostrophe's not allowed" kind of msgbox when someone attempts to enter the offending character.

I've tried the REPLACE method - see below.

I have had a quick look for a solution, but nothing seems to work. Here is my BEFOREUPDATE code:

txtCust = Replace(companyname, "'", "")
Dim sID As String
sID = Nz(companyname.Value, "")
If sID <> "" Then
    If Nz(DLookup("companyname", "tblsubconsultant", "companyname = '" & sID & "'"), "") <> "" Then
       MsgBox "This Sub Consultant already exists in the database, you may not add it again!"
       Me.undo
    Else
        'MsgBox "No Dup"
    End If
End If


Also, will I really have to enter this kind of code into EVERY box where potentially an apostrophe may be entered? There could be as many as 30 or 40 seperate places to do it!

Answer : Delete / Replace Apostrophes on Data Entry

try this change, which uses the Replace function to replace ALL occurences if a single '  with '' (that is TWO ', not a ")

txtCust = Replace(companyname, "'", "")
Dim sID As String
sID = Replace(Nz(companyname.Value, ""),"'", "''")
If sID <> "" Then
    If Nz(DLookup("companyname", "tblsubconsultant", "companyname = '" & sID & "'"), "") <> "" Then
       MsgBox "This Sub Consultant already exists in the database, you may not add it again!"
       Me.undo
    Else
        'MsgBox "No Dup"
    End If
End If


Access sees '' (two 's right after one another) as an ESCAPE sequence, indicating that a ' should be stored, and searched for.

AW
Random Solutions  
 
programming4us programming4us