0

I am trying to fetch Date, Month and Year separately from date column in Access Database.

I am using following code for it.

I don't know what is the problem with this, but either it shows me error or no data is returned.

I am new to OLEDB so I don't know if it is possible or not.

Please help.

And please show me alternatives if this way is incorrect.

    conn_string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\MHV\Documents\Visual Studio 2012\Projects\UTS\UTS.mdb"

    conn = New OleDbConnection(conn_string)
    conn.Open()

    Grid_string = "SELECT datepart(mm,T_Date) from Transactions"

    Grid_cmd = New OleDbCommand(Grid_string, conn)
    RW_AD = New OleDbDataAdapter(Grid_cmd)
    Grid_DS = New DataSet

    Grid_cmd.Connection = conn
    Grid_cmd.CommandText = Grid_string

    RW_AD.Fill(Grid_DS, "Transactions")

    Grid_cmd.ExecuteNonQuery() 

    DataGridView1.DataSource = Grid_DS.Tables("Transactions").DefaultView

P.S. : Connection and other things are working fine. It only shows me error when I use datepart().

Sunil Kumar
  • 3,142
  • 1
  • 19
  • 33
user3762349
  • 1
  • 1
  • 4

4 Answers4

1

Can you please try to making datepart interval in quotation?

Grid_string = "SELECT datepart(\"mm\",T_Date) from Transactions"
Kiran Hegde
  • 3,651
  • 1
  • 16
  • 14
1

Using ' instead of " will fix the problem:

Grid_string = "SELECT datepart('mm',T_Date) from Transactions"

Shikkediel
  • 5,195
  • 16
  • 45
  • 77
0

Try using m instead of mm

Grid_string = "SELECT datepart('m',T_Date) from Transactions"
loki
  • 9,816
  • 7
  • 56
  • 82
0

Data Type returned is Int16. Cast to Int16 or you'll get error

Gianluca Demarinis
  • 1,964
  • 2
  • 15
  • 21