Solved

sql query please help..very urgent

Posted on 2006-06-27
2
257 Views
Last Modified: 2013-12-25
My program will call  a crystal report template if my sql query count * > 0 but even if my count * is 0, still program going into the    If loop-- and calling crysal template which is not supposed to do  so..

Pls help..

Dim rsUpdateROHS As ADODB.Recordset
     Call ReadDBSetting
     Set CONNOW = New ADODB.Connection
     CONNOW.Open gstrContStr
     Set rsUpdateROHS = New ADODB.Recordset
     
     strSQL = "select  count(*) from approvedcountries where countries in (select ShipTo from LoadPlan WHERE   DJFile_Name ='" & Trim(txtDJFile) & "')"

     rsUpdateROHS.Open strSQL, CONNOW, adOpenStatic, adLockOptimistic

     If Not rsUpdateROHS.EOF Then                    ''Eventhough my select count * is 0 still program going to crytal template

      CRAck.Reset
      CRAck.ReportFileName = App.Path & "\Report\ROHS1.rpt"
      CRAck.Connect = CONNOW
      CRAck.Destination = crptToWindow
      'CRAck.WindowState = crptMaximized
      CRAck.Action = 1

      rsUpdateROHS.Close
      Set rsUpdateROHS = Nothing
     
      Else        ----------Should go else or end sub of count * is 0
      End Sub
     
0
Comment
Question by:nyee84
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 300 total points
ID: 16998540
 '>change the sql to give the count(*) a name:

  strSQL = "select  count(*) counter from approvedcountries where countries in (select ShipTo from LoadPlan WHERE   DJFile_Name ='" & Trim(txtDJFile) & "')"

     rsUpdateROHS.Open strSQL, CONNOW, adOpenStatic, adLockOptimistic

     If rsUpdateROHS.fields("counter").value > 0 Then                    '< change here to use that field value.

      CRAck.Reset
      CRAck.ReportFileName = App.Path & "\Report\ROHS1.rpt"
      CRAck.Connect = CONNOW
      CRAck.Destination = crptToWindow
      'CRAck.WindowState = crptMaximized
      CRAck.Action = 1

      rsUpdateROHS.Close
      Set rsUpdateROHS = Nothing
     
      Else        ----------Should go else or end sub of count * is 0
      End Sub


select count(*) will return ALWAYS 1 line. always except when the statement fails (which would go to the error handling)
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 200 total points
ID: 16998984
add a having clause to not generate a row when the reult is zero...
but basically you should probably just check that the value returned isn't zero and bypass the report generation that way...

 strSQL = "select  count(*) from approvedcountries where countries in (select ShipTo from LoadPlan WHERE   DJFile_Name ='" & Trim(txtDJFile) & "') having count(*) <> 0 "
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

705 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