Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VBA & SQL:  Passing strings in the Where clause

Posted on 2004-10-15
7
Medium Priority
?
659 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
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!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

636 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