Results 1 to 10 of 10
  1. #1
    pjordan@drcog.org is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    Dec 2011
    Location
    Denver, CO
    Posts
    71

    Search Text Box for String

    Hi,

    I have a FORM linked to a single table. In many previous forms, I've been using this code described here http://www.dreamincode.net/forums/to...n-access-form/ To create a simple search text box for various numeric ID fields in tables.

    This time, however, I'm wanting to do the same thing, however in this database and Table, the field [Project_Name] I'm searching for is a string.

    I've been playing around with my code (based on that website) and just can't seem to get the string search to work.

    Code:
     
    Private Sub TXT_SEARCHBOX_PrjName_AfterUpdate()
      If IsNull(TXT_SEARCHBOX_PrjName) = False Then
              Me.Recordset.FindFirst "[Project_Name]=" & TXT_SEARCHBOX_PrjName
              Me!TXT_SEARCHBOX_PrjName = Null
              If Me.Recordset.NoMatch Then
                     MsgBox "Project Not Found", vbOKOnly + vbInformation, "Invalid Propject Name"
                            Me!TXT_SEARCHBOX_PrjName = Null
              End If
       End If
    End Sub
    From everything I've been reading and researching, I need to somehow put quotes around the TXT_SEARCHBOX_PrjName in the
    Code:
    Me.Recordset.FindFirst "[Project_Name]=" & TXT_SEARCHBOX_PrjName
    shown above.

    I'm not sure how to do that. I've tried the following all without success and with or without errors
    Code:
    Me.Recordset.FindFirst "[Project_Name]=" & "TXT_SEARCHBOX_PrjName"
    Me.Recordset.FindFirst "[Project_Name]=" & 'TXT_SEARCHBOX_PrjName'
    Me.Recordset.FindFirst "[Project_Name]=" & /'TXT_SEARCHBOX_PrjName/'
    Me.Recordset.FindFirst "[Project_Name]="" & TXT_SEARCHBOX_PrjName"
    Me.Recordset.FindFirst "[Project_Name]=" & '"TXT_SEARCHBOX_PrjName"'
    Me.Recordset.FindFirst "[Project_Name]=" & "'TXT_SEARCHBOX_PrjName'"
    and a few others I can't remember, but the main thing is when I use single quotes ' it tries of course to comment out the information after that, so not sure how to correctly use it if it does require single quotes.

    What is the proper way to turn this search into a string search?


    Secondly when that is solved, I have another field in the same table called [API_or_CC]. I would like the above search to only search for the [Project_Name] that has"API" associated with it (and not any that have 'CC​')

    Thanks

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    DONT use findfirst, instead FILTER the results. Then all records are the ones you want.

    Code:
    sub txtFind_Afterupdate()
    if isNull(txtFind) then
       me.filterOn = false
    
    else
       me.filter = "[field]='" & txtFind & "'"
       me.filterOn = true
    end if

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Me.Recordset.FindFirst "[Project_Name]='" & Me.TXT_SEARCHBOX_PrjName & "'"
    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.

  4. #4
    pjordan@drcog.org is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    Dec 2011
    Location
    Denver, CO
    Posts
    71
    Thanks for the help in how to do the quotes guys. I thought I had tried the '" & Me.TXT_SEARCHBOX_PrjName & "'"
    already, but guess it was just one variation I didn't. Worked great.

    And did try this method:


    Code:
    sub txtFind_Afterupdate()
    if isNull(txtFind) then
       me.filterOn = false
    
    else
       me.filter = "[field]='" & txtFind & "'"
       me.filterOn = true
    end if
    Though in my case we have other subsets of that project one still needs to see in the records and this only allows one to see just that one record without being able to jump to the next one.

    The next part is trying to add in a Where or maybe a strCriteria where [API_or_CC] = "API"?

    I tried
    Code:
    strCriteria = "[API_or_CC]= '" & API & "'"
    but that didn't work and did try a where statement too that was similar, but that didn't work either...

  5. #5
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    In your first code, add after the End If: Me.Requery

    For multiple variations use LIKE
    Me.Filter="Field Like '*" & me!txtfind & "*'"

  6. #6
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    strCriteria = "[API_or_CC]= '" & API & "'"

    is only the correct syntax if API_or_CC is defined as a String. If it's defined as a Number the syntax would be

    strCriteria = "[API_or_CC]= " & API

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  7. #7
    pjordan@drcog.org is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    Dec 2011
    Location
    Denver, CO
    Posts
    71
    Quote Originally Posted by Missinglinq View Post
    strCriteria = "[API_or_CC]= '" & API & "'"

    is only the correct syntax if API_or_CC is defined as a String. If it's defined as a Number the syntax would be

    strCriteria = "[API_or_CC]= " & API

    Linq ;0)>
    Yes the API_or_CC is a Short Text

    Here's what I have using the main code:

    Code:
    Private Sub TXT_SEARCHBOX_PrjName_AfterUpdate()
      If IsNull(TXT_SEARCHBOX_PrjName) = False Then
              strCriteria = "[API_or_CC]= '" & API & "'"
              Me.Recordset.FindFirst "[Project_Name]='" & Me.TXT_SEARCHBOX_PrjName & "'"
              Me!TXT_SEARCHBOX_PrjName = Null
              If Me.Recordset.NoMatch Then
                     MsgBox "Project Not Found", vbOKOnly + vbInformation, "Invalid Propject Name"
                            Me!TXT_SEARCHBOX_PrjName = Null
              End If
       End If
    
    
    End Sub
    It still doesn't search for Project_Names that only state the API_or_CC is = API

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    API is literal text? - it must be within the quote marks. You are not including the API criteria in the FindFirst.

    Me.Recordset.FindFirst "[Project_Name]='" & Me.TXT_SEARCHBOX_PrjName & "' AND [API_or_CC]='API'"

    Did you consider ranman's suggestion to use Filter and FilterOn? If there are multiple records that meet the criteria and you want to view them all together, use Filter.
    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
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725
    Give us some sample data to show
    a) some Project_names you have
    b) a sample search
    c) the result you want
    d) the result you actually get

  10. #10
    pjordan@drcog.org is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    Dec 2011
    Location
    Denver, CO
    Posts
    71
    Quote Originally Posted by June7 View Post
    API is literal text? - it must be within the quote marks. You are not including the API criteria in the FindFirst.

    Me.Recordset.FindFirst "[Project_Name]='" & Me.TXT_SEARCHBOX_PrjName & "' AND [API_or_CC]='API'"

    Did you consider ranman's suggestion to use Filter and FilterOn? If there are multiple records that meet the criteria and you want to view them all together, use Filter.
    Yes that was it thank you. I'm all set everyone, thanks again for all the help and helping new ways of doing things as well...

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 05-29-2015, 10:21 AM
  2. Replies: 16
    Last Post: 04-01-2014, 12:22 PM
  3. Query To Search Text String
    By JeffGeorge in forum Queries
    Replies: 2
    Last Post: 08-15-2013, 10:56 AM
  4. Replies: 11
    Last Post: 02-03-2012, 12:04 PM
  5. Replies: 1
    Last Post: 02-02-2012, 06:22 PM

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