Solved

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

Posted on 2009-05-19
7
645 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

735 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