1

The function below works fine and outputs the correct result except it outputs the dates as strings and not as dates. How can I get it to output dates instead?

Function GetExpiries_YieldX(TradeDate As Date, Code As String) As Variant

    'Create and open the connection
    Dim oConnection As Connection
    Set oConnection = New Connection
    oConnection.ConnectionString = strConnectionStringYieldX
    oConnection.Open

    'Create the command object
    Dim oCommand As Command
    Set oCommand = New Command
    oCommand.CommandType = adCmdText

    Dim SQLString As String

        SQLString = "SELECT DISTINCT Expiry" _
                 & " FROM MTM" _
                 & " WHERE TradeDate = ?" _
                 & "   and Code = ?"

    oCommand.CommandText = SQLString
    oCommand.ActiveConnection = oConnection

    oCommand.Parameters.Append oCommand.CreateParameter("Date", adDBTimeStamp, adParamInput)
    oCommand.Parameters.Append oCommand.CreateParameter("Code", adVarChar, adParamInput, 50)

    oCommand.Parameters.Item("Date").Value = TradeDate
    oCommand.Parameters.Item("Code").Value = Code

    Dim result As New ADODB.Recordset
    Set result = oCommand.Execute

    Dim resultA As Variant
    'GetExpiries_YieldX = WorksheetFunction.Transpose(result.GetRows)
    GetExpiries_YieldX = result.GetRows

    oConnection.Close

End Function
13
  • is the column Expiry in the database defined as DataTime? Commented Apr 10, 2013 at 13:47
  • what are you doing with the resultset? If putting into a worksheet range, you could just dump the recordset into a range using Range(..).CopyFromRecordset method, then modify the format of those range using NumberFormat Commented Apr 10, 2013 at 13:49
  • @Philip no it's just date type (SQL Server 2008 btw), I am putting the dates on the worksheet. At the moment I'm just converting them to dates doing this sort of thing: =DATE(LEFT(A2,4),MID(A2,6,2),RIGHT(A2,2)) but I would prefer if they just came out as dates if possible. I find it very difficult to find decent documentation on how to write these sort of functions (i.e. vba functions that query sql server dbs) properly :/ Commented Apr 10, 2013 at 13:59
  • or you could try casting the Expiry field into a date format in your SQL string: "SELECT DISTINCT CDATE(Expiry)... Commented Apr 10, 2013 at 14:00
  • casting just returns #VALUES... Commented Apr 10, 2013 at 14:01

1 Answer 1

3

ADODB versions prior to 2.8 had aproblem recognizing the new Date datatype introduced in SQL Server 2008.

So, check the following:

  1. Ensure you are using the native SQL Server Provider (NOT OLEDB SQL provider) in your Connnection string, so it should be something like "Provider=SQLNCLI10.1;Data Source=MyServer;Initial Catalog=MyDatabase;Uid=MyUser;Pwd=MyPassword;"
  2. Make sure you've installed latest SQL Native Client (sqlncli2008.msi or something like that, you can steal it from SQL Server 2008 redistributable)
  3. Ensure ADO version referenced in your VB Project is 2.8

found all that on ADODB & SQL 2008 (MS Forums)

I hope that helps you get a Date column in your array from a Date column in your db!

Sign up to request clarification or add additional context in comments.

2 Comments

Just changing the connection string (i.e. step 1 n your answer) fixed it! Thanks!
you are most welcome, it was fun going back and forth to work out what was going on :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.