CJ7
CJ7

Reputation: 23295

How to ensure dates are in correct format for OleDbCommand parameters?

When I execute the code below against an MDB database, the data table is empty, however when I run this in a query tool against the database it returns 2 records.

What could be the problem?

Is it an issue with the date format of the parameters (ie. 8/5/13 vs 5/8/13)?

Using oDB As OleDbConnection = GetDbConnection()
    Using oCmd As New OleDbCommand("SELECT * " & _
            " FROM Table1, Table2" & _
            " WHERE (Table1.Date BETWEEN @Date1 AND @Date2) AND (Table1.Id IS NULL) AND (Table2.number = Table1.num) AND (Table1.code1 = Table2.code1) ", oDB)
        oCmd.Parameters.AddWithValue("@Date1", Date.Today)
        oCmd.Parameters.AddWithValue("@Date2", Date.Today.AddDays(Me.intDaysAhead))
        oDB.Open()
        dt = New DataTable()
        Using da As OleDbDataAdapter = New OleDbDataAdapter(oCmd)
            da.Fill(dt)
        End Using
    End Using
End Using

Upvotes: 0

Views: 1872

Answers (1)

IvanH
IvanH

Reputation: 5159

I think you are right. I recommend to specify a parameter type OleDbType

 oCmd.Parameters.Add("@Date1", OleDb.OleDbType.Date).Value = Date.Today

Upvotes: 1

Related Questions