Open Form using Multiple Criteria

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • remas
    New Member
    • Feb 2014
    • 6

    Open Form using Multiple Criteria

    i have constructed database to display specific records according to user choice from multiple comboboxes based on one taBLE but tried with many ways and give up
    can any one help me to correct the following code:
    Code:
    Private Sub search_Click()
    On Error GoTo Err_search_Click
    
        Dim stDocName As String
        Dim stLinkCriteria As String
        
         Dim stLinkCriteria1 As String
          Dim stLinkCriteria2 As String
           Dim stLinkCriteria3 As String
        
        stLinkCriteria1 = " central =  " & Me.comcentral & ""
        stLinkCriteria2 = " AND from = '  " & "'" & Me![comfrom] & "'"
        stLinkCriteria3 = " AND cto = '  " & "'" & Me![comto] & "'"
       stLinkCriteria = stLinkCriteria1 & stLinkCriteria2
       stLinkCriteria = stLinkCriteria & stLinkCriteria3
       
      MsgBox (stLinkCriteria)
    
        stDocName = "kpi"
        DoCmd.OpenForm stDocName, , , stLinkCriteria
    
    Exit_search_Click:
        Exit Sub
    
    Err_search_Click:
        MsgBox Err.Description
        Resume Exit_search_Click
        
    End Sub
    i tried to filter the form too but it doesn't work
    plz help
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1293

    #2
    remas,
    Welcome to Bytes. You'll find a lot of people here willing and able to help, but you have to do your best to explain your problem.

    You are not telling us what is happening. "It doesn't work" is not really much to go on.

    That being said, I suspect that 'from' and 'to' indicate you really want to test for a range and not equality. from= and cto= is a little curious to me. I would expect a from / to pair to use a BETWEEN verb in the criteria. Something like
    Code:
    stLinkCriteria3 = " AND cto BETWEEN '" & Me![comfrom] & "' AND " '"  & Me![comto] & "'"
    Also, using code tags is both helpful and required on the site. Click on the [CODE] tag and then type your lines of code between the tags that appear in your post.

    Jim

    Comment

    • remas
      New Member
      • Feb 2014
      • 6

      #3
      thank you jim for replying
      i'm sorry if i wasn't so clear but this is the first time i post a question
      the data base has one table with columns "from as begining date , to as ending date, central , name and code" user is supposed to choose to search about one of the columns (code, name or central" and choose the beginning and the end of search date "they are different columns so i think i can't use between"
      the problem is the form opened empty
      thanks again

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Assuming neither of the three Fields can be NULL, the Criteria String should look something like the following. I am still a little confused on what you are looking for, so the below illustration is just a generalization:
        Code:
        Dim strLinkCriteria As String
        
        stLinkCriteria = "[central] = '" & Me.[comcentral] & "' AND [Date Field] BETWEEN #" & _
                          Me![comfrom] & "# AND #" & Me![comto] & "#"
           
        DoCmd.OpenForm stDocName, , , stLinkCriteria

        Comment

        • jimatqsi
          Moderator Top Contributor
          • Oct 2006
          • 1293

          #5
          remas,
          What might be very helpful to you is to make a query with the Access query editor that returns a sample of data like you want. Just hard code the criteria, for the sake of the example, by keying typical values into the criteria line of the query editor.

          Once you have that, you can actually see the SQL code that results from your query. Right-click on the upper panel of the query editor and choose the SQL view. Look at what follows the Where verb in the SQL. That right there is what you want your criteria to look like (without the 'where' verb)

          This will teach you a whole lot of useful Access stuff. Stick with til you get it. We're here to help when you get stuck.

          Jim
          Last edited by jimatqsi; Feb 22 '14, 07:00 PM. Reason: spelling

          Comment

          • remas
            New Member
            • Feb 2014
            • 6

            #6
            thank you all
            the data base is a performance report for a company , the user should be able to search the required field either the code of employee , his name or the central name but for any field he is supposed to set the from date and to date to start the search "from and to are 2 different columns" and the resulting form should display all records that satisfy the 3 chosen conditions "from, to and one of the other fields"
            when i tried the code the stLinkCriteria value is "false" why is that?
            thank you again

            Comment

            • remas
              New Member
              • Feb 2014
              • 6

              #7
              thank you all again i did it with a query then copy and paste the code from sql view
              Last edited by remas; Feb 25 '14, 11:30 PM.

              Comment

              Working...