Solved

Excel VB SQL, get date from Excel for SQL query in VB

Posted on 2009-05-06
4
726 Views
Last Modified: 2013-12-26
Hi, pardon my english. I'm having problem getting a date from an excel column, and then use it in VB as a condition query in SQL.
This is what I've tried, let's say the date is in column A1, the value is '2009-05-06'
It will work if I done it manually in VB, under the SQL query:
WHERE Sales.InvoiceDate= '2009-05-06'

So I tried getting the date from excel and store it in an object (what does it call)
Dim DateExcel as Date
DateExcel = cell(1,1)
WHERE Sales.InvoiceDate= DateExcel

Couldn't work, so I tried 1 step back
Dim DateExcel as Date
DateExcel = "2009-05-06"
WHERE Sales.InvoiceDate= DateExcel

Couldn't work either. I tried changing the DateExcel to string, declaring the DateExcel on top (declarations), nothing works.

I use Excel macro recorder to get the VB lines for getting data from ODBC, i'll attach the recorded code below. I have little knowledge about the formats so I go by trial and errors, so there might be lots of mistakes in what i'm trying to do. Please point out where was I wrong.
Sub Macro17()
'
' Macro12 Macro
'
Dim DateExcel As Date
DateExcel = "2009-05-06"
 
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "ODBC;DSN=SB MYOB 2009;", Destination:=Range("$C$4")).QueryTable
        .CommandText = Array( _
        "SELECT Sales.SaleID, Sales.InvoiceDate AS 'Date', Sales.InvoiceNumber AS 'Sales No', Items.ItemNumber AS 'Type', Sales.ShipToAddress AS 'Finance', Sales.TotalLines AS 'Price', Sales.TotalPaid AS 'Cash" _
        , _
        " DP'" & Chr(13) & "" & Chr(10) & "FROM Items Items, ItemSaleLines ItemSaleLines, Jobs Jobs, Sales Sales" & Chr(13) & "" & Chr(10) & "WHERE (Jobs.JobNumber='SLB') AND (ItemSaleLines.SaleID=Sales.SaleID) AND (Jobs.JobID=ItemSaleLines.JobID) AND (Items.ItemID" _
        , _
        "=ItemSaleLines.ItemID) AND (Items.ItemIsInventoried='Y') AND (Sales.InvoiceDate= nenenku )" & Chr(13) & "" & Chr(10) & "ORDER BY Sales.SaleID" _
        )
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "Table_Query_from_SB_MYOB_2009_1"
        .Refresh BackgroundQuery:=False
    End With
End Sub

Open in new window

0
Comment
Question by:MachoLinKz
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 24312942
If your cell contains an actual date, rather than text, you can use:
 

Sub Macro17()
'
' Macro12 Macro
'
Dim DateExcel As Date
' change range reference as required
DateExcel = Range("A1").Value 
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "ODBC;DSN=SB MYOB 2009;", Destination:=Range("$C$4")).QueryTable
        .CommandText = Array( _
        "SELECT Sales.SaleID, Sales.InvoiceDate AS 'Date', Sales.InvoiceNumber AS 'Sales No', Items.ItemNumber AS 'Type', Sales.ShipToAddress AS 'Finance', Sales.TotalLines AS 'Price', Sales.TotalPaid AS 'Cash" _
        , _
        " DP'" & Chr(13) & "" & Chr(10) & "FROM Items Items, ItemSaleLines ItemSaleLines, Jobs Jobs, Sales Sales" & Chr(13) & "" & Chr(10) & "WHERE (Jobs.JobNumber='SLB') AND (ItemSaleLines.SaleID=Sales.SaleID) AND (Jobs.JobID=ItemSaleLines.JobID) AND (Items.ItemID" _
        , _
        "=ItemSaleLines.ItemID) AND (Items.ItemIsInventoried='Y') AND (Sales.InvoiceDate='" & Format(DateExcel, "yyyy-mm-dd") & "') " & Chr(13) & "" & Chr(10) & "ORDER BY Sales.SaleID" _
        )
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "Table_Query_from_SB_MYOB_2009_1"
        .Refresh BackgroundQuery:=False
    End With
End Sub

Open in new window

0
 
LVL 25

Expert Comment

by:reb73
ID: 24312973
You have to cast ExcelDate to character data and enclose within single quotes like -

"WHERE Sales.InvoiceDate= '" & format(DateExcel, "yyyy-mm-dd") & "'"

0
 

Author Closing Comment

by:MachoLinKz
ID: 31578376
Spot On! Thanks a bunch Ror!~
0
 

Author Comment

by:MachoLinKz
ID: 24313029
Thanks for the input guys~ Saved me hours of looking around
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel User Form VBA Help 18 32
Excel Spacing Anomaly 4 24
Why do my Excel files become huge? 27 34
Return Column Number based on a specific value 25 26
A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question