Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

I hope I am in the right forum

Hello,

I have a cd collection program that has a datagrid that I have filters on it such as sort by title or artist and I have one that is sort(filter) by Year. I am using an input box to get the users request every filter except the year is working - I can't get it to filter -

can someone help?

Dim strSQL, strYear As String
       strYear = InputBox("Please enter a year to search" & vbNewLine & "Example: 1995")
       strSQL = "select CdArtist,CdTitle, CdYear,CdKey,CdCategory from Cds where CdYear between 1975 and 1995 " 'Here is the problem- what do I put in place of 1975 and 1995?
       OleDbSelectCommand1.CommandText = strSQL

       DbCdCollection1.Clear()
       OleDbDataAdapter1.Fill(DbCdCollection1)
       UpdateCounter()

 

thanks

Rob

Edited by PlausiblyDamp
Posted
What is the problem, the code you put isnot filtering or you want to know what to put in the where clause of the SQL statement ?
Dream as if you'll live forever, live as if you'll die today
Posted (edited)

When I put in the InputBox something like 1989 - 1992 which should only give me those dates.. I get ALL dates no sorting filtering of any kind

here is a filter that works

 

Private Sub mnuByTitle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuByTitle.Click
       Dim strTitle As String, strSQL As String
       strTitle = InputBox("Please enter a title to search")
       strSQL = "select CdArtist,CdTitle, CdYear,CdKey,CdCategory from Cds where CdTitle like '" & strTitle & "%'"
       OleDbSelectCommand1.CommandText = strSQL
       DbCdCollection1.Clear()
       OleDbDataAdapter1.Fill(DbCdCollection1)
       UpdateCounter()
   End Sub

 

Rob

Edited by PlausiblyDamp
Posted

Ah ok now I get it,

 

So you are inputting for ex 1982 - 1990

 

If this is the case then you need to fix the string strYear before using it in SQL.

 

Try this

Dim strSQL, strYear As String
       strYear = InputBox("Please enter a year to search" & vbNewLine & "Example: 1995")

stryear = stryear.Replace(" ","") 'removes the spaces from the input
Dim ArrYear() as String = stryear.Split("-"c) ' this would divide the string based on the - so you will have two resulting strings (which are the two years inputted by the user)
'Now ArrYear(0) contains the first year
'ArrYear(1) contains the second year
strSQL = "select CdArtist,CdTitle, CdYear,CdKey,CdCategory from Cds where CdYear between " ArrYear(0) & " and " ArrYear(1)        

OleDbSelectCommand1.CommandText = strSQL

       DbCdCollection1.Clear()
       OleDbDataAdapter1.Fill(DbCdCollection1)
       UpdateCounter()

 

Hope this helps,

Dream as if you'll live forever, live as if you'll die today

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...