Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2017
    Posts
    2

    Exclamation simple query but result is difficult to analyze.

    hi Friend,
    I have a search form with 3 parameters
    Name, Supplier, ProductType


    at any condition the below code the result is showing all the records from QrySrchPolymer. Output is not at all changing for any given search criteria


    please let me know where is the mistake and fix it.


    waiting for Ur suggestion.


    ID Supplier.Name PolymerName.Name ProductType.Name Color.Name EModulus Thickness Datasheet
    7 supplier 1 NARENDER product1 color 10 1 0
    8 ad polymer 1 duct color1 20 20 0
    9 Anions Pharma poly type color 30 3 0
    10 divya divya product1 color1 30 48 0








    Name : (table) PolymerName.Name ----> CName(Dim)
    Supplier : Supplier.Name ----> SName
    ProductType : ProductType.Name ----> TName




    Private Sub Command121_Click()
    ' conditions a,b,c,ab,bc,ca,abc,all empty


    Dim Qry, crt As String
    Dim CName, SName, TName As String


    'strString = Replace(strString, " ", "")


    On Error Resume Next
    If Not IsNull(Me.ChemicalName) Then
    CName = Replace(Me.ChemicalName, " ", "")
    Qry = "SELECT * FROM QrySrchPolymer WHERE PolymerName.Name Like '*" & CName & "*' "



    ElseIf Not IsNull(Me.SupplierName) Then
    SName = Replace(Me.SupplierName, " ", "")
    Qry = "SELECT * FROM QrySrchPolymer WHERE PolymerName.Name Like '*" & SName & "*' "


    ElseIf Not IsNull(Me.ProductType) Then
    SName = Replace(Me.ProductType, " ", "")
    Qry = "SELECT * FROM QrySrchPolymer WHERE ProductType.Name Like '*" & TName & "*' "



    ElseIf Not IsNull(Me.ChemicalName) And Not IsNull(Me.SupplierName) Then
    CName = Replace(Me.ChemicalName, " ", "")
    SName = Replace(Me.SupplierName, " ", "")
    Qry = "SELECT * FROM QrySrchPolymer WHERE PolymerName.Name Like '*" & CName & "*' and Supplier.Name Like '*" & SName & "*' "



    ElseIf Not IsNull(Me.SupplierName) And Not IsNull(Me.ProductType) Then
    SName = Replace(Me.SupplierName, " ", "")
    TName = Replace(Me.ProductType, " ", "")
    Qry = "SELECT * FROM QrySrchPolymer WHERE Supplier.Name Like '*" & SName & "*' AND ProductType.Name Like '*" & TName & "*'"



    ElseIf Not IsNull(Me.ProductType) And Not IsNull(Me.ChemicalName) Then
    CName = Replace(Me.ProductType, " ", "")
    CName = Replace(Me.ChemicalName, " ", "")
    Qry = "SELECT * FROM QrySrchPolymer WHERE ProductType.Name Like '*" & TName & "*' and PolymerName.Name Like '*" & CName & "*' "


    ElseIf Not IsNull(Me.ChemicalName) And Not IsNull(Me.SupplierName) And Not IsNull(Me.ProductType) Then
    CName = Replace(Me.ChemicalName, " ", "")
    SName = Replace(Me.SupplierName, " ", "")
    TName = Replace(Me.ProductType, " ", "")
    Qry = "SELECT * FROM QrySrchPolymer WHERE PolymerName.Name Like '*" & CName & "*' and Supplier.Name Like '*" & SName & "*' AND ProductType.Name Like '*" & TName & "*'"

    Else
    Qry = "SELECT * FROM QrySrchPolymer"
    End If
    'MsgBox Qry
    DoCmd.OpenReport "RptqrySearchPolymer", acViewReport, , OpenArgs:=Qry
    End Sub

  2. #2
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Does the MsgBox display the correct SQL? (uncomment it, though).
    I suspect the problem is in the report. What does the report do with the OpenArgs, and does the report have a record source defined?

  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,930
    Your OpenArgs are in the wrong location. Need 2 more commas:

    DoCmd.OpenReport "RptqrySearchPolymer", acViewReport, , , , OpenArgs:=Qry

    Instead of building a full SQL statement and passing in OpenArgs, can just build the WHERE clause and pass it in the WHERE CONDITION argument (which is where you have placed OpenArgs parameter).
    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
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Your OpenArgs are in the wrong location.
    Actually, I dont think so. Since the OP used the OpenArgs:= syntax, it shouldn't matter should it (I'm not sure - I never use it)? But mixing the two kinds of parameter references is not a good idea, anyway.
    More important is how that argument is being used in the report.




  5. #5
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Why not just make the recordsource of the report itself Qry? Simple.
    EDIT:
    I just realized that Qry is not a named query, but just a string. I'll shortly post UDF here to convert the string to a named query. Call the UDF just before opening the report.
    OK, here's the code:
    Code:
    ' Purpose   : Attach new SQL property to an existing querydef. If the Query doesn't exist,
    '           : create it with the passed SQL.
    '---------------------------------------------------------------------------------------
    '
    Function fcnCustomizeSQL(qName As String, strPassedSQL As String) As Boolean
        Dim qthisQuery As DAO.QueryDef
        'if the query has been deleted, create it
        If DCount("Name", "MSysObjects", "[Name] = " & Chr$(39) & qName & Chr$(39)) = 0 Then
            Set qthisQuery = CurrentDb.CreateQueryDef(qName, strPassedSQL)
            Set qthisQuery = Nothing
            Exit Function
        End If
        'else modify it
        Set qthisQuery = CurrentDb.QueryDefs(qName)
        qthisQuery.SQL = strPassedSQL
        Set qthisQuery = Nothing
    End Function
    qName is the name to give to the named query. strPassedSQL is the name of SQL string.
    qName value would be the name of the report's recordsource. The name stays the same, and just the querys change.
    Since you're creating a named query, it's easy to test, just run it!

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Okay, John, agreed. And I too have never used that parameter syntax.

    Still recommend setting the report RecordSource to: SELECT * FROM QrySrchPolymer;

    Then just build the WHERE clause. Open report with parameter in WHERE CONDITION:

    DoCmd.OpenReport "RptqrySearchPolymer", acViewReport, , Qry

    Then no need to modify query object or for code in report to handle OpenArgs.

    Name is a reserved word and should avoid using reserved words as names. Also is used in multiple tables, recommend not duplicating field name across tables.

    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.

  7. #7
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    June7 - I agree completely.

    Far better to do filtering outside the report, and use the WHERE parameter positionally.

    John

  8. #8
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I just realized that Qry is not a named query, but just a string.
    Uh, no I don't think so. I'd say it's a variant. OP has made the classic error of multi line declarations.
    Dim Qry, crt As String
    Dim CName, SName, TName As String
    Not saying it's the cause, but I think my esteemed peers would agree that leaving it up to Access to properly handle a variant variable when you don't realize it is a variant not a good idea.
    narendertangutooei: please use code tags and indentation for anything more than a few lines of code. It makes it much easier to read - see post #5.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 7
    Last Post: 01-11-2017, 03:27 PM
  2. Simple Line Chart field result over time
    By illicited in forum Reports
    Replies: 4
    Last Post: 01-23-2015, 05:42 PM
  3. Difficult Query!
    By pastormcnabb in forum Queries
    Replies: 2
    Last Post: 04-09-2013, 09:40 PM
  4. difficult query
    By methis in forum Queries
    Replies: 2
    Last Post: 03-18-2013, 02:36 PM
  5. Export a simple query result to an excel file
    By wop_nuno in forum Import/Export Data
    Replies: 1
    Last Post: 05-21-2009, 04:18 AM

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