Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have various search boxes in my program (one per form) that is coded to bring results back by various methods and it works beautifully, so long as the returned results are in Integer format. My current problem is I am working a new form and need the search box to bring back results by REPAIR_DATE, which is a DateTime field.

my current code as follows:

VB
<pre>    Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
        Cursor = Cursors.WaitCursor
        Dirty()

        If cbColName.Text = "SEARCH BY:" Then
            MeMsgBoxSearchCriteria.ShowDialog()
        Else : lbSearchResults.Items.Clear()
            Select Case MaintenanceDataSet.Maintenance_Table.Columns(cbColName.Text).DataType
                Case GetType(Integer)
                    DV.RowFilter = cbColName.Text & " = " & tbSearchInput.Text.Trim
                Case GetType(Date)
                    DV.RowFilter = cbColName.Text & " = #" & tbSearchInput.Text.Trim & "#"
                Case Else
                    DV.RowFilter = cbColName.Text & " LIKE '*" & tbSearchInput.Text.Trim & "*'"
            End Select

            If DV.Count > 0 Then
                For IX As Integer = 0 To DV.Count - 1
                    lbSearchResults.Items.Add(DV.Item(IX)("ID"))
                Next
                If DV.Count = 1 Then
                    lbSearchResults.SelectedIndex = 0
                    Dim ix As Integer = MaintenanceDataSetBindingSource.Find("ID", CInt(lbSearchResults.SelectedItem.ToString))
                    MaintenanceDataSetBindingSource.Position = ix
                Else
                    lbSearchResults.Visible = True
                    lbSearchResults.BringToFront()
                End If
            Else
                ' Display a message box noting the search criteria is not found.   
                MeMsgBoxNoSearch.ShowDialog()
            End If
        End If
        Cursor = Cursors.Default
    End Sub

    Private Sub lbSearchResults_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lbSearchResults.SelectedIndexChanged
        Dim ix As Integer = MaintenanceDataSetBindingSource.Find("ID", CInt(lbSearchResults.SelectedItem.ToString))
        MaintenanceDataSetBindingSource.Position = ix
        lbSearchResults.Visible = False
    End Sub


I am looking to bring back results based on column REPAIR_DATE.

What I have tried:

I tried redefining "ix as Integer" to "ix as Date" and changing "CInt(lbSearchResults.SelectedItem.ToString)" to "CDate(lbSearchResults.SelectedItem.ToString)" and it ERRORS WITH "value of type 'integer' cannot be converted to 'date'" I am still fairly new to VB and not sure how to properly bring REPAIR_DATE results back as opposed to an integer. I do get the results by date in my listbox (when I use REPAIR_DATE as the sort-by field, but with code set as I have it above, but when clicking on one of the returned results to load that record it causes the program to abort with the above listed error mainly because it is looking to return a number not a date. I understand why it fails, I just do not know how to resolve.
Posted
Updated 28-May-17 10:59am
v2

1 solution

Quote:
I tried redefining "ix as Integer" to "ix as Date" and changing "CInt(lbSearchResults.SelectedItem.ToString)" to "CDate(lbSearchResults.SelectedItem.ToString)" and it ERRORS WITH "value of type 'integer' cannot be converted to 'date'"

The error message is quite clear. Assuming that ID field is type of integer, you cannot implicity convert it to date/datetime data type.
You have to refer to REPAIR_DATE field in according procedure.

Note that there's few other issues in your code:

  1. ToString() method is redundant:
    VB.NET
    Dim ix As Integer = MaintenanceDataSetBindingSource.Find("ID", CInt(lbSearchResults.SelectedItem.ToString))
  2. to filter data by date, i'd suggest to change your code:
    VB
    DV.RowFilter = cbColName.Text & " = #" & tbSearchInput.Text.Trim & "#"

    to:
    VB
    Dim ci As CultureInfo = CultureInfo.InvariantCulture
    Dim stringDate As String = tbSearchInput.Text.Trim
    DV.RowFilter = String.Format("{0}=#{1}#", cbColName.Text, stringDate.Format("yyyy/MM/dd", ci))

    It's good programming practice to use ISO datetime format. Depending on situation you may need to add time part.
    Have a look here: vb.net - How to use BindingSource.Filter for a Date in visual basic 2012? - Stack Overflow[^]




Good luck!
 
Share this answer
 
Comments
K3JAE 29-May-17 15:00pm    
Re Item 1: At your suggestion: Removed the .ToString method.
Item 2: Made suggested change, thanks for that piece of info. Good to know.

Re the original question: I am not fully sure, or understand what you are referring to when you say: "You have to refer to REPAIR_DATE field in according procedure." I thought I had done this previously but with no luck. Can you be a bit more specific with what you are suggesting?

I am looking for the result of any search to come back only with a Date of Repair (list of dates if multiple returns on the search criteria). Currently I am coming back with only a Record ID or IDs.

I do appreciate your help. As I'm sure you can obviously tell, I am not a very proficient coder and still learning new things daily.
Maciej Los 29-May-17 16:42pm    
Don't downgrade your value. Every single programmer once have been a beginner.
I'll try to inspect your code again to be able to provide more details, but you have to debug your programme to identify why filtering data on date doesn't work.
K3JAE 2-Jun-17 4:54am    
Any luck with resolving my issue? I still cannot seem to get the REPAIR_DATE to show in my results off my search. Just asking as a reminder.
Maciej Los 5-Jun-17 5:14am    
No. I can't reproduce your issue. ;(
K3JAE 6-Jun-17 16:53pm    
Would more page code possibly assist? I truly am wondering if I am doing something completely wrong here. I want to bring back results based on REPAIR DATE instead of ID? Therefore I need to figure out how to do that properly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900