Querry with User Input and Search the columns

M

mario

I want to get the user input and use that string to search if it occurs in
any part of three columns in the table.

For example,

Select ID, Survey_01, Survey_02, Survey_03, ([Enter SurveyNum to be
searched]) As FindSurveyNum Where Instr(Survey_01, FindSurveyNum) > 0 OR
Instr(Survey_02, FindSurveyNum) > 0 OR Instr(Survey_03, FindSurveyNum) > 0

When I executed the above example, it prompts for "Enter SurveyNum to be
searched" multiple times. Is there a way where I could be prompted only once
and use that search string for querrying different columns in the table.

Thanks
 
P

pietlinden

I want to get the user input and use that string to search if it occurs in
any part of three columns in the table.

For example,

Select ID, Survey_01, Survey_02, Survey_03, ([Enter SurveyNum to be
searched]) As FindSurveyNum Where Instr(Survey_01, FindSurveyNum) > 0 OR
Instr(Survey_02, FindSurveyNum) > 0 OR Instr(Survey_03, FindSurveyNum) > 0

When I executed the above example, it prompts for "Enter SurveyNum to be
searched" multiple times. Is there a way where I could be prompted only once
and use that search string for querrying different columns in the table.

Thanks

Bad design. Stop. Redesign. What happens if you have to add another
question to your survey? Gets messy FAST. Download At Your Survey
from Duane Hookum's website. It is set up properly for querying.

then you could use something like

SELECT RespondentID, QuestionID, Response
FROM Responses
WHERE QuestionID In (1,2,3)
AND Response>0
AND SurveryNum = [Enter a Survey Number:];

otherwise, you would have to union query this mess together, and I
spent six months doing that. Total hassle. And completely
avoidable. If you have all the data in the tables already, you could
create a union query out of it and clean up this mess. But
summarizing data like this is difficult, because SQL is not designed
to work across columns as well as it is down columns.
 
J

John Spencer

If you cannot change your design then you can try the following query.

Select ID, Survey_01
, Survey_02
, Survey_03
, [Enter SurveyNum to be searched] As FindSurveyNum
WHERE Survey_01 Like "*" & [Enter SurveyNum to be searched] & "*" OR
Survey_02 Like "*" & [Enter SurveyNum to be searched] & "*" OR
Survey_03 Like "*" & [Enter SurveyNum to be searched] & "*"

If this doesn't work then try replacing the asterisks "*" with percent signs "%".

John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top