Solved

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

Posted on 2009-05-19
7
660 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month4 days, 3 hours left to enroll

630 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