473,394 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

simple query

284 100+
i have a simple query and does not getting desire results which i want
i am using vb6 and access
i had a table with with 8 fields but just to simplyfy by question i am just
supposing to four.

suppose i had a table with field


Name----------City----------Type----------Value

Property1-----Karachi-----1-------------- 100
Property2---- Lahore------ 3-------------- 250
Property3----- Karachi---- 1------------- 50


Now, i had made a form to search the desired criteria
and the field which we can choose searh are

City Karachi
Type (Blank)
Value <200

and i am using the following query

Expand|Select|Wrap|Line Numbers
  1.  
  2. "select * from property where city='" & citytextbox & "' and type ='" & typetextbox & "'    and value <='" & valuetextbox & "'
  3.  
i had also tried the querey replacing and with or but does not work

The problem is when we select all the criteria like city,type and value
the query work fine but if i left any one field blank to search it does not
show me any record or wrong recrod.

i want the query just like that if i select city (karachi) remaing all other criteria balnk then it show me only all the names haveing the city karachi.
and if i select select city (karachi) and type (1) then i had to show
me all the properties where city is karachi and type =1

hope you understant my question

Thanks in advance
Mar 18 '08 #1
9 1469
I hope its because of AND use OR instead of AND. You should get your desired answer.
Mar 18 '08 #2
mafaisal
142 100+
Hello,

Try This

Expand|Select|Wrap|Line Numbers
  1. Dim Condition As String
  2. Condition = IIf(typetextbox.Text = "", "city='" & citytextbox.Text & "'", "city='" & citytextbox.Text & "' and type ='" & typetextbox.Text & "'")
  3. "select * from property where  " & Condition & " and value <='" & valuetextbox & "'
  4.  
  5.  
Faisal
Mar 18 '08 #3
Killer42
8,435 Expert 8TB
I recommend writing out the code in a more readable form, rather than using the IIF() function.

I mean, something along the lines of...
Expand|Select|Wrap|Line Numbers
  1. If typetextbox.Text = "" Then
  2.   ' Use one query format
  3. Else
  4.   ' Use different format
  5. End If
The end result may be the same, but you'll find the code much easier to maintain in future.
Mar 18 '08 #4
QVeen72
1,445 Expert 1GB
Hi,

Build an SQL String according to user Selection :

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim sSQL As String
  3. sSQL = "select * from property where city='" & citytextbox & "' "
  4. If Trim(typetextbox.Text) <> "" Then
  5.     sSQL = sSQL & " And type ='" & typetextbox & "'"
  6. End If
  7. If Val(valuetextbox.Text) > 0 Then
  8.     sSQL = sSQL & " And Value <='" & valuetextbox & "'"
  9. End If
  10. ' Now your SQL Is Built Here,, You can include as many If conditions you want
  11.  
Regards
Veena
Mar 19 '08 #5
muddasirmunir
284 100+
i think the problem is just due to the reasen that the query is treating blank as a name and serching blank field as a filter
and it is not the problem of and ,or

like if i made all field blank and search the record then the query is showing me no record becasue there is no field in database which is
blank

but i want that if i make all fileld blanks then it has to show me all the data
becuase i want the blank fileld to be treated as means "any"
any city,type
if i did make city field blank it means i want the city can be any
while the query is searching in database for the city which is blank.


i think QVeena suggestioin is good , only one problem
i did not know
how to merger this string with recordset
i always use
Expand|Select|Wrap|Line Numbers
  1. rs.open "query",connection
so how can i use
str="query "
with recordset, acutally


Expand|Select|Wrap|Line Numbers
  1.  
  2. sSQL = "select * from property where city='" & citytextbox.text & "'"
  3. If Trim(typetextbox.Text) <> "" Then
  4.     sSQL = sSQL & " And type ='" & typetextbox & "'"
  5. End If
  6. If Val(valuetextbox.Text) > 0 Then
  7.     sSQL = sSQL & " And Value <='" & valuetextbox & "'"
  8. End If
  9. rs.open" '" & sSQL & "'",con,rsadopenstatic,adlocoptimestic
  10.  

i had use this but geeting an error
"Invalid SQL statement Expected,"Delete","Inser","Procedure",
"Select","Update".
Mar 19 '08 #6
QVeen72
1,445 Expert 1GB
Hi,

Change it to:

rs.open sSQL,con

Regards
Veena
Mar 19 '08 #7
muddasirmunir
284 100+
thanks QVeena
all other things has working fine now, but only one problem if i filter any
field except value its work fine but if i give any value to Vlauetextbox
then it give me the error
"Data type mismatch in criteria experssion'

i think some thing is wrong in this code

Expand|Select|Wrap|Line Numbers
  1. If Val(valuetxt.Text) > 0 Then
  2.     sSQL = sSQL & " And Value <='" & valuetxt.Text & "'"
  3. End If
  4.  
well this is numeric field
it seem that there is no error in the code so whats wrong
Mar 19 '08 #8
QVeen72
1,445 Expert 1GB
Hi,

I wanted to warn you in my prev post only..
Remove Single quote for Numeric fields:

Expand|Select|Wrap|Line Numbers
  1. If Val(valuetxt.Text) > 0 Then
  2.     sSQL = sSQL & " And Value <= " & Val(valuetxt.Text )
  3. End If
  4.  
Regards
Veena
Mar 19 '08 #9
muddasirmunir
284 100+
Thanks a Lot

it is now all working fine with your help.
Mar 19 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: unixman | last post by:
As usual, it is 2:00am, and I'm pulling my hair out, finally resorting to posting in the newsgroups for help. :) Simple problem, in theory. Given table "map": CREATE TABLE map ( entry_id...
6
by: Eddie Smit | last post by:
field- field- surname town ---- ---- john NY john Vegas john Boston eddie Boston eddie New Orleans eddie NY
2
by: Mrs Howl | last post by:
I have a query that just reads one table and appends to an output table, one-for-one. No criteria. It's not a Total query (i.e. no group by). It normally run run in minutes, but gets horribly...
1
by: j.mandala | last post by:
I created a simple link between two tables in a query. TableA has Social Security numbers stored as Long Integer Data. (I imported this table). The Join is between these two fields Table ...
3
by: Don Sealer | last post by:
I'm guessing this is pretty simple however not simple enough for me. I'm developing a database to track expenses, income, banking transactions, etc. I have a very simple query with four fields,...
2
by: Don Wash | last post by:
Hi All! I've been searching everywhere for a simple sample of producing a bar graph using CrystalReport by specifying SQL Query, and I've found none of it! I find so many complex samples with so...
3
by: John Baker | last post by:
Hi:7 Newby here to ASP, and using the ASP.NET Web Matrix development tool. While that tool looks great for a Newby, I have run into a snag. I have an HTML Text Box which I have named HireInput,...
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
2
by: Fendi Baba | last post by:
I created a person table with various fields such as Suffix, Salutation, etc, Some of these fields may not be mandatory for example suffix. In the actual table itself, I only have a field for...
9
by: Emin | last post by:
Dear Experts, I have a fairly simple query in which adding a where clause slows things down by at least a factor of 100. The following is the slow version of the query ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.