Results 1 to 17 of 17

Thread: WHERE Statement with 2 criteria

  1. #1

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    WHERE Statement with 2 criteria

    I am attempting to have the following SQL Query find a records using the field PatID, then of those only give me the records where the contents of the field Disabled is NOT EQUAL to the letter Y.

    The statement below doesnt seem to be working correctly.

    VB Code:
    1. sSQL = "select vaccine,manufacturer,dov,lotnum,exp,docname,type,Dose,insurance,Disabled from vfcrec Where PatID like '" & Text2.Text & "' and Disabled <> 'Y' order by vaccine"

    Any help would be appreciated. Thanks in Advance.

    EDIT: I just attepted to subsititue the 'Y' with chr$(89) and what happens now is even though the operator is <> (which i believe means NOT EQUAL) The Output is showing me records that DO EQUAL Y....

    Im confused...
    Last edited by DKasler; Jan 14th, 2005 at 12:59 AM.
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: WHERE Statement with 2 criteria

    try IS NOT. i think that is right...
    maybe NOT =
    Last edited by dglienna; Jan 14th, 2005 at 12:28 AM.

  3. #3

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: WHERE Statement with 2 criteria

    Nah, that didnt work.

    According to the MSDN Lib:
    <> Not equal to
    Any other suggestions?
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: WHERE Statement with 2 criteria

    Put in a breakpoint, and look at sSQL to make sure that it is what you want it to be. There may be something else wrong with it. Put in a
    debug.print sSQL

    I got this back:

    Code:
    select vaccine,manufacturer,dov,lotnum,exp,docname,Type,Dose,insurance,Disabled from vfcrec Where PatID like 'Text2' And Disabled <> 'Y' order by vaccine
    I don't think the variable is right, and you may need to be using """ instead of '
    Last edited by dglienna; Jan 14th, 2005 at 12:50 AM.

  5. #5

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: WHERE Statement with 2 criteria

    sSQL is just a string value...

    Here is the full set of code:

    VB Code:
    1. Dim sConnect As String
    2.     Dim sSQL As String
    3.     Dim dfwConn As ADODB.Connection
    4.  
    5.     ' set strings
    6.     sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Password='';User ID=Admin;Data Source=Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & frmLocation.Text1.Text & "';Persist Security Info=False;Mode=Share Deny None;Extended Properties='';Jet OLEDB:System database='';Jet OLEDB:Registry Path='';Jet OLEDB:Database Password='';Jet OLEDB:Engine Type=4;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password='';Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False"
    7.     sSQL = "select vaccine,manufacturer,dov,lotnum,exp,docname,type,Dose,insurance,Disabled from vfcrec Where PatID like '" & Text2.Text & "' and Disabled <>  '" & Chr$(89) & "' order by vaccine"
    8.    
    9.     ' open connection
    10.     Set dfwConn = New Connection
    11.     dfwConn.Open sConnect
    12.  
    13.     ' create a recordset using the provided collection
    14.     Set datPrimaryRS = New Recordset
    15.     datPrimaryRS.CursorLocation = adUseClient
    16.     datPrimaryRS.Open sSQL, dfwConn, adOpenForwardOnly, adLockReadOnly
    17.  
    18.     Set MSHFlexGrid2.DataSource = datPrimaryRS
    Last edited by DKasler; Jan 14th, 2005 at 12:53 AM.
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: WHERE Statement with 2 criteria

    isn't Type a reserved word? I edited the post above

  7. #7

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: WHERE Statement with 2 criteria

    Ive used 'type' as a field name in this expression for months and its worked fine...

    just now im trying to add the
    VB Code:
    1. and Disabled <>  '" & Chr$(89) & "'

    and thats whats not working. Until I added that the statement did exactly what it was suposed to.
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

  8. #8
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: WHERE Statement with 2 criteria

    I got this from Access 2000

    Code:
    SELECT Call.ID, Call.Field1
    FROM Call
    WHERE (((Call.Field1)<>"Y"));
    I don't have SQL Server.

  9. #9

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: WHERE Statement with 2 criteria

    Quote Originally Posted by dglienna
    I don't have SQL Server.
    I dont either. Im pulling the data from an Access DB
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

  10. #10
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770

    Re: WHERE Statement with 2 criteria

    Quote Originally Posted by DKasler
    Ive used 'type' as a field name in this expression for months and its worked fine...

    just now im trying to add the
    VB Code:
    1. and Disabled <>  '" & Chr$(89) & "'

    and thats whats not working. Until I added that the statement did exactly what it was suposed to.

    Uh, watch out for reserved words..... type and disabled are most likely reserved words. Try putting brackets [] around the field name.

  11. #11
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770

    Re: WHERE Statement with 2 criteria

    Quote Originally Posted by DKasler
    I dont either. Im pulling the data from an Access DB
    If you dont have the money to fork over for SQL Server, MySQL can be an option for you. Sure beats not having to use Access any day.

  12. #12
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: WHERE Statement with 2 criteria

    Try designing a new query

  13. #13

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: WHERE Statement with 2 criteria

    Ok..

    VB Code:
    1. and Disabled = Chr$(89) order by vaccine"

    DOES EXACTLY WHAT IT SHOULD... Finds records where the Field Disabled IS EQUAL to Chr$(89)

    If I change the = to <> the query stops finding records and MOST of the records in the table <> Chr$(89) (all but 1 record are actually blank in the Disabled field)

    I dont believe this has anything to do with Reserved words because if it did the query wouldnt work with an = either...
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

  14. #14
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: WHERE Statement with 2 criteria

    I assume Disabled is a text field. What possible values can this field hold? Y and N? Is it possible that there are NULL values? That can cause problems. Try something like:
    VB Code:
    1. Where [color=red]([/color][PatID] like '" & Text2.Text & "'[color=red])[/color] and [color=red]([/color][color=red]Not[/color] [Disabled] [color=red]=[/color] 'Y'[color=red])[/color] [color=black]order by vaccine"[/color]
    If that doesn't work, you may need to test for [Disabled] Is Null also.
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  15. #15

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: WHERE Statement with 2 criteria

    I got it more or less worked out. I ran an update query changing any field not EQUAL to Y to N

    Your right... it was probably NULL working its way in there.

    Thanks Much
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

  16. #16
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: WHERE Statement with 2 criteria

    Glad it's working. I hope I helped some. I am trying to learn also.

  17. #17
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: WHERE Statement with 2 criteria

    Just wanted to make sure you were aware that LIKE requires a wildcard in the lookup text. Not sure what you are typing in the TEXT BOX - but to use LIKE you must also have a wildcard character.

    We use MS SQL - it would be:

    WHERE PATID LIKE '%SMITH%'

    Also LIKE is for character data - not numeric data types.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width