Link to home
Start Free TrialLog in
Avatar of fcoit
fcoitFlag for United States of America

asked on

VBA & SQL: Passing strings in the Where clause

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
Avatar of stevbe
stevbe

AND (Not IsNull([scripts.PriorAuth_PAMC]))

Steve
Steve's way should work. You could also try:

AND ((scripts.PriorAuth_PAMC) Is Not Null)
Avatar of fcoit

ASKER

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
... all previous code

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

Steve
ASKER CERTIFIED SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fcoit

ASKER

Arthur,

Thanks it worked... "Life is so simple we make it hard"
glad to be of assistance

AW