Solved

VBA & SQL:  Passing strings in the Where clause

Posted on 2004-10-15
7
510 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
  • 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

760 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