Solved

Excel VBA Query

Posted on 2014-02-12
2
386 Views
Last Modified: 2014-02-13
Is it possible to better rewrite the query in Excel VBA using parameter query or other better way to handle multiple parameters, Tks

'Retrieve record from SQL Server
Set cnt = New ADODB.Connection
    

stADO = "Provider=SQLOLEDB.1;" & _
        "Data Source=xxxx;Initial Catalog=Dynamics_BAK;User ID=report;Password=123"

    
With cnt
    .CursorLocation = adUseClient
    .Open stADO
    .CommandTimeout = 0
End With


'SQL Statement
stSQL = "select  * from WMSORDERTRANS where SHIPMENTID = 'A10009480'  order by linenum"

Set rst = cnt.Execute(stSQL)
    

If rst.RecordCount <> 0 Then
    rst.MoveFirst
    Do
        MsgBox rst("itemid")
        rst.MoveNext
    Loop Until rst.EOF
    rst.Close
End If

Open in new window

0
Comment
Question by:AXISHK
2 Comments
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 39856381
You can construct a list of values and use the IN () clause.  I assume that you would pull these values from your worksheet.
Dim strShipIDs As String
strShipIDs = "'A10009480'"
strShipIDs = strShipIDs & ", " & "'A10009481'"
strShipIDs = strShipIDs & ", " & "'A10009482'"
strShipIDs = "(" & strShipIDs & ")"

'SQL Statement
stSQL = "select  * from WMSORDERTRANS where SHIPMENTID In " & strShipIDs & "  order by linenum"

Open in new window


Also, you can use a range of values with the Between clause:
'SQL Statement
stSQL = "select  * from WMSORDERTRANS where SHIPMENTID Between 'A10009480' And 'A10009482' order by linenum"

Open in new window

0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

770 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