+ Reply to Thread
Results 1 to 3 of 3

Getting actual date in SQL Query

  1. #1
    Registered User
    Join Date
    06-29-2011
    Location
    Lisbon
    MS-Off Ver
    Excel 2010
    Posts
    5

    Getting actual date in SQL Query

    Good night everyone

    I'm trying to get the "now" date instead of a defined date [ (Tab_SERVICO.Data_SRV>={ts '2011-06-30 00:00:00'}) ] in the code below (in bold)
    How can I do it? I tried some possible solutions found on the web, but none of them worked.
    This code works if I define a data manualy, as it shows.

    Sub GetServices()
    With ActiveSheet.QueryTables.Add(Connection:=Array(Array("ODBC;DSN=vd_nacional;Description=vd_nacional;UID=opvd;PWD=opvd2011;APP=Microsoft Office 2003;WSID=REMOTESERVER2;DATABASE=VD_NACIONAL;Network=DBM"), Array("SSOCN")), Destination:=Range("A1"))
    .CommandText = Array("SELECT Tab_SERVICO.Cod_PA, Tab_SERVICO.Data_SRV" & Chr(13) & "" & Chr(10) & "FROM VD_Nacional.dbo.Tab_SERVICO Tab_SERVICO" & Chr(13) & "" & Chr(10) & "WHERE (Tab_SERVICO.Cod_PA Like '520%') AND (Tab_SERVICO.Data_SRV>={ts '2011-06-30 00:00:00'})" & Chr(13) & "" & Chr(10) & "ORDER BY T", "ab_SERVICO.Cod_PA")
    .Name = "Consulta de vd_nacional"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .Refresh BackgroundQuery:=False
    End With
    End Sub

    Any help would be apreciated, thanks

  2. #2
    Registered User
    Join Date
    06-29-2011
    Location
    Lisbon
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Getting actual date in SQL Query

    After all i was able to solve it after researching some more.
    I had to use a DateSerial and Format

    The solution:

    Sub GetServices()

    DataServico = DateSerial(Year(Now), Month(Now), Day(Now))
    DataServico = Format(DataServico, "YYYY-MM-DD HH:MM:SS")


    With ActiveSheet.QueryTables.Add(Connection:=Array(Array("ODBC;DSN=vd_nacional;Description=vd_nacional;UID=opvd;PWD=opvd2011;APP=Microsoft Office 2003;WSID=REMOTESERVER2;DATABASE=VD_NACIONAL;Network=DBM"), Array("SSOCN")), Destination:=Range("A1"))
    .CommandText = Array("SELECT Tab_SERVICO.Cod_PA, Tab_SERVICO.Data_SRV" & Chr(13) & "" & Chr(10) & "FROM VD_Nacional.dbo.Tab_SERVICO Tab_SERVICO" & Chr(13) & "" & Chr(10) & "WHERE (Tab_SERVICO.Cod_PA Like '520%') AND (Tab_SERVICO.Data_SRV>={ts '" & DataServico & "'})" & Chr(13) & "" & Chr(10) & "ORDER BY T", "ab_SERVICO.Cod_PA")
    .Name = "Consulta de vd_nacional"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .Refresh BackgroundQuery:=False
    End With
    End Sub


    Thanks anyway!



    Quote Originally Posted by DelmarPT View Post
    Good night everyone

    I'm trying to get the "now" date instead of a defined date [ (Tab_SERVICO.Data_SRV>={ts '2011-06-30 00:00:00'}) ] in the code below (in bold)
    How can I do it? I tried some possible solutions found on the web, but none of them worked.
    This code works if I define a data manualy, as it shows.

    Sub GetServices()
    With ActiveSheet.QueryTables.Add(Connection:=Array(Array("ODBC;DSN=vd_nacional;Description=vd_nacional;UID=opvd;PWD=opvd2011;APP=Microsoft Office 2003;WSID=REMOTESERVER2;DATABASE=VD_NACIONAL;Network=DBM"), Array("SSOCN")), Destination:=Range("A1"))
    .CommandText = Array("SELECT Tab_SERVICO.Cod_PA, Tab_SERVICO.Data_SRV" & Chr(13) & "" & Chr(10) & "FROM VD_Nacional.dbo.Tab_SERVICO Tab_SERVICO" & Chr(13) & "" & Chr(10) & "WHERE (Tab_SERVICO.Cod_PA Like '520%') AND (Tab_SERVICO.Data_SRV>={ts '2011-06-30 00:00:00'})" & Chr(13) & "" & Chr(10) & "ORDER BY T", "ab_SERVICO.Cod_PA")
    .Name = "Consulta de vd_nacional"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .Refresh BackgroundQuery:=False
    End With
    End Sub

    Any help would be apreciated, thanks

  3. #3
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Getting actual date in SQL Query

    Hi DelmarPT. I'm happy your figured out your problem but if you could please take the time (for the future of your posting) to read the forum rules located here and wrap your code with code tags as per Rule #3. Also, there is no need to quote full posts, especially your own.

    Many Thanks & Kindest Regards:
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1