Vb.net在DataGridView上的过滤日期

时间:2019-07-15 20:03:56

标签: vb.net ms-access datagridview datetimepicker

我正在使用vb.net和访问进行编码。我有一个datagridview,我想创建一个按钮,该按钮将按用户使用datetimepicker选择的日期过滤dgv。我试图这样做,但似乎看不懂。

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

“ where”的前两个部分运行良好。 但是我认为我使用datetimepicker编码错误,因为我是这种风格的新手。

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

谢谢。

2 个答案:

答案 0 :(得分:1)

Access查询中的日期必须用#号包围。例如= #12/31/2018# https://support.office.com/en-us/article/examples-of-using-dates-as-criteria-in-access-queries-aea83b3b-46eb-43dd-8689-5fc961f21762

在您的情况下:

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

答案 1 :(得分:0)

日期应为字符串格式,并且必须用单引号引起来。在最后一行中,在日期前后加上单引号

    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) & "'"