Hello everyone!

I am developing an application with an Access DB, and i'm having trouble retrieving data from the db, i want to count how many operations have been made through two dates that i'm passing from a datetimepicker, and i can't manage to do it, since i keep getting an error saying "Data type mismatch in criteria expression". In the db, i have that date field type as a date. Before, i had it as a text, but it wouldn't work if i selected one date of x month and the other date from a different month.

So, my question is, how do i indicate the type of data in the sql query?

My code is the following:

Dim cmdtotalsei As New OleDb.OleDbCommand("select count(cod_seizure) from seizure where seizure_date between '" & DateTimePicker1.Text & "' and '" & DateTimePicker2.Text & "'", conn)

All help will be appreciated. Thank you!

Recommended Answers

All 3 Replies

As far as I know DateTimePicker has a value and not text as in -

DateTimePicker.Value

I find two problem in your code
First one is you have to convert datetimepicker1.text to dateandtime
Second thing is that you have to include dateandtime between # that is you have to change your code some thing like this

Dim cmdtotalsei As New OleDb.OleDbCommand("select count(cod_seizure) from seizure where seizure_date between #" &Convert.ToDateTime( DateTimePicker1.Text) & "# and #" &Convert.ToDateTime( DateTimePicker2.Text) & "#", conn)

I hope it will helpful for you.
Best Of Luck.

+1 AndreRet - DateTimePicker.Value property.

....

Dim cmdtotalsei As New OleDb.OleDbCommand()
cmdtotalsei.CommandText="select count(cod_seizure) from seizure where seizure_date between @dt1 and @dt2"
cmdtotalsei.Connection=conn

cmdtotalsei.Parameters.AddWithValue("@dt1",DateTimePicker1.Value)
cmdtotalsei.Parameters.AddWithValue("@dt2",DateTimePicker2.Value)

....
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.