Solved

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

Posted on 2009-05-06
4
699 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now