1

I'm trying to use this query, but I get an error

Data mismatch in criteria

Dat is the Date column inside the MS Access database. I'm trying to select the sum of column named Total for every row named Pro between a date picked from the datepickers...

There are many posts regarding this, but my query is different then those

Dim DTST As String
DTST = DateTimePicker1.Value.ToString

Dim DTEn As String
DTEn = DateTimePicker2.Value.ToString

Dim Query1 As String = "SELECT SUM(Total) FROM [T500] WHERE Pro =@Pro AND Dat BETWEEN'" + DTST + "' AND '" + DTEn + "'"

Dim cmd2 As OleDb.OleDbCommand = New OleDbCommand(Query1, dbCon)
cmd2.Parameters.AddWithValue("@Pro", ComboBoxBP.SelectedItem.ToString)
2
  • Hey date/time format expert! Why don't you use sql parameter for DTST and DTEn and let the framework do it's job? Did you ever wonder what <DateTime>.ToString() returns? Is it a valid ms-access date format? What about localization? Apr 2, 2015 at 8:38
  • It is a valid format. I've changed it to match the format in my database. Tried it without ToString, same circle of errors, either syntax error, or data mismatch :S
    – Taffs
    Apr 2, 2015 at 8:40

3 Answers 3

4

The Dates are just additional parameters. One of the problems with the way you (and the other answers) are doing it is that you are converting perfectly good DateTime variables to string. MSAccess/OleDb will usually make sense of things, but it is unnecessary and allowing something else to interpret your intent is usually undesirable.

The DB columns must be implemented as a Date type in order for the data to be treated as Dates (BETWEEN), but you do not need to "format" the Date variables (ever).

Another problem is disposing of Command and Connection objects when done with them:

Dim SQL = "SELECT SUM(Total) FROM [T500] WHERE Pro =@Pro AND Dat BETWEEN @dt1 AND @dt2"


Using dbCon As OleDbCOnnection(GetConnection()),
     cmd As New OleDbCommand(SQL, dbCon)

    dbCon.Open

    ' ToDO be sure SelectedItems.COunt >0 earlier
    cmd2.Parameters.AddWithValue("@Pro", ComboBoxBP.SelectedItem.ToString)
    cmd2.Parameters.AddWithValue("@dt1",  DateTimePicker1.Value)   
    cmd2.Parameters.AddWithValue("@dt2",  DateTimePicker2.Value)  

    Dim Total = cmd.ExecuteScalar()
    ...
End Using     ' close and dispose of Command and Connection objects

As you can see, you pass DateTime values to it as any other parameter, and the DTP's.Value property will work perfectly well without any massaging or processing.

Here is a link for info on the GetConnection() method and dbConnections in general.


Note that OleDB does not actually use named parameters (@Pro, @dt1). They are just placeholders, you have to AddWithValue in the same order as they appear in the SQL statement. It is more common to see params specified as "?", but meaningful params are helpful in mapping the right var to the right param in code.

Finally, it cant happen with a DateTimePicker, but gluing bits of string from UI controls together to make SQL can result in SQL injection attacks and should always be avoided. SQL using parameters is generally easier to code, build, read and mantain.

2

My edit seems to be cancelled, so here is:

Dim DTST As String
DTST = DateTimePicker1.Value.ToString("'#'yyyy'/'MM'/'dd'#'")

Dim DTEn As String
DTEn = DateTimePicker2.Value.ToString("'#'yyyy'/'MM'/'dd'#'")

Dim Query1 As String = "SELECT SUM(Total) FROM [T500] WHERE Pro =@Pro AND Dat BETWEEN " + DTST + " AND " + DTEn + ""
0
1

A parameterised date in Access needs to be wrapped in # outside the quotes (and inside if it was a literal date e.g. #10/10/2015#

Dim DTST As String
DTST = DateTimePicker1.Value.ToString("'#'yyyy'/'MM'/'dd'#'")

Dim DTEn As String
DTEn = DateTimePicker2.Value.ToString("'#'yyyy'/'MM'/'dd'#'")

Dim Query1 As String = "SELECT SUM(Total) FROM [T500] WHERE Pro =@Pro AND Dat BETWEEN " + DTST + " AND " + DTEn + ""

Dim cmd2 As OleDb.OleDbCommand = New OleDbCommand(Query1, dbCon)
cmd2.Parameters.AddWithValue("@Pro", ComboBoxBP.SelectedItem.ToString)
6
  • I have tried with "#" before and got syntax error... Additional information: Syntax error in date in query expression 'Pro =@Pro AND Dat BETWEEN #'4/1/2015 12:00:00 AM'# AND #'4/2/2015 12:00:00 AM''.
    – Taffs
    Apr 2, 2015 at 8:18
  • Try it now, ive changed the position
    – Matt
    Apr 2, 2015 at 8:24
  • Now it says Data type mismatch again :S These errors have been circling no matter what I try...
    – Taffs
    Apr 2, 2015 at 8:27
  • Try again with them directly on the values
    – Matt
    Apr 2, 2015 at 8:37
  • This one gives "expression expected" on the first "#" and makes the query invalid :S Thanks for sticking with this so far...
    – Taffs
    Apr 2, 2015 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.