Sam1996
Sam1996

Reputation: 35

Vb.net filter date on datagridview

I am coding using vb.net and using access. I have a datagridview with, and I want to create a button that will filter my dgv by the date that the user will choose using datetimepicker. I tried to do this, but it seems that it does not read.

Private Sub CmdOk_Click(sender As Object, e As EventArgs) Handles cmdOk.Click

        s = "Select * from tblQuestion 
Where tblQuestion.IDRespondent = " & Me.UcSayDoctor.TextBox.Text & " 
OR tblQuestion.ID_Customer = " & Me.UcSayCustomer.TextBox.Text & " 
OR tblQuestion.Date =  " & (FormatDateTime(dateFilter.Value, DateFormat.ShortDate)) & ""
        retVal = getRS(s, rs, False, sErro)

        If retVal Then
            da.Fill(ds, rs, "tblQuestion")
            mainForm.gridQuestion.DataSource = (ds.Tables("tblQuestion"))
        End If
        Me.Hide()
    End Sub

The first two part of "where" is working well. But i think I am coding wrong using datetimepicker since I am new to this style.

OR tblQuestion.Date =  " & (FormatDateTime(dateFilter.Value, DateFormat.ShortDate)) & ""

Thanks in advance.

Upvotes: 0

Views: 818

Answers (2)

Guru Rusum
Guru Rusum

Reputation: 1

Date should be in a string format and must be encoded in single quotes. In your last line add single quotes before and after date

    s = "Select * from tblQuestion Where tblQuestion.IDRespondent = " & Me.UcSayDoctor.TextBox.Text & " OR tblQuestion.ID_Customer = " & Me.UcSayCustomer.TextBox.Text & " OR tblQuestion.Date =  " & "'" & FormatDateTime(dateFilter.Value, DateFormat.ShortDate) & "'"

Upvotes: 0

Slai
Slai

Reputation: 22876

Dates in Access queries have to be surrounded by hashtags. For example = #12/31/2018# https://support.office.com/en-us/article/examples-of-using-dates-as-criteria-in-access-queries-aea83b3b-46eb-43dd-8689-5fc961f21762

In your case :

OR tblQuestion.Date = #" & dateFilter.Value.ToString("m/d/yyyy") & "#"

Upvotes: 1

Related Questions