Results 1 to 12 of 12

Thread: [RESOLVED] Error in code

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Resolved [RESOLVED] Error in code

    Am trying to search my database using dates. Where am i going wrong? My code is
    VB Code:
    1. If chkPurchased.Enabled Then sWhere = sWhere & sKeyWord & "PurchaseDate >= & "#" & dtpPurchaseAfter.Value & "#" AND PurchaseDate <= & "#" & dtpPurchaseAfter.Value & "#"

    Thanks.

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

    Re: Error in code

    What error are you getting?

  3. #3
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: Error in code

    If you are going against a SQL database, you need to change the # to an apostrophe

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

    Re: Error in code

    Please always tell us what the error is, even if you think it is obvious.

    After inspection I can see that you haven't built the string properly, it should be like this:
    VB Code:
    1. If chkPurchased.Enabled Then sWhere = sWhere & sKeyWord & "PurchaseDate >= #" & dtpPurchaseAfter.Value & "# AND PurchaseDate <= #" & dtpPurchaseAfter.Value & "#"

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Error in code

    Quote Originally Posted by si_the_geek
    Please always tell us what the error is, even if you think it is obvious.

    After inspection I can see that you haven't built the string properly, it should be like this:
    VB Code:
    1. If chkPurchased.Enabled Then sWhere = sWhere & sKeyWord & "PurchaseDate >= #" & dtpPurchaseAfter.Value & "# AND PurchaseDate <= #" & dtpPurchaseAfter.Value & "#"
    My apologies for not giving all the info. I have tried the above but came up with no results at all yet i chose dates that fall within the scope.

    My complete code is:
    VB Code:
    1. rivate Sub cmdSearch_Click()
    2.  
    3. Dim sKeyWord As String
    4. Dim sSQL As String, sWhere As String
    5.   Set rst = New Recordset
    6.  
    7.   Select Case cboField.ListIndex
    8.   Case 0:  sKeyWord = "AND "
    9.   Case 1:  sKeyWord = "OR "
    10.   Case 2:  sKeyWord = "AND NOT "
    11.   End Select
    12.  
    13.  
    14. sSQL = "SELECT movieInfo.title, MediaType.StrKey, genre.genre, movie_inventory.PurchaseDate, movieInfo.movie_year, country.country, language1.language1, audienceRating.rating " _
    15.         & "FROM audienceRating INNER JOIN (language1 INNER JOIN " _
    16.         & "(country INNER JOIN ((MediaType INNER JOIN (movieInfo INNER JOIN movie_inventory " _
    17.         & "ON movieInfo.movieId = movie_inventory.movieId) ON " _
    18.         & "MediaType.media_catId = movie_inventory.media_catId) INNER JOIN " _
    19.         & "genre ON movieInfo.GenreID = genre.genreId) ON country.countryId = movieInfo.countryId)" _
    20.         & "ON language1.languageId = movieInfo.languageId) ON audienceRating.ratingId = movieInfo.ratingId"
    21.  
    22.   If txtSearch.Text <> "" And chkMatchcase.Value Then sWhere = sWhere & sKeyWord & "title Like '%" & txtSearch.Text & "%' "
    23.   If txtSearch.Text <> "" And chkMatchWhole.Value Then sWhere = sWhere & sKeyWord & "title = '" & txtSearch.Text & "' "
    24.   If txtReference.Text <> "" Then sWhere = sWhere & sKeyWord & "reference = '" & txtReference.Text & "' "
    25.   If cboGenre.Text <> "" Then sWhere = sWhere & sKeyWord & "genre = '" & cboGenre.Text & "' "
    26.   If cboSubgenre.Text <> "" Then sWhere = sWhere & sKeyWord & "genre = '" & cboSubgenre.Text & "' "
    27.   If cboCountry.Text <> "" Then sWhere = sWhere & sKeyWord & "country = '" & cboCountry.Text & "' "
    28.   If cboFormat.Text <> "" Then sWhere = sWhere & sKeyWord & "strKey = '" & cboFormat.Text & "' "
    29.   If cboYear.Text <> "" Then sWhere = sWhere & sKeyWord & "movie_year = '" & cboYear.Text & "' "
    30.   If chkPurchased.value Then sWhere = sWhere & sKeyWord & "PurchaseDate >= #" & dtpPurchaseAfter.Value & "# AND PurchaseDate <= #" & dtpPurchaseAfter.Value & "#"
    31.  
    32.   If sWhere <> "" Then
    33.     sSQL = sSQL & " WHERE " & Mid(sWhere, Len(sKeyWord) + 1)
    34.   End If
    35.  
    36.   rst.Open sSQL, cnAddMovie, adOpenKeyset, adLockPessimistic, adCmdText
    37.      
    38.     Call PopulateList(ListView1, rst)
    39.    
    40. End Sub

    Thanks for your help

  6. #6
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: Error in code

    Have you tried this:

    If chkPurchased.value Then sWhere = sWhere & sKeyWord & "PurchaseDate >= #" & cdate(dtpPurchaseAfter.Value) & "# AND PurchaseDate <= #" & cdate(dtpPurchaseAfter.Value) & "#"

  7. #7
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Error in code

    Do a

    Debug.Print YourSQLStatement

    then Post that....

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Error in code

    Quote Originally Posted by randem
    Do a

    Debug.Print YourSQLStatement

    then Post that....
    This is what I get:
    VB Code:
    1. SELECT movieInfo.title, MediaType.StrKey, genre.genre, movieInfo.movie_year, country.country, language1.language1, movie_inventory.PurchaseDate, audienceRating.rating FROM audienceRating INNER JOIN (language1 INNER JOIN (country INNER JOIN ((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) ON country.countryId = movieInfo.countryId)ON language1.languageId = movieInfo.languageId) ON audienceRating.ratingId = movieInfo.ratingId WHERE PurchaseDate >= #7/1/2006 1:36:12 PM# AND PurchaseDate <= #7/1/2006 1:36:12 PM#

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

    Re: Error in code

    Ok... first of all, here's the same SQL statement in a way we can read it:
    Code:
    SELECT    movieInfo.title,
              MediaType.StrKey,
              genre.genre,
              movieInfo.movie_year,
              country.country,
              language1.language1,
              movie_inventory.PurchaseDate,
              audienceRating.rating
    
    FROM      audienceRating
              INNER JOIN ( language1
              INNER JOIN ( country
              INNER JOIN ( ( 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 ) 
                ON country.countryId = movieInfo.countryId ) 
                ON language1.languageId = movieInfo.languageId ) 
                ON audienceRating.ratingId = movieInfo.ratingId
    
    WHERE     PurchaseDate > = #7/1/2006 1:36:12 PM#
              AND PurchaseDate < = #7/1/2006 1:36:12 PM#
    As the Where clause is twice comparing the same field to the same value (just with different signs), it is effectively:
    Code:
    WHERE     PurchaseDate = #7/1/2006 1:36:12 PM#
    ..which I'm guessing isn't what you wanted. I'm assuming that you should either be using a different value for one of them, or a different field.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Error in code

    Am actually trying to limit my search to a given period. Like in this case let us say I want all movies that were bought between July 1, 2006 and September 10, 2006. All these information is stored in one field PurchaseDate. How can I query this?

    Thanks.

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

    Re: Error in code

    You will obviously need to get two dates from the client, one for the start of the date range, and one for the end of it. For this you could use two controls, eg: dtpPurchaseAfterStart and dtpPurchaseAfterEnd

    You can then put these into the appropriate places in the "If chkPurchased.value Then" line.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Error in code

    Quote Originally Posted by si_the_geek
    You will obviously need to get two dates from the client, one for the start of the date range, and one for the end of it. For this you could use two controls, eg: dtpPurchaseAfterStart and dtpPurchaseAfterEnd

    You can then put these into the appropriate places in the "If chkPurchased.value Then" line.
    Thanks si. I actually have traced where the problem was. As you rightly put i needed two controls to define start/end date. But when you look at my code above, I made a mistake and used the same control in my WHERE clause. It is now sorted out and working.

    Thanks all of you.

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