views:

196

answers:

2

Hello ,

before a little time , I used a code to get the dates between 2 dates from the database (column with dates dd/mm/yy) , I think it works nice first time , the code is :

      Dim b As New Date
            Dim a As Integer
            a = Val(tx2.Text)
            b = System.DateTime.Today
            b = b.AddDays(-a)

             MsgBox(b)
 Conn.Open()
  SQLstr = " Select * from tb where lastvstart BETWEEN #01/01/1800# AND #" & b & "#"
 Dim DataAdapter1 As New OleDbDataAdapter(SQLstr, Conn)
   DataSet1.Clear()

        DataAdapter1.Fill(DataSet1, "Tb")
        Conn.Close()

as you see , the code let the user to insert a number and minus it form the date of today , then calculates the date that I want , after that I use BETWEEN Clause to get all dates between them

But now , this code gets some data and overpasses others , I mean some of the dates is between the tow dates but the code never get it , why that happens ?

+2  A: 

If you look at the generated SQL string, does it contain the date that you expect? I would assume that the database requires it to follow a specific format (either dd/MM/yyyy or MM/dd/yyyy given the hard coded date in the query). Could it be that your day and month switch places when the string version of the date is created and inserted into your SQL query?

As a side note, I would strongly recommend against concatenating SQL queries together like that. If possible, use parameterized queries instead. That could possibly also remove some type conversion issues.

Update
Here is an example of using a parameterized query over OLE DB to an Access database:

Using connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""C:\path\file.mdb""")
    Using cmd As New OleDbCommand("select * from tb where lastvstart BETWEEN ? AND ?", connection)

     Dim param As OleDbParameter

     ' add first date '
     param = New OleDbParameter()
     param.DbType = DbType.Date
     param.Value = New DateTime(1800, 1, 1)
     cmd.Parameters.Add(param)

     'add second date '
     param = New OleDbParameter()
     param.DbType = DbType.Date
     param.Value = DateTime.Today.AddDays(-a)
     cmd.Parameters.Add(param)
     cmd.Parameters.Add(New OleDbParameter())

     connection.Open()
     Using adapter As New OleDbDataAdapter(cmd)
      Using ds As New DataSet()
       adapter.Fill(ds)
       Console.WriteLine(ds.Tables(0).Rows.Count)
      End Using ' DataSet ' 
     End Using ' OleDbDataAdapter '
    End Using ' OleDbCommand '
End Using ' OleDbConnection '
Fredrik Mörk
How can I use parameterized queries , I'm a freshman on it :P
Eias.N
I find and "Add" parameter but never find Update ?
Eias.N
Works perfectly , thanx
Eias.N
Paremeterized queries can be a huge pain. I've been using queries concatenated in VBA code for about 15 years now and they work just fine.
Tony Toews
@Tony Toews: making *really* sure that the input is clean, or cleaning up after someone using the security hole can also be a huge mess.
Fredrik Mörk
+1  A: 

Can you not change the Sqlstr to

SQLstr = " Select * from tb where lastvstart <= '" & b.ToString("dd MMM yyyy") & "'";

EDIT, change based on DB

Use this string and check if it works

SQLstr = " Select * from tb where lastvstart <= #" & b.ToString("dd MMM yyyy") & "#";
astander
When I use this code , compiler highlight the following code : DataAdapter1.Fill(DataSet1, "Tb")and tell me that types of data is not the same ? what is the problem ?
Eias.N
You will need to show a bit more code for us to check.
astander
I edited the code above , I hope it'll helps
Eias.N
Which database are you using?
astander
MS ACCSESS 2007
Eias.N
Check the edited answer, MS ACCESS dates work slightly different to Sql Server Dates
astander
I used the parameters , it solve the problem thank you astander
Eias.N