apostrophe in textbox errors

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
 
J

John Vinson

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]
 
D

Douglas J. Steele

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, """", """""") & """"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Runtime Error '2448': 4
'Runtime Error 2448' 2
Runtime error 2001 2
Creating a filter using VBA 5
Run-time error '2448' 5
Option group to filter a form 5
Error on modified Allenbrowne SearchForm 6
Search Problem 9

Top