We help IT Professionals succeed at work.
Get Started

EXCEL (SQL SENTENCE TO VBA)

478 Views
Last Modified: 2013-11-24
Hello Experts, I did a Connection to Excel Sheet and AS/400 Databese with VBA and the main purpose is to get the summary by dates, the codes works well, but when I add an other field (this is the field that I add "FKITSAVE.TSPN" is the part Nr), the Macro send a mistake to ODBC, I want to know the quantity issued by part, but just the summary.

AS/400 Databese has:
Part Nr     Qty Issued     Date
A1                   100         01/01/2010
A1                   106         02/01/2010
A1                   109         03/01/2010
A1                   100         04/01/2010
A1                   108         05/01/2010

The summary that I need is:

Part Nr     Qty Issued
A1                   523

This Is my CODE:

Sub IssKBM()

Dim qryTable As QueryTable
Dim rngDestination As Range
Dim strConnection As String
Dim strSQL As String
Dim tpe As String


'Clean File
Sheets("Issues").Select
Range("A1").Select
Selection.CurrentRegion.Select
Selection.EntireColumn.Delete
Range("A1").Select

'Connection to AS/400 (KBM)

strConnection = "ODBC;DSN=AS/400-2;"

Set rngDestination = ActiveSheet.Range("A1")

'Start SQL

strSQL = "SELECT FKITSAVE.TSPN, SUM(FKITSAVE.TSTQTY)" & Chr(13) & "" & Chr(10) & _
        "FROM S10663DC.KBM500MFG.FKITSAVE FKITSAVE" & Chr(13) & "" & Chr(10) & _
        "WHERE (FKITSAVE.TSCO=001) AND (FKITSAVE.TSDATE>1091201) AND (FKITSAVE.TSCODE='13')" & Chr(13) & "" & Chr(10) & _
        "ORDER BY FKITSAVE.TSPN"
       
'Start Qry

Set qryTable = ActiveSheet.QueryTables.Add(strConnection, rngDestination)

qryTable.CommandText = strSQL
qryTable.Refresh False

End Sub

I hope so someone can help me :(
Comment
Watch Question
Grand Poobah
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2011
Commented:
This problem has been solved!
Unlock 2 Answers and 13 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE