?
Solved

VBA & SQL:  Passing strings in the Where clause

Posted on 2004-10-15
7
Medium Priority
?
606 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

752 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