Solved

Get data from excel cell and use it in VB SQL query

Posted on 2009-05-19
7
604 Views
Last Modified: 2013-12-25
Hi, I'm trying to get a data from excel, store it in some declared name, then use that name as part of SQL query, i think i got the format wrong in the query.

The 2 declared names to be used is totalrow and SalesIDLB. Can any1 see what is wrong with the codes? (look inside the With End query, the mistake should be at those 2 names coz when i replaced them with number, it works) .

Thank you

Sub KKDP()

    Dim totalrow As Integer

    totalrow = Range("H6").Value

    SaleIDLB = Range("J" & totalrow).Value

    

    Do Until SaleIDLB = ""

    

        With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _

            "ODBC;DSN=SB MYOB 2009;", Destination:=Range("S3" & totalrow)).QueryTable

            .CommandText = Array( _

            "SELECT ItemSaleLines.TaxExclusiveTotal" & Chr(13) & "" & Chr(10) & "FROM ItemSaleLines ItemSaleLines" & Chr(13) & "" & Chr(10) & "WHERE (ItemSaleLines.SaleID=SaleIDDB) AND (ItemSaleLines.ItemID=77)" _

            )

            .RowNumbers = False

            .FillAdjacentFormulas = False

            .PreserveFormatting = True

            .RefreshOnFileOpen = False

            .BackgroundQuery = True

            .RefreshStyle = xlInsertDeleteCells

            .SavePassword = False

            .SaveData = True

            .AdjustColumnWidth = True

            .RefreshPeriod = 0

            .PreserveColumnInfo = True

            .ListObject.DisplayName = "KKDPLB"

            .Refresh BackgroundQuery:=False

        End With

        

        Range("KKDPLB[[#Headers],[TaxExclusiveTotal]]").Select

        ActiveSheet.ListObjects("KKDPLB").Unlist

        Selection.ClearContents

        Range("S" & totalrow).Select

        Selection.Cut

        Range("Q" & totalrow).Select

        ActiveSheet.Paste

    

        totalrow = totalrow + 1

        SaleIDLB = Range("J" & totalrow).Value

    Loop

   

End Sub

Open in new window

0
Comment
Question by:MachoLinKz
  • 4
  • 3
7 Comments
 
LVL 5

Expert Comment

by:TheVeee
ID: 24420751
Been awhile but your sql string looks weird.  Looks like your trying to do some join between two tables but reading only one?  I pull your sql out first and make it simpler if your using only one table and if your using two tables, your missing one..

What I pulled from your code...
SELECT ItemSaleLines.TaxExclusiveTotal" & Chr(13) & "" & Chr(10) & "FROM ItemSaleLines ItemSaleLines" & Chr(13) & "" & Chr(10) & "WHERE (ItemSaleLines.SaleID=SaleIDDB) AND (ItemSaleLines.ItemID=77)  

to me should be

SELECT TaxExclusiveTotal FROM ItemSaleLines WHERE SaleID=SaleIDDB AND ItemID=77  

Is saleID and SaleIDDB in the same database?  If not heres what you sql should be saying table B is the second table...

SELECT A.TaxExlusiveTotal From ItemSaleLines A, SecondTable B where A.SaleID = B.SaleIDDB and A.ItemID = 77



0
 

Author Comment

by:MachoLinKz
ID: 24428435
That weird SQL strings are made by the excel macro recorder.
The simple SQL I used to record it is

SELECT TaxExclusiveTotal
FROM ItemSaleLines
WHERE ItemSaleLines.SaleID = 100 AND ItemSaleLines.ItemID = 77

Now, SaleIDDB is the defined name in VB i used to store the value i get from Excel's cell. In the example above, it works when i just put plain number like 100, but it gives me error when i swap it with 'SaleDBLB'.

So it's more about the format i should use in VB, any idea?

0
 

Author Comment

by:MachoLinKz
ID: 24428439
upzz, it's SaleIDLB, I mispelled it wrong twice
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 5

Expert Comment

by:TheVeee
ID: 24430649
ok, thats a start.  Field SaleIDDB is probably defined not as numeric and is a variant.  Try to see if there a .value you have to pass to back of it.  I havent done writing in Excel queries except thru Visual basic or java in awhile, but should be similiar.

SaleIDDB.value
0
 

Author Comment

by:MachoLinKz
ID: 24447993
Sorry for late reply, it does have a value returned. I tried assigning a value to it, then putting it in range S34. It returns 105 which is right.

    Range("s34") = SaleIDLB
0
 

Accepted Solution

by:
MachoLinKz earned 0 total points
ID: 24448270
It's ok i've figured it out, i changed the format in the SQL query to WHERE (ItemSaleLines.SaleID='" & Format(SaleIDLB) & "')

Thank You
0
 
LVL 5

Expert Comment

by:TheVeee
ID: 24449737
cool.... Have a good Memorial Weekend!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

760 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

20 Experts available now in Live!

Get 1:1 Help Now