Solved

VBA & SQL:  Passing strings in the Where clause

Posted on 2004-10-15
7
547 Views
Last Modified: 2012-08-13
I need to add an additional statement to the where clause depending on whether the user checks a box or not.
The additional statement is as follows:

AND scripts.PriorAuth_PAMC IS NOT NULL

I am having problems passing the "IS NOT NULL" any hints will be greatly appreciated.
======================================================================

Dim mydb As Database, myqd As QueryDef, strSQL As String
Dim strScripts As String, dtBegDate As String, dtEndDate As String, sClass As String
Dim ChrRet As String, dbLocScriptMbr As String, sSQL As String, myrs As Recordset
Dim qdReportSet As QueryDef, qSelectedNabp As Recordset
Dim DocName As String
Dim bAnd As Boolean
Dim myRecSet As Recordset
Dim sPriorAuth As String

strScripts = "Scripts_" & [Forms]![frmzClient]![strClientId]
dtBegDate = [Forms]![frmGetMonthlyReportDates]![dtmRxStartDate]
dtEndDate = [Forms]![frmGetMonthlyReportDates]![dtmRxStopDate]
dbLocScriptMbr = [Forms]![frmzClient]![txtdbLocScriptMbr] & ".dbo."
sClass = "Instore"
ChrRet = Chr$(13) & Chr$(10)

Set mydb = CurrentDb()

sSQL = "SET NOCOUNT ON" + ChrRet
sSQL = sSQL + "SET ANSI_WARNINGS OFF" + ChrRet
sSQL = sSQL & "DECLARE @ClientId Char(2) , @dtBegDate datetime, @dtEndDate datetime, @strNabp Char(7), @strClass Char(7), @strPriorAuth Char(11) " + ChrRet
sSQL = sSQL & "SET @ClientId = '" + [Forms]![frmzClient]![strClientId] + "'" + ChrRet
sSQL = sSQL & "SET @dtBegDate = '" + dtBegDate + "'" + ChrRet
sSQL = sSQL & "SET @dtEndDate = '" + dtEndDate + "'" + ChrRet
sSQL = sSQL & "SET @strClass = '" + sClass + "'" + ChrRet
sSQL = sSQL + ChrRet

sSQL = sSQL & "SELECT DISTINCT scripts.Nabp, scripts.RxNum, scripts.RxDate, drug.PackageSize, drug.DrugName, scripts.Qty," + ChrRet
sSQL = sSQL & "       scripts.DaySupply, scripts.PriorAuth_PAMC, scripts.DAW, scripts.IngrCostS, scripts.AmtDue, scripts.Account," + ChrRet
sSQL = sSQL & "       scripts.PlanNum, scripts.Pat_ID, scripts.Person_Code, scripts.Ndc_Mfg, scripts.Ndc_Prod, scripts.Ndc_Pkg," + ChrRet
sSQL = sSQL & "       scripts.CostBasis, scripts.Phy_ID, scripts.AuditFlag, scripts.Cost_Ind, scripts.Refill_Code, " + ChrRet
sSQL = sSQL & "       AuditRequestData.Code, AuditRequestData.lngCounter, " + ChrRet
sSQL = sSQL & "       scripts.Counter, scripts.PriorAuthFlag, Pharmacy.Name AS PName, Pharmacy.Store_Nbr, Pharmacy.Address1," + ChrRet
sSQL = sSQL & "       Pharmacy.Address2, Pharmacy.City, Pharmacy.State," + ChrRet
sSQL = sSQL & "       CASE When Len(LTrim(RTrim(ZipCode))) = 9 Then Left(ZipCode , 5) + '-' + Right(RTrim(ZipCode),4)" + ChrRet
sSQL = sSQL & "            When Len(LTrim(RTrim(ZipCode))) = 5 Then ZipCode END AS Zip," + ChrRet
sSQL = sSQL & "       '(' + Left(Phone_Nbr,3) + ') ' + Substring(Phone_Nbr,4,3) + '-' + Right(RTrim(Phone_Nbr),4) AS Phone" + ChrRet
sSQL = sSQL & "  FROM " + dbLocScriptMbr & strScripts + " AS scripts " + ChrRet
sSQL = sSQL & "  LEFT JOIN FDB_drugs As drug" + ChrRet
sSQL = sSQL & "    ON scripts.Ndc_Mfg = drug.NDC_Mfg" + ChrRet
sSQL = sSQL & "   AND scripts.Ndc_Prod = drug.NDC_Prod" + ChrRet
sSQL = sSQL & "   AND scripts.Ndc_Pkg = drug.NDC_Pkg" + ChrRet
sSQL = sSQL & "  LEFT JOIN Pharmacy" + ChrRet
sSQL = sSQL & "    ON scripts.Nabp = Pharmacy.Nabp" + ChrRet
sSQL = sSQL & "  LEFT JOIN AuditRequestData" + ChrRet
sSQL = sSQL & "    ON scripts.Counter = AuditRequestData.lngCounter" + ChrRet
sSQL = sSQL & "   AND scripts.ClientId = AuditRequestData.ClientID" + ChrRet
sSQL = sSQL & "   AND AuditRequestData.AuditClass = @strClass" + ChrRet
sSQL = sSQL & " WHERE scripts.RxDate Between @dtBegDate And @dtEndDate " + ChrRet
sSQL = sSQL & "   AND scripts.BalanceOut = 0 " + ChrRet
sSQL = sSQL & "   AND scripts.ClientId = @ClientId " + ChrRet
0
Comment
Question by:fcoit
[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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 39

Expert Comment

by:stevbe
ID: 12323686
AND (Not IsNull([scripts.PriorAuth_PAMC]))

Steve
0
 
LVL 4

Expert Comment

by:Gal_atea
ID: 12323785
Steve's way should work. You could also try:

AND ((scripts.PriorAuth_PAMC) Is Not Null)
0
 

Author Comment

by:fcoit
ID: 12323798
Let me explain a little bit more.  I want this statement to be included if the user checks the 'chkPriorAuth' check box so how can I control that.  Make it come on and off.  So your suggestion will not work.  Thanks for your input.

sSQL = sSQL & "   scripts.PriorAuth_PAMC IS NOT NULL" + ChrRet
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 39

Expert Comment

by:stevbe
ID: 12323913
... all previous code

If Me.chkPriorAuth.Value = True Then
    sSQL = sSQL & "   scripts.PriorAuth_PAMC IS NOT NULL" + ChrRet
End If

Steve
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 500 total points
ID: 12323964
at the end of your code:

sSQL = sSQL & "   AND scripts.ClientId = AuditRequestData.ClientID" + ChrRet
sSQL = sSQL & "   AND AuditRequestData.AuditClass = @strClass" + ChrRet
sSQL = sSQL & " WHERE scripts.RxDate Between @dtBegDate And @dtEndDate " + ChrRet
sSQL = sSQL & "   AND scripts.BalanceOut = 0 " + ChrRet
sSQL = sSQL & "   AND scripts.ClientId = @ClientId " + ChrRet

add this:

If chkPriorAuth.Value = True then
   ' check box IS CHecked
   sSQL = sSQL & "AND scripts.PriorAuth_PAMC Is Not Null"
End if

AW
0
 

Author Comment

by:fcoit
ID: 12324030
Arthur,

Thanks it worked... "Life is so simple we make it hard"
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12325550
glad to be of assistance

AW
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

734 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