Solved

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

Posted on 2009-05-06
4
754 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

732 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