Results 1 to 12 of 12
  1. #1
    Meccer is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2011
    Posts
    17

    Search Command

    Hello,
    I want to open a Report (Contacts), with a search command I've got a textbox for the search keyword (txtSearch) and a dropdown to select the column in the database to search in (ddCategory).

    Here is my code, but nothing happens when I press the button;


    Code:
    Private Sub SearchContacts_Click()
     
     DoCmd.OpenReport "Contacts", acViewPreview, , "ddCategory like '*" & txtSearch & "*'"
    End Sub

  2. #2
    pmacbeth is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Apr 2011
    Posts
    3
    I would try

    DoCmd.OpenReport "Contacts", acViewPreview, , ddCategory & " like '*" & txtSearch & "*'"

  3. #3
    Meccer is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2011
    Posts
    17
    Thank you for the response,
    Unfortunately the button is still not doing anything, even if there is text or no text in the TextBox.

  4. #4
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    To make sure the button is connected to the code, try this:


    Code:
    Private Sub SearchContacts_Click()
     
       msgbox  "text search is" & Me.txtSearch
    
    End Sub
    If the message box is displayed, try:

    Code:
    Private Sub SearchContacts_Click()
    
       msgbox  "[ddCategory] like '*" & Me.txtSearch & "*'"
    
    End Sub
    Next, try:


    Code:
    Private Sub SearchContacts_Click()
    
        DoCmd.OpenReport "Contacts", acViewPreview
    
    End Sub
    Then try:

    Code:
    Private Sub SearchContacts_Click()
        dim strCriteria as string
    
        strCriteria =  "[ddCategory] like '*" & Me.txtSearch & "*'"
        DoCmd.OpenReport "Contacts", acViewPreview, , strCriteria
    
    End Sub

  5. #5
    pmacbeth is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Apr 2011
    Posts
    3
    I don't know why it's not working, but if you put the name of the dropdown between quotation marks as in

    strCriteria = "[ddCategory] like '*" & Me.txtSearch & "*"

    you don't get the name of the column in the string strCriteria, but the name of the dropdown...

    That should be

    strCriteria = Me.ddCategory & " like '*" & Me.txtSearch & "*"

  6. #6
    Meccer is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2011
    Posts
    17
    Ok it seems that the code is not connected to the button, Ssanfu I tried your first piece of code and still nothing.

    I've deleted everything and started over, and still I get nothing.

  7. #7
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You deleted the code, deleted the button, created a new button, renamed the button, clicked on the "On Click" property, then added the message box line of code?

    And still nothing????

  8. #8
    Meccer is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2011
    Posts
    17
    Sorry for the inconvienence, macro's was not enabled.

    Thanks for all the help

  9. #9
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    So you will mark the thread solved??

  10. #10
    Meccer is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2011
    Posts
    17
    One more question; I tested all of your code.

    The textbox (txtContacts) gives the correct answer.
    ddContacts is a dropdown with three values; name, company and job the user must choose one of these options (this is the field to search in).
    but on the message box I get ddContacts and not the value.

  11. #11
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by Meccer View Post
    One more question; I tested all of your code.

    The textbox (txtContacts) gives the correct answer.
    ddContacts is a dropdown with three values; name, company and job the user must choose one of these options (this is the field to search in).
    but on the message box I get ddContacts and not the value.

    What is the bound column of the comb box?? That is the value that the comb box returns unless you use the columns property.
    (What is the SQL of the combo box?)

  12. #12
    Meccer is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2011
    Posts
    17
    I've managed to figure it out, the problem was with the quotation marks in the search criteria; (how do a mark a post solved?)

    Dim CB As String
    Me.ddContacts.SetFocus
    CB = Me.ddContacts.Text

    strCriteria = CB & " like '*" & Me.txtContacts & "*'"
    DoCmd.OpenReport "Contacts", acViewReport, , strCriteria

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

Similar Threads

  1. Replies: 4
    Last Post: 03-17-2011, 06:17 AM
  2. Replies: 8
    Last Post: 02-19-2011, 05:48 PM
  3. Replies: 2
    Last Post: 08-31-2010, 08:57 PM
  4. Replies: 1
    Last Post: 07-27-2010, 02:27 PM
  5. Command Button
    By nashr1928 in forum Forms
    Replies: 1
    Last Post: 07-05-2010, 07:05 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