Solved

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

Posted on 2009-05-19
7
616 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

911 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