Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-05-19
7
Medium Priority
?
670 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
[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
  • 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
Technology Partners: 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

Technology Partners: 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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

721 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