Results 1 to 12 of 12

Thread: Why does this if statement not work?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Why does this if statement not work?

    Hi guys, I am using the current code below, now i'm trying to make it search the database for what is typed in text1.text and then when it finds the results it adds them to listview4.

    For some reason when i search it comes back with 'No results found' (The else statement)
    even though it is defo in the database.

    Anyone have any ideas why this is happening?

    thanks
    jamie

    Code:
     Dim db As Database
    Dim rs As Recordset
    Dim ds As Recordset
    Dim WS As Workspace
    
    Dim dbfile As Variant
    Dim pwdstring As Variant
    
     Set WS = DBEngine.Workspaces(0)
        dbfile = ("\\server\Database\maindb.mdb")
    pwdstring = "C123"
    Set db = DBEngine.OpenDatabase(dbfile, False, False, ";PWD=" & pwdstring)
    
    Set rs = db.OpenRecordset("SELECT * FROM `Leads` WHERE `Company Name` LIKE UCASE('%" & Text1.Text & "%') OR `Contact Name` LIKE UCASE('%" & Text1.Text & "%') OR `Phone Number` LIKE UCASE('%" & Text1.Text & "%') OR `Contact Number2` LIKE UCASE('%" & Text1.Text & "%') OR `Mobile` LIKE UCASE('%" & Text1.Text & "%')")
    
    
    
    If Not (rs.EOF And rs.BOF) Then
    
    
    Do While Not rs.EOF
    
    
    
    
    ListView1.ListItems.Clear
    
    
    
    Dim lvwItem8 As ListItem
    
             
      
          
       
            Set lvwItem8 = ListView1.ListItems.Add(, , rs.Fields.Item("Contact Name").value)
            lvwItem8.SubItems(1) = rs.Fields.Item("Company Name").value
           
    lvwItem8.SubItems(2) = rs.Fields.Item("Status").value
           
    lvwItem8.SubItems(3) = rs.Fields.Item("Phone Number").value
    
    
    lvwItem8.SubItems(4) = rs.Fields.Item("leadid").value
                 
    
    
    
    lvwItem8.SubItems(5) = rs.Fields.Item("Calling From").value
                 
    
    
                
        rs.MoveNext
    Loop
    rs.Close
    
      Else
    MsgBox "No results found"
        
        End If

  2. #2
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Why does this if statement not work?

    Take the SQL as it would be converted when it is passed to SQL and see if it works in Native SQL (Enterprise manager in my case). It would be easier to do that if you changed it to:

    Dim strSQL As string
    strSQL = "("SELECT * FROM `Leads` WHERE `Company Name` LIKE UCASE('%" & Text1.Text & "%') OR `Contact Name` LIKE UCASE('%" & Text1.Text & "%') OR `Phone Number` LIKE UCASE('%" & Text1.Text & "%') OR `Contact Number2` LIKE UCASE('%" & Text1.Text & "%') OR `Mobile` LIKE UCASE('%" & Text1.Text & "%')")"

    Set rs = db.OpenRecordset(strSQL)

    Then get the SQL being passed by going to the immeadiate window and typing:

    ?strSQL

    Cut and paste that into native SQL and run it.

    I'm sure you'll find that even though the data is there as you say your query is not retreiving it.

  3. #3
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Why does this if statement not work?

    Perhaps you also need to Uppercase the Column(s) data as well eg.
    Code:
    strSQL = "SELECT * FROM Leads WHERE " & _
                "UPPER( [Company Name])      LIKE '%" & UCase(Text1.Text) & "%' OR " & _
                "UPPER( [Contact Name])      LIKE '%" & UCase(Text1.Text) & "%' OR " & _
                "UPPER( [Phone Number])      LIKE '%" & UCase(Text1.Text) & "%' OR " & _
                "UPPER( [Contact Number2])   LIKE '%" & UCase(Text1.Text) & "%' OR " & _
                "       [Mobile]             LIKE '%" & Text1.Text & "%'"
    Last edited by Doogle; Aug 16th, 2012 at 08:18 AM.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Why does this if statement not work?

    Ahh.... I think it's your wild cards... Access, uses the asterisk ... * for its wildcard... so it should be " LIKE '*" & ...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Why does this if statement not work?

    Nice catch tg.

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Why does this if statement not work?

    It sticks out to me because I had the opposite problem many moons ago.... going from Access's * to SQL Server's % .... many... many moons ago...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Why does this if statement not work?

    Hi Doogle, I get the error Undefined function 'UPPER' in expression.

    Any ideas why?

    Thanks
    Jamie

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Why does this if statement not work?

    On the code



    Set rs = db.OpenRecordset("SELECT * FROM Leads WHERE " & _
    "UPPER( [Company Name]) LIKE '%" & UCase(Text1.Text) & "%' OR " & _
    "UPPER( [Contact Name]) LIKE '%" & UCase(Text1.Text) & "%' OR " & _
    "UPPER( [Phone Number]) LIKE '%" & UCase(Text1.Text) & "%' OR " & _
    "UPPER( [Contact Number2]) LIKE '%" & UCase(Text1.Text) & "%' OR " & _
    "[Mobile] LIKE '%" & Text1.Text & "%'")

  9. #9
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Why does this if statement not work?

    Because I got it wrong, in SQL Server UPPER is the Function to Upper Case something in Access it's UCase.
    Change UPPER to UCase and it should be OK

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Why does this if statement not work?

    For some reason, the error goes but i still get the else statement popup saying no results found...

    Any ideas?

  11. #11
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Why does this if statement not work?

    Read Post #4 and change the % characters to * characters

  12. #12
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Why does this if statement not work?

    Read post #2 and save yourself some time. Basically get it (the SQL) working in a native environment .where you can just run the query, and when that works plug it into your program.

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