Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496

    Getting error when using '

    I have unbound text boxes within a search form

    I use

    Code:
    Me.frmSchoolSearchSub1.Form.Filter = "[NewSchoolsID] Like '*" & Me.txtIDnumber & "*'" & IIf(Not IsNull(Me.cmbState), " and [StateID] =" & Me.cmbState, Null) & " and [SchoolName] Like '*" & Me.txtSchoolS & "*'" & " and [SchoolSuburb] Like '*" & Me.txtSchoolSub & "*'" & "and [SchoolPostCode] Like '*" & Me.txtFilterSchoolPostCode & "*'" & " and [SchoolPhone] Like '*" & Me.txtPhone & "*'" & " and IsNull([Removed])"
    however if someone uses ' character say like st paul's then I get an error because I use Like '*" in my code - the ' interferes



    How do I get around this?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Need to double up the ' mark.

    I use:

    Replace([fieldname], "'", "''")

    You will probably have to do that on both parts.

    Replace([SchoolName], "'", "''") Like '*" & Replace(Me.txtSchoolS, "'", "''") &
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by June7 View Post
    Need to double up the ' mark.

    I use:

    Replace([fieldname], "'", "''")

    You will probably have to do that on both parts.

    Replace([SchoolName], "'", "''") Like '*" & Replace(Me.txtSchoolS, "'", "''") &
    wait - if I am filtering I need to replace? I don't wish to replace anything.

    The form simply searches the database

    Then occasionally when searching through the continuous form they may use an apostrophe - because of this I get an error.... in the database some names have an apostrophe as they are schools i.e. st paul's catholic.

  4. #4
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Click image for larger version. 

Name:	Capture.JPG 
Views:	19 
Size:	25.8 KB 
ID:	15177

    ...The error I want to avoid

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I see a wildcard in your future. Maybe make a rule to not allow special characters and provide instruction for the user.. how to search without apostrophe. Not imagining a way to analyze the field for apostrophe right now.

  6. #6
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by ItsMe View Post
    I see a wildcard in your future. Maybe make a rule to not allow special characters and provide instruction for the user.. how to search without apostrophe. Not imagining a way to analyze the field for apostrophe right now.
    Yeah I was hoping I wouldn't have to but it seems that way... I really need to think this one over as the company really needs the apostrophe and I like my search form

    I want my cake, and stuff my face with it too

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Maybe try the instr() function and shove the control directly in the middle of it, see if it breaks

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    I tried to enforce a rule also but apostrophes just won't go away. Between contractions, possessives, foot symbol, I have to deal with in data. It doesn't seem to be a problem in Access query objects but building an SQL in VBA is different matter.

    It doesn't change the data in table, just manipulates the value in the VBA sql so that the apostrophe will be accepted as a solo literal character.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Ruegen, I thought you already tried June's sample. Your answer is right there in post #2.

  10. #10
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    ohhhh I was worried it would replace the data and remove the apostrophe - I will give it a go thanks

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    No, your sql is just setting filter criteria, not editing data.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    One other solution is to use double-quotes as your string delimiers, instead of single-quotes:

    Me.frmSchoolSearchSub1.Form.Filter = "[NewSchoolsID] Like ""*" & Me.txtIDnumber & "*""" & IIf(Not IsNull(Me.cmbState), " and [StateID] =" & Me.cmbState, Null) & " and [SchoolName] Like ""*" & Me.txtSchoolS & "*""" & " and [SchoolSuburb] Like ""*" & Me.txtSchoolSub & "*""" & "and [SchoolPostCode] Like ""*" & Me.txtFilterSchoolPostCode & "*""" & " and [SchoolPhone] Like ""*" & Me.txtPhone & "*""" & " and IsNull([Removed])"

    Of course, that means now your users can't enter double-quotes in their search criteria..... you can't have it both ways, unforunately.

    HTH

    John

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Ah ha ! Always wondered if there was situation that justified use of double quote over apostrophe. Thank you John.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  14. #14
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Is there a way to prevent users from adding a double quote? So they can't use it when searching - message box return or something?

    I imagine this sort of thing happens in hacking websites - using an apostrophe at the end of a string in a field box.... then writing code after it, concatenating etc..

  15. #15
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Why prevent it? If the data in table has the character, users should be able to type it as part of the value to be matched.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 01-23-2014, 07:49 AM
  2. Replies: 3
    Last Post: 03-05-2013, 11:17 AM
  3. Replies: 0
    Last Post: 07-16-2012, 05:42 AM
  4. Replies: 6
    Last Post: 05-30-2012, 12:32 PM
  5. Replies: 1
    Last Post: 05-11-2012, 10:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums