Results 1 to 34 of 34

Thread: Implementing search function

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Implementing search function

    Am trying to implement a search functionality in my movie application. I want to enable people to search the database (MSACCESS 2000) using either the title, actors, genre, language, format, loan_dates etc. Attached is my GUI but I dont know how well I can code inorder to get desired results. On clicking the search button, a new form will pop-up and the movies meeting the criteria are listed in a ListView. Upon which, one can click or double click a selected item and it shows the details of this movie. The search will include AND/OR operators.
    Attached Images Attached Images  
    Last edited by osemollie; Jul 19th, 2006 at 09:23 AM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Implementing search function

    You just need to build the SQL statement based on the control values. As the SQL statement is just a string, you can use normal string manipulation to do that.

    For an example, see the "How about a search button?" section of this FAQ thread.

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Implementing search function

    You'll want a criterion choice for each search term. IOW, Genre = 'XXX' AND Country = 'YYY' OR Language = 'ZZZ', and you'll want to be able to group with parentheses - do you want a search on (Genre = 'XXX' AND Country = 'YYY') OR Language = 'ZZZ' or do you want Genre = 'XXX' AND (Country = 'YYY' OR Language = 'ZZZ')?

    The GUI to produce a real search statement can be very involved, and beyond the capability of most users to understand. It's always a trade-off - do you leave the user with not very much capability, or do you overwehelm the user with capability he can't understand?
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Implementing search function

    AL,

    I'd rather leave the user with not very much capability. Looking at the GUI which fields then should I drop, in your view?

  5. #5
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Implementing search function

    i dont know if this would work, but something along these lines?
    VB Code:
    1. Private Sub cmdSearch_Click(sSQL As String)
    2.     'first selection from keyword level operator
    3.     If cboKeyword.ListIndex = 0 Then
    4.         sSQL = "SELECT * FROM Table_Name "
    5.         sSQL = sSQL & "WHERE Title_Column = '" & cboTitle.Text & ", "
    6.         sSQL = sSQL & "AND Actors_Column = '" & cboActors.Text & ", "
    7.         sSQL = sSQL & "AND Genre_Column = '" & cboGenre.Text & ", "
    8.         sSQL = sSQL & "AND Lang_Column = '" & cboLanguage.Text & ", "
    9.         sSQL = sSQL & "AND Format_Column = '" & cboFormat.Text & ", "
    10.         sSQL = sSQL & "AND Loan_Dates_Column = '" & cboLoan_Dates.Text & "'"
    11.         cn.Execute sSQL
    12.     'second selection from keyword level operator
    13.     ElseIf cboKeyword.ListIndex = 1 Then
    14.         sSQL = "SELECT * FROM Table_Name "
    15.         sSQL = sSQL & "WHERE Title_Column = '" & cboTitle.Text & ", "
    16.         sSQL = sSQL & "OR Actors_Column = '" & cboActors.Text & ", "
    17.         sSQL = sSQL & "OR Genre_Column = '" & cboGenre.Text & ", "
    18.         sSQL = sSQL & "OR Lang_Column = '" & cboLanguage.Text & ", "
    19.         sSQL = sSQL & "OR Format_Column = '" & cboFormat.Text & ", "
    20.         sSQL = sSQL & "OR Loan_Dates_Column = '" & cboLoan_Dates.Text & "'"
    21.         cn.Execute sSQL
    22.     Else
    23.         sSQL = "SELECT * FROM Table_Name "
    24.         sSQL = sSQL & "WHERE NOT Title_Column = '" & cboTitle.Text & ", "
    25.         sSQL = sSQL & "AND NOT Actors_Column = '" & cboActors.Text & ", "
    26.         sSQL = sSQL & "AND NOT Genre_Column = '" & cboGenre.Text & ", "
    27.         sSQL = sSQL & "AND NOT Lang_Column = '" & cboLanguage.Text & ", "
    28.         sSQL = sSQL & "AND NOT Format_Column = '" & cboFormat.Text & ", "
    29.         sSQL = sSQL & "AND NOT Loan_Dates_Column = '" & cboLoan_Dates.Text & "'"
    30.         cn.Execute sSQL
    31. End Sub
    Quote Originally Posted by osemollie
    Am trying to implement a search functionality in my movie application. I want to enable people to search the database (MSACCESS 2000) using either the title, actors, genre, language, format, loan_dates etc. Attached is my GUI but I dont know how well I can code inorder to get desired results. On clicking the search button, a new form will pop-up and the movies meeting the criteria are listed in a ListView. Upon which, one can click or double click a selected item and it shows the details of this movie. The search will include AND/OR operators.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Implementing search function

    Thanks BrailleSchool, you have given me an idea of how I should work it out. Let me try it out and will keep you posted.

  7. #7
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Implementing search function

    Quote Originally Posted by osemollie
    Thanks BrailleSchool, you have given me an idea of how I should work it out. Let me try it out and will keep you posted.
    youre welcome

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Implementing search function

    If you don't want to have "empty" textboxes being part of the search, you will need to use If statements as in the example I linked to.

    By the way, BrailleSchool's example could be simplified to this:
    VB Code:
    1. Dim sKeyWord As String
    2.  
    3.   Select Case cboKeyword.ListIndex
    4.   Case 0:  sKeyWord = "AND "
    5.   Case 1:  sKeyWord = "OR "
    6.   Case 2:  sKeyWord = "AND NOT "
    7.   End Select
    8.    
    9.   sSQL = "SELECT * FROM Table_Name "
    10.   sSQL = sSQL & "WHERE Title_Column = '" & cboTitle.Text & "' "
    11.   sSQL = sSQL & sKeyWord & "Actors_Column = '" & cboActors.Text & "' "
    12.   sSQL = sSQL & sKeyWord & "Genre_Column = '" & cboGenre.Text & "' "
    13.   sSQL = sSQL & sKeyWord & "Lang_Column = '" & cboLanguage.Text & "' "
    14.   sSQL = sSQL & sKeyWord & "Format_Column = '" & cboFormat.Text & "' "
    15.   sSQL = sSQL & sKeyWord & "Loan_Dates_Column = '" & cboLoan_Dates.Text & "'"
    16.   cn.Execute sSQL

  9. #9
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Implementing search function

    i was thinking about that but didnt know how to do it. now i do
    Quote Originally Posted by si_the_geek
    If you don't want to have "empty" textboxes being part of the search, you will need to use If statements as in the example I linked to.

    By the way, BrailleSchool's example could be simplified to this:
    VB Code:
    1. Dim sKeyWord As String
    2.  
    3.   Select Case cboKeyword.ListIndex
    4.   Case 0:  sKeyWord = "AND "
    5.   Case 1:  sKeyWord = "OR "
    6.   Case 2:  sKeyWord = "AND NOT "
    7.   End Select
    8.    
    9.   sSQL = "SELECT * FROM Table_Name "
    10.   sSQL = sSQL & "WHERE Title_Column = '" & cboTitle.Text & "' "
    11.   sSQL = sSQL & sKeyWord & "Actors_Column = '" & cboActors.Text & "' "
    12.   sSQL = sSQL & sKeyWord & "Genre_Column = '" & cboGenre.Text & "' "
    13.   sSQL = sSQL & sKeyWord & "Lang_Column = '" & cboLanguage.Text & "' "
    14.   sSQL = sSQL & sKeyWord & "Format_Column = '" & cboFormat.Text & "' "
    15.   sSQL = sSQL & sKeyWord & "Loan_Dates_Column = '" & cboLoan_Dates.Text & "'"
    16.   cn.Execute sSQL

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Implementing search function

    Quote Originally Posted by si_the_geek
    VB Code:
    1. sSQL = sSQL & "WHERE Title_Column = '" & cboTitle.Text & "' "
    2.   sSQL = sSQL & sKeyWord & "Actors_Column = '" & cboActors.Text & "' "
    3.  ...
    Si, aren't you going to get an incorrect SQL statement if any of the comboboxes is blank?
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Implementing search function

    Nope, the SQL statement will be perfectly valid - but the relevant part of the Where clause will only match with rows where the field is blank (but not Null).

    (if the field was Numeric, or a date, a blank value would indeed make it an invalid SQL statement)


    In order to ignore any "blank" comboboxes, you need to use an If statement as in the example I keep on refering too.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Implementing search function

    si_the_geek, I have been to the thread but am a bit slow, am still not able to relate your code above and the one below, which is from the thread. Someone help me understand better. In my case, I dont want to have "empty" textboxes.

    VB Code:
    1. Dim strSQL as String, strWhere as String
    2.     'build the SQL statement based on what the user typed in txtSearch and txtAnotherSearch
    3.   strSQL = "SELECT * FROM tbl_master"
    4.                    '(find the conditions)
    5.   If txtSearch.Text <> "" Then strWhere = strWhere & " AND Field2 = " & Val(txtSearch.Text)
    6.   If txtAnotherSearch.Text <> "" Then strWhere = strWhere & " AND Field7 = " & Val(txtAnotherSearch.Text)
    7.   If strWhere <> "" Then    '(put the conditions into the SQL statement, without the first And)
    8.     strSQL & " WHERE " & Mid(strWhere, 5)
    9.   End If

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Implementing search function

    No problem, this kind of thing can be a bit daunting if you haven't done it before!

    It helps if you can write the SQL for each situation, for example if you were only looking for Genre the SQL might look like this:
    VB Code:
    1. strSQL = "SELECT * FROM table_name WHERE genre = '" & cboGenre.Text & "'"

    The idea is to build the SQL statement based on the control values, only using the conditions that have been specified. To do this you start of with the known part ("SELECT * FROM table_name"), and then add the conditions on to it.

    As you don't know which condtions will definitely be used (if any), you cant just place the "Where" into the known part, so we build the Where clause in a separate string, and join it on when it is finished.


    BrailleSchool's idea is a great part, and this is how you would implement it all together:
    VB Code:
    1. Dim sKeyWord As String    'Find the keyword to use
    2.   Select Case [u]cboKeyword[/u].ListIndex   '(change to the name of your keyword combo, and change the keywords below to a different order if needed)
    3.   Case 0:  sKeyWord = " AND "
    4.   Case 1:  sKeyWord = " OR "
    5.   Case 2:  sKeyWord = " AND NOT "
    6.   End Select
    7.  
    8. Dim strSQL as String, strWhere as String
    9.     'build the SQL statement based on what the user typed in txtSearch and txtAnotherSearch
    10.   strSQL = "SELECT * FROM [u]table_name[/u]"  '(change to the name of your table)
    11.  
    12.                    '(find the conditions)
    13. '(this line is basically the same as the example above, but only if the text has been set)
    14.   If cboGenre.Text <> "" Then strWhere = strWhere & sKeyWord & "Genre = '" & cboGenre.Text & "'"
    15.  
    16.   If txtAnotherSearch.Text <> "" Then strWhere = strWhere & sKeyWord & "NumericField = " & Val(txtAnotherSearch.Text)
    17.  
    18.                    '(put the conditions into the SQL statement, without the first keyword)
    19.   If strWhere <> "" Then    
    20.     strSQL & " WHERE " & Mid(strWhere, len(sKeyWord) + 1)
    21.   End If
    The most awkard part is "find the conditions", which you should probably start off with just one condition, and test it. You can then add one at a time, and test each one as you go (on it's own, and then with the ones that are already working).

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Implementing search function

    Thanks for the clarification. Lemme dive into it and will keep you posted.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Implementing search function

    I decided to take si_the_geek's advice and try out indivudual codes to confirm whether they work or not before attempting the COMBINATIONS. Looking at my GUI graphic above, you will realise that I want to allow the user to search movie eg loaned out between specified dates. How can I code that?
    - On clicking the search button, I want to open a new window and inside this window, the Results get shown in a ListView control. How to?
    - As Al42 had pointed earlier, I get an error when i try the searching while the combo boxes are empty.

    Your help is much appreciated.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Implementing search function

    I have the following code:-
    VB Code:
    1. Private Sub cmdSearch_Click()
    2. Dim strSQL As String
    3. Set rsMovie = New ADODB.Recordset
    4. Dim li As ListItem
    5.  
    6.  
    7. With rsMovie
    8. .Open "SELECT movie_inventory.movieId, movieInfo.title, MediaType.StrKey, trans1.TransDate, TransDetails.ItemDueDate FROM MediaType INNER JOIN (movieInfo INNER JOIN ((trans1 INNER JOIN TransDetails ON trans1.transactID = TransDetails.transactID) INNER JOIN movie_inventory ON TransDetails.movie_inventoryId = movie_inventory.movie_inventoryId) ON movieInfo.movieId = movie_inventory.movieId) ON MediaType.media_catId = movie_inventory.media_catId WHERE TransDate >= " & dtpLoanBefore.Value & "", cnAddMovie, adOpenForwardOnly, adLockReadOnly
    9.  
    10. Do While Not .EOF
    11. Set li = ListView1.ListItems.Add(Text:=.Fields("title") & vbNullString)
    12.     li.ListSubItems.Add Text:=.Fields("StrKey") & vbNullString
    13.     li.ListSubItems.Add Text:=.Fields("TransDate") & vbNullString
    14.     li.ListSubItems.Add Text:=.Fields("ItemDueDate") & vbNullString
    15.     .MoveNext
    16. Loop
    17. '.Close
    18. End With
    19.  
    20.  
    21.  
    22. Set rsMovie = Nothing
    23.  
    24. End Sub
    The loan checkbox is known as chkLoan. Where in my code should I place my IF ... Then .... End If code?

  17. #17
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Implementing search function

    The .Open immediately opens the recordset using the SQL you provide, so you cannot give it part of the SQL, you need all of it. So instead of having:
    .Open "SELECT ....
    You need to use:
    .Open strSQL

    Your SQL statement needs to be built in strSQL as I showed you, everything before the WHERE in your .Open line needs to be in the initial setup of strSQL (replacing strSQL = "SELECT * FROM table_name" from my example), and all control values (such as your current Where clause) need to be in If statements as in my example.

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Implementing search function

    sorry, what does this line do in the code in relation to the rest?

    VB Code:
    1. strSQL = strSQL & " WHERE " & Mid(strWhere, Len(sKeyWord) + 1)

  19. #19
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Implementing search function

    All of the conditions (in the example at least, and presumably in your project too) are 'optional', so you cannot tell which will be the first one - so we just put the same keyword before each one (then it will be between each different item, but also at the start instead of "WHERE"). By the time this line of code is reached, strWhere may contain this:
    " AND Genre = 'genre' AND NumericField = 7"

    ..which means that in order to create a proper SQL statement we need to remove the first keyword (which is what the Mid does), and replace it with WHERE, then append that to the end of the existing SQL.

  20. #20
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Implementing search function

    I just build a strWhere. If it's blank it gets " Where ", if not it gets " And ". Then it gets the condition being tested for. After all the conditionals strSQL = strSQL & strWhere.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  21. #21
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Implementing search function

    That's basically the same as the approach I've suggested - except my version makes the 'repeated' code half the size (so easier to write/read/maintain), and probably faster (as you dont repeatedly check for a blank strWhere, only do 1 Mid at the end).

    The speed isn't really an issue tho, as the whole code will run in a very small amount of time anyway.

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Implementing search function

    Nope, the SQL statement will be perfectly valid - but the relevant part of the Where clause will only match with rows where the field is blank (but not Null).

    (if the field was Numeric, or a date, a blank value would indeed make it an invalid SQL statement)
    Some fields of mine are numeric and when left blank, I get an error in the SQL code. What is the best way around it?

    - Just to mention, On clicking the search button, I want to open a new form and on this form, the results get shown in a ListView control . How to do I do that?

  23. #23
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Implementing search function

    Some fields of mine are numeric and when left blank, I get an error in the SQL code. What is the best way around it?
    Again, use If statements as shown in my example - you should not be adding any "blank" conditions to the SQL statement.

    If you can't work out how to do that, show us the current code.



    Just to mention, On clicking the search button, I want to open a new form and on this form, the results get shown in a ListView control . How to do I do that?
    Basically as you posted above (assuming that code works for the same form), but with code to load the form first (eg: Form1.Load), and prefixing the control with the form name (eg: Set li = Form1.ListView1.ListItems.Add ....).

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Implementing search function

    Still having bit of a problem with numeric field being blank. My code is:
    VB Code:
    1. Private Sub cmdTesting_Click()
    2. Dim sKeyWord As String
    3. Set rst = New Recordset
    4.    
    5.   Select Case cboField.ListIndex
    6.   Case 0:  sKeyWord = "AND "
    7.   Case 1:  sKeyWord = "OR "
    8.   Case 2:  sKeyWord = "AND NOT "
    9.  
    10.   End Select
    11.      sSQL = "SELECT movieInfo.title, MediaType.StrKey, genre.genre, movieInfo.movie_year FROM (MediaType INNER JOIN (movieInfo INNER JOIN movie_inventory ON movieInfo.movieId = movie_inventory.movieId) ON MediaType.media_catId = movie_inventory.media_catId) INNER JOIN genre ON movieInfo.GenreID = genre.genreId "
    12.   sSQL = sSQL & "WHERE title = '" & txtSearch.Text & "' "
    13.   sSQL = sSQL & sKeyWord & "genre = '" & cboGenre.Text & "' "
    14.   sSQL = sSQL & sKeyWord & "StrKey = '" & cboFormat.Text & "' "
    15.   sSQL = sSQL & sKeyWord & "movie_year = " & cboYear.ListIndex & " "
    16.  
    17.    rst.Open sSQL, cnAddMovie, adOpenKeyset, adLockPessimistic, adCmdText
    18.      'show the data
    19.  
    20. Debug.Print sSQL
    21.    
    22.    
    23.     Call PopulateList(ListView1, rst)
    24. End Sub

    - I have not gotten exactly how am to handle the loan dates. If the loan checkbox is checked, then i specify the range of dates to search.

  25. #25
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Implementing search function

    You seem to have ignored several important parts of each line in the "'(find the conditions)" part of my example, and left out the vital "'(put the conditions into the SQL statement, without the first keyword)" section

    Here's a corrected version:
    VB Code:
    1. Private Sub cmdTesting_Click()
    2. Dim sKeyWord As String
    3. Dim sSQL as String, sWhere as String
    4.   Set rst = New Recordset
    5.  
    6.   Select Case cboField.ListIndex
    7.   Case 0:  sKeyWord = "AND "
    8.   Case 1:  sKeyWord = "OR "
    9.   Case 2:  sKeyWord = "AND NOT "  
    10.   End Select
    11.  
    12.   sSQL = "SELECT movieInfo.title, MediaType.StrKey, genre.genre, movieInfo.movie_year " _
    13.        & "FROM (MediaType INNER JOIN " _
    14.               & "(movieInfo INNER JOIN " _
    15.                 & "movie_inventory ON movieInfo.movieId = movie_inventory.movieId) " _
    16.               & "ON MediaType.media_catId = movie_inventory.media_catId) " _
    17.             & "INNER JOIN genre ON movieInfo.GenreID = genre.genreId "
    18.  
    19.                    '(find the conditions)
    20.   If txtSearch.Text <> "" Then sWhere = sWhere & sKeyWord & "title = '" & txtSearch.Text & "' "
    21.   If cboGenre.Text <> "" Then sWhere = sWhere & sKeyWord & "genre = '" & cboGenre.Text & "' "
    22.   If cboFormat.Text <> "" Then sWhere = sWhere & sKeyWord & "strKey = '" & cboFormat.Text & "' "
    23.   If cboYear.ListIndex <> "" Then sWhere = sWhere & sKeyWord & "movie_year = " & cboYear.ListIndex
    24. '...
    25.  
    26.                    '(put the conditions into the SQL statement, without the first keyword)
    27.   If sWhere <> "" Then    
    28.     sSQL & " WHERE " & Mid(sWhere, len(sKeyWord) + 1)
    29.   End If
    30.  
    31.  
    32.  
    33.   rst.Open sSQL, cnAddMovie, adOpenKeyset, adLockPessimistic, adCmdText
    34.      'show the data
    35.  
    36. Debug.Print sSQL
    37.    
    38.    
    39.     Call PopulateList(ListView1, rst)
    40. End Sub


    For the Loan Dates you may want to set up the condition like this (with an appropriate If before it):
    VB Code:
    1. sWhere = sWhere & sKeyWord & "(LoanDate >= #" & txtLoadStart.Text & "# AND LoanDate >= #" & txtLoadEnd.Text & "#)"

  26. #26

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Implementing search function

    si_the_geek, I really appreciate your patience and toleranace. Am determined to learn and this is proving a wonderful experience. For real, one cannot find your kind of teaching in a book. Thanks.

    - Am faced with another dilema. All the fields except the loan/return controls search a different query (movieInfo, MediaType, genre etc tables). But the loan/return controls search using a different query all together (generated from order and order_details tables). How can I ensure that a specific query runs depending on the controls used?
    - Looking at the GUI, my intention of using the field combobox was to search the database using the selected/filled in values. My choice for keyword level search was to enable a user to search using keywords typed in the textbox. For example I might have a movie title as 'Twins' and another one as 'The Siamese Twins', the keyword search should allow one to search using 'Twins' and all the two movies should come up.

  27. #27
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Implementing search function

    Quote Originally Posted by osemollie
    - Am faced with another dilema. All the fields except the loan/return controls search a different query (movieInfo, MediaType, genre etc tables). But the loan/return controls search using a different query all together (generated from order and order_details tables). How can I ensure that a specific query runs depending on the controls used?
    VB Code:
    1. If chkLoan.Value = 1 Then
    2. ...
    3. ElseIf chkReturn.Value = 1 Then
    4. ...
    5. Else
    6. <search code for comboboxes>
    7. End If
    I'd also disable (<control>.Enabled = False) the controls you're not going to be doing the search on if either one is checked, so the user knows not to set things that won't be searched on.

    - Looking at the GUI, my intention of using the field combobox was to search the database using the selected/filled in values. My choice for keyword level search was to enable a user to search using keywords typed in the textbox. For example I might have a movie title as 'Twins' and another one as 'The Siamese Twins', the keyword search should allow one to search using 'Twins' and all the two movies should come up.
    Also using the comboboxes, or ignoring the comboboxes?
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  28. #28
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Implementing search function

    I'm not quite sure what you mean from your "Twins" example, it may be as simple as changing from this:
    VB Code:
    1. ... & "title = '" & txtSearch.Text & "' "
    ..to this:
    VB Code:
    1. ... & "title Like '%" & txtSearch.Text & "%' "
    That will find any rows where the Title field contains the contents of txtSearch.Text at any position in the field (so "Twins" would match both of your examples, but it would also match things like "Twinset").


    si_the_geek, I really appreciate your patience and toleranace. Am determined to learn and this is proving a wonderful experience. For real, one cannot find your kind of teaching in a book. Thanks.
    Thanks.

    I aim to help people become better programmers, rather than simply throwing examples at them (but I do that too!). It's good to see people trying to learn.

  29. #29

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Implementing search function

    Looking at my GUI above, my movie application allows one to borrow movies for:

    If VCD =3 days
    DVD = 2 days
    VHS = 4

    How can I ensure that if one borrows a VHS for example, the checkout date is increased automatically to 4 days from the day of checkout? And there after I will have an admin form where I want to allow the user to determine the number of days allowed, instead of the default that I have set.

    How can I code that?

    Thanks.

  30. #30
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Implementing search function

    What control are you using to tell the program what kind of movie it is? Option buttons? Check boxes?

  31. #31

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Implementing search function

    Sorry, I have moved the question to the appropriate thread "Which control can I use" Look at the GUI there. Am using comboboxes to determine types.

  32. #32
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Implementing search function

    Quote Originally Posted by osemollie
    How can I ensure that if one borrows a VHS for example, the checkout date is increased automatically to 4 days from the day of checkout?
    Use the Datediff function.
    And there after I will have an admin form where I want to allow the user to determine the number of days allowed, instead of the default that I have set.

    How can I code that?
    Keep the number of days for each in your database - in a table named Expiration, maybe? The program looks up the 3 expiration times when it runs and keeps them in global variables. Another function allows someone with the correct password to change the values, both in the global variables and in the data table (at the same time - change in both or neither).
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  33. #33

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Implementing search function

    Hi AL42, thanks for coming to my rescue. Look at : http://www.vbforums.com/showthread.p...01#post2556401

    to see the GUI am using. Am a bit not sure of how to implement your solution. The expiry dates are saved in a table Mediatype wshose fields are: mediaTypeId, mediaType, DaysForBorrowing.

    Thanks

  34. #34
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Implementing search function

    Just get DaysForBorrowing and use it in a DateAdd - dtReturnDate = DateAdd("d", DaysForBorrowing, Now) and write a little utility that allows someone with the correct password to change DaysForBorrowing in the table (a simple Update SQL statement).
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width