Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 3rd, 2004, 08:29 PM
Authorized User
 
Join Date: Jul 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default VBA Search

I hope someone can help.

I have a combobox on a form searching for a last name in an underlying table. Initially I would get an error if anyones name had ' in it. From the access forum I was given a funtion to resolve this problem. I no longer get error reports, but it will now not find anyone with ' just gives some random name it finds.

I would appreciate any help.

Stephen




Private Sub Combo332_AfterUpdate()
    ' Find the record that matches the control.
    Dim rs As Object

    Set rs = Me.Recordset.Clone
    rs.FindFirst "[Last Name] = '" & filterStr(Me![Combo332]) & "'"
    If Not rs.EOF Then Me.Bookmark = rs.Bookmark
End Sub

Private Function filterStr(ByVal str As String) As String
    Dim i As Integer
    Dim temp_str As String

    temp_str = ""
    For i = 1 To Len(str)
        If Mid(str, i, 1) = "'" Then
            temp_str = temp_str & "''"
        Else
            temp_str = temp_str & Mid(str,

i, 1)
        End If
    Next
    filterStr = temp_str
End Function

 
Old June 17th, 2004, 01:01 AM
Authorized User
 
Join Date: Oct 2003
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Stephen, simply replace single quotes ', with doubles "", keeping the doubles you already had.
Original; rs.FindFirst "[Last Name] = '" & filterStr(Me![Combo332]) & "'"
To This; rs.FindFirst "[Last Name] = """ & filterStr(Me![Combo332]) & """"
Always do this for string searches, if you anticipate there may be apostrophe's in the string, or even quotes.
The filterStr function's an "interesting" idea, but not necessary.
Double quotes, wherever you normally would put single, while keeping the obligatory doubles.
Hope this helps, good luck!


 
Old June 22nd, 2004, 07:13 PM
Authorized User
 
Join Date: Jul 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi DBoulos,

Thanks for the tip, tried it and I like it!

Thanks again,
Stephen

 
Old June 23rd, 2004, 05:56 PM
Authorized User
 
Join Date: Oct 2003
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Your welcome, glad to help!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Search button doesn't search Access DB cbones Visual Studio 2008 1 October 27th, 2008 07:36 PM
VBA code to Search All Text Fields on a form dezmond2 BOOK: Access 2007 VBA Programmer's Reference ISBN: 978-0-470-04703-3 1 July 7th, 2007 11:58 AM
Search/Update in multiple worksheets VBA code help utalwalk Excel VBA 1 April 19th, 2006 04:42 AM
VBA Script to search fields and display contents Jwill1014 Access VBA 8 June 2nd, 2005 07:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.