Results 1 to 11 of 11
  1. #1
    mindbender is offline Competent Performer
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    283

    1 or the other parameters

    I currently have a query that when executed asks for a product code- UCase$([Enter Product Code])



    I have a separate query that when executed asks for a carrier #- UCase$([Enter Carrier ID])

    Is there a way to create this within 1 query so they can either enter a Product Code OR A Carrier# or BOTH if they have both?

    Thanks in advance for your thoughts

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    no.
    use a form, put 2 text boxes, txtCode, txtCarrier
    then use vb code to open the query you want

    the queries do not have the param , instead it looks at the form textbox...
    qsFindCode= select * from table where [ProductCode]=forms!myForm!txtCode


    Code:
    sub btnOpen_click()
    
    select case true
        case isNull(me.txtCode) and not isNull(me.txtCarrier)
              docmd.openquery "qsFindCarrier"
    
        case not isNull(me.txtCode) and isNull(me.txtCarrier)
              docmd.openquery "qsFindCode"
    
        case not isNull(me.txtCode) and not isNull(me.txtCarrier)
              docmd.openquery "qsFindBoth"
    end select
    end sub

  3. #3
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    yes - depends on the output you are expecting (you haven't said what that is) something like

    SELECT *
    FROM myTable
    WHERE ProductCode=[Enter Product Code] OR Carrier=[Enter Carrier ID]

    Note you do not need UCase - Access is not case sensitive. And if you do decide you need it, drop the $ - the code will fail if you do not enter anything

  5. #5
    mindbender is offline Competent Performer
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    283
    Quote Originally Posted by ranman256 View Post
    no.
    use a form, put 2 text boxes, txtCode, txtCarrier
    then use vb code to open the query you want

    the queries do not have the param , instead it looks at the form textbox...
    qsFindCode= select * from table where [ProductCode]=forms!myForm!txtCode


    Code:
    sub btnOpen_click()
    
    select case true
        case isNull(me.txtCode) and not isNull(me.txtCarrier)
              docmd.openquery "qsFindCarrier"
    
        case not isNull(me.txtCode) and isNull(me.txtCarrier)
              docmd.openquery "qsFindCode"
    
        case not isNull(me.txtCode) and not isNull(me.txtCarrier)
              docmd.openquery "qsFindBoth"
    end select
    end sub

    Right now it is a form that is opened and the form is populated by the query. So for this method They would get to the switchboard and see 2 fields. They would enter 1 or the other or both....is there a way to open the form results using the vba?

  6. #6
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    the form will show ALL records, then filter the list when the user enters text and clicks the Filter button:


    '----------------
    sub btnFilter_click()
    '----------------
    dim sWhere as string

    sWhere = "1=1"
    if not IsNUll(me.txtCode) then sWhere = sWhere & " and [Code]='" & me.txtCode & "'"
    if not IsNUll(me.txtCarrier) then sWhere = sWhere & " and [Carrier]='" & me.txtCarrier & "'"

    If sWhere = "1=1" Then
    Me.FilterOn = False
    Else
    Me.Filter = sWhere
    Me.FilterOn = True
    End If
    end sub

  7. #7
    mindbender is offline Competent Performer
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    283
    Quote Originally Posted by ranman256 View Post
    the form will show ALL records, then filter the list when the user enters text and clicks the Filter button:


    '----------------
    sub btnFilter_click()
    '----------------
    dim sWhere as string

    sWhere = "1=1"
    if not IsNUll(me.txtCode) then sWhere = sWhere & " and [Code]='" & me.txtCode & "'"
    if not IsNUll(me.txtCarrier) then sWhere = sWhere & " and [Carrier]='" & me.txtCarrier & "'"

    If sWhere = "1=1" Then
    Me.FilterOn = False
    Else
    Me.Filter = sWhere
    Me.FilterOn = True
    End If
    end sub
    This is awesome. I am well on my way. Thank you very much.

  8. #8
    mindbender is offline Competent Performer
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    283
    Quote Originally Posted by mindbender View Post
    This is awesome. I am well on my way. Thank you very much.

    Weird - it worked the first time then it stopped working.
    Click image for larger version. 

Name:	Capture.JPG 
Views:	9 
Size:	111.4 KB 
ID:	35123


    I attached a picture of the design view form with the txt fields used for searching...txtCode, txtCarrier and txtMCode

    When the Filter button is clicked, this runs

    Code:
    Private Sub Command36_Click()Dim sWhere As String
    
    
    sWhere = "1=1"
    If Not IsNull(Me.txtCode) Then sWhere = sWhere & " and [PROD_CD]='" & Me.txtCode & "'"
    If Not IsNull(Me.txtCarrier) Then sWhere = sWhere & " and [SUB_LOC_CD]='" & Me.txtCarrier & "'"
    If Not IsNull(Me.txtMCode) Then sWhere = sWhere & " and [MKT_CD]='" & Me.txtMCode & "'"
    
    
    If sWhere = "1=1" Then
    Me.FilterOn = False
    Else
    Me.Filter = sWhere
    Me.FilterOn = True
    End If
    End Sub
    This is not returning any data at all. It did the first time I ran it but not any time after

    The unfilter button onclick runs this:

    Code:
    Private Sub Command40_Click()
    Me.FilterOn = False
    Me.txtCode.Value = ""
    Me.txtCarrier.Value = ""
    Me.txtMCode.Value = ""
    Me.Refresh
    End Sub
    I am so close...thanks for the help
    Last edited by mindbender; 08-15-2018 at 11:20 AM. Reason: added pic

  9. #9
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Hint: Null is not the same as "".
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    mindbender is offline Competent Performer
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    283
    Quote Originally Posted by pbaldy View Post
    Hint: Null is not the same as "".
    I changed it to Null and the unfilter button works but the initial filter button still returns no data

    Click image for larger version. 

Name:	Capture.JPG 
Views:	9 
Size:	27.4 KB 
ID:	35124

  11. #11
    mindbender is offline Competent Performer
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    283
    AHA....I did not need the .VALUE

    This works like a charm
    Code:
    Private Sub Command40_Click()Me.FilterOn = False
    Me.txtCode = Null
    Me.txtCarrier = Null
    Me.txtMCode = Null
    Me.Refresh End Sub

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

Similar Threads

  1. If then with parameters
    By Drew101 in forum Queries
    Replies: 12
    Last Post: 05-30-2017, 07:19 AM
  2. Asking For Parameters Twice
    By batowl in forum Reports
    Replies: 3
    Last Post: 03-08-2012, 03:01 PM
  3. Parameters
    By Alaska1 in forum Access
    Replies: 1
    Last Post: 03-30-2011, 02:05 PM
  4. Too few parameters
    By jgelpi16 in forum Programming
    Replies: 1
    Last Post: 12-23-2010, 10:28 AM
  5. Too Few Parameters
    By jgelpi16 in forum Programming
    Replies: 8
    Last Post: 09-27-2010, 01:46 PM

Tags for this Thread

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