Creating a search function

This is a discussion on Creating a search function within the C# Programming forums, part of the General Programming Boards category; Okay i have a table created in Access with these fields ... Cno Date Kgms Amount Destination Origin I am ...

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    Creating a search function

    Okay i have a table created in Access with these fields ...

    Cno
    Date
    Kgms
    Amount
    Destination
    Origin

    I am making a search form for it ..The problem is i can just get a better way to creat a search function ..The way i am creating it is just to lenghty ..This is what i am doing

    if ( cno And Date And Kgms And Amount And Destination And ORigin are not empty )
    then

    use the Select SQL statement Where Cno is this blah blah

    else if ( cno And Date And Kgms And Amount And Destination is not empty but Origin is )
    Then

    use SQL statement where ...

    U get the idea right i have to right too many if else ....How can i get rid of that
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I've written these types of things in ASP before, which possibly isn't what you're doing. Maybe they'll help you anyway, the principles can still be used:
    Code:
    dim strSQL,
        WhereClause,
        ItemValue
    
    strSQL = "SELECT * FROM tblData "
    '
    'Method 1.  Grab each form element individually
    '
    ItemValue = Request.Form("Field1")
    if ItemValue <> "" Then
      WhereClause = WhereClause & " AND Field1 = '" & ItemValue & "'"
    End If
    
    ItemValue = Request.Form("Field2")
    if ItemValue <> "" Then
      WhereClause = WhereClause & " AND Field2 = '" & ItemValue & "'"
    End If
    
    '
    ' Method 2.  Loop the array.
    '
    For Each Item In Request.Form
      ThisItemName  = Request.Form.Key (Item)
      ThisItemValue = Request.Form.Item(Item)
      WhereClause = WhereClause & " AND " & ThisItemName & " = '" & ThisItemValue & "'"
    Next
    
    If WhereClause <> "" Then
      strSQL = strSQL & " WHERE " & WhereClause & " ;"
    End If
    strSQL = strSQL & " ;"
    Watch out for bad input data corrupting your SQL string though
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 10:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21