apostrophe in textbox errors

  • Thread starter Thread starter RipperT
  • Start date Start date
R

RipperT

An unbound cboBox has this After_Update code behind it.

Private Sub cboNameLookup_AfterUpdate()
Me.Filter = "LstName = '" & Me.cboNameLookup & "'"
Me.FilterOn = True
If Me.RecordsetClone.RecordCount = 0 Then
Me.FilterOn = False
MsgBox " Name not found."
End If
End Sub

If the user types an apostrophe into it, I get the following runtime error:

Run time error '2448':
You can't assign a value to this object.

What can I do to prevent this?

Many thanx, Rip
 
An unbound cboBox has this After_Update code behind it.

Private Sub cboNameLookup_AfterUpdate()
Me.Filter = "LstName = '" & Me.cboNameLookup & "'"
Me.FilterOn = True
If Me.RecordsetClone.RecordCount = 0 Then
Me.FilterOn = False
MsgBox " Name not found."
End If
End Sub

If the user types an apostrophe into it, I get the following runtime error:

Run time error '2448':
You can't assign a value to this object.

What can I do to prevent this?

Delimit the filter with " rather than ': to do so, double up the
doublequote:

Me.Filter = "LstName = """ & Me.cboNameLookup & """"


John W. Vinson[MVP]
 
John Vinson said:
Delimit the filter with " rather than ': to do so, double up the
doublequote:

Me.Filter = "LstName = """ & Me.cboNameLookup & """"

And in the event that you may have a mix of both single and double quotes in
the text (such as John's "House Of Access Expertise"), pick one of the two
delimiters to use, and use the Replace function to double up on that
delimiter.

Me.Filter = "LstName = '" & Replace(Me.cboNameLookup, "'", "''") & "'"

or

Me.Filter = "LstName = """ & Replace(Me.cboNameLookup, """", """""") & """"
 
Back
Top