|
-
Aug 16th, 2012, 06:49 AM
#1
Thread Starter
Fanatic Member
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
-
Aug 16th, 2012, 07:17 AM
#2
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.
-
Aug 16th, 2012, 08:12 AM
#3
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.
-
Aug 16th, 2012, 08:50 AM
#4
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
-
Aug 16th, 2012, 09:16 AM
#5
Re: Why does this if statement not work?
-
Aug 16th, 2012, 10:18 AM
#6
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
-
Aug 17th, 2012, 05:43 AM
#7
Thread Starter
Fanatic Member
Re: Why does this if statement not work?
Hi Doogle, I get the error Undefined function 'UPPER' in expression.
Any ideas why?
Thanks
Jamie
-
Aug 17th, 2012, 05:44 AM
#8
Thread Starter
Fanatic Member
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 & "%'")
-
Aug 17th, 2012, 06:12 AM
#9
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
-
Aug 17th, 2012, 06:17 AM
#10
Thread Starter
Fanatic Member
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?
-
Aug 17th, 2012, 06:43 AM
#11
Re: Why does this if statement not work?
Read Post #4 and change the % characters to * characters
-
Aug 17th, 2012, 06:47 AM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|