Re: Trouble with a multiple parameter search
- From: "Allen Browne" <AllenBrowne@xxxxxxxxxxxxxx>
- Date: Tue, 3 Jan 2006 11:01:02 +0800
Hi Mary
What you are trying to do makes perfect sense, but I think it might be
easier to take a little step further and turn this into a search form. This
involves a bit of code, but is much simpler, more flexible, and more
efficient in the long run. For example, if you only used 2 of the criteria
boxes, the code will produce a SQL statement that only has 2 phrases in the
WHERE clause instead of 14 like the one you posted.
I suggest a continuous form where the search results are shown one per row.
The criteria boxes go in the Form Header section (View menu in form design),
along with a command button to execute the search. The search button builds
the SQL statement from the non-blank boxes, and applies it as the
RecordSource of the search form, so that it shows only matches as its
results.
The example below assumes the search criteria text boxes are named
txtFindTP, txtFindSPKR, etc. The code concatenates each non-null search
value into the string, tacking " AND " on the end ready for the next one.
The final trailing " AND " is then chopped off, and the string is added to
the stub of the SQL statement. (Note that the constant is one long line,
starting with SELECT and ending with the FROM clause."
Private Sub cmdSearch_Click()
Dim strSql As String
Dim lngLen As Long
Const strcStub = "SELECT [Bridge Transcripts].TP, [Bridge
Transcripts].TC, [Bridge Transcripts].SPKR, [Bridge Transcripts].TRANSCRIPT,
[Bridge Transcripts].STARS, [Bridge Transcripts].WITH, [Bridge
Transcripts].KEYWORDS, [Bridge Transcripts].LOC, [Bridge Transcripts].ID
FROM [Bridge Transcripts]"
'Build the WHERE clause from the non-Null boxes.
If Not IsNull(Me.txtFindTP) Then
strSql = strSql & "(TP = """ & Me.txtFindTP & """) AND "
End If
If Not IsNull(Me.txtFindSpkr) Then
strSql = strSql & "(SPKR = """ & Me.txtFindSPKR & ") AND "
End If
'etc for other text boxes.
lngLen = Len(strSql) - 5 'Chop off the trailiing " AND ".
If lngLen > 0 Then
strSql = strcStub & " WHERE " & Left$(strSql, lngLen) & ";"
Else
strSql = strcStub & ";"
MsgBox "No criteria"
End If
'Finally, show the search results in this form.
Me.RecordSource = strSql
End Sub
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
<maryrb@xxxxxxxxx> wrote in message
news:1136254512.856277.288170@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>
> I'm very new to Access, and have been struggling to get what seems to
> me a very simple database to work correctly.
>
> I've got just one table, and each record contains only nine fields
> (including the primary key). The fields are all text, except for one
> that is memo.
>
> What I'd like to be able to do is build a form with input boxes for
> seven of the fields that will launch a multiple parameter query. I'll
> never be putting search terms into all of the fields on the form, but
> I'll often be using different fields for different searches.
>
> Seems like a fairly straightforward project. But I'm having trouble
> with the query. Even without using the form, it will only return proper
> results on data entered into the first two parameters. No results are
> returned for anything that I enter into the last five parameters
> (although i do get proper returns when i build simple single-parameter
> queries for each of the fields).
>
> So ... here's the SQL I've got. From all the SQL stuff i've been poring
> over, I think I'm using AND correctly, but Access obviously doesn't
> think so.
>
> SELECT [Bridge Transcripts].TP, [Bridge Transcripts].TC, [Bridge
> Transcripts].SPKR, [Bridge Transcripts].TRANSCRIPT, [Bridge
> Transcripts].STARS, [Bridge Transcripts].WITH, [Bridge
> Transcripts].KEYWORDS, [Bridge Transcripts].LOC, [Bridge
> Transcripts].ID
> FROM [Bridge Transcripts]
>
> WHERE ([Bridge Transcripts].[TP] = [Forms]![BTF]![TP] Or
> [Forms]![BTF]![TP] Is Null)
> AND ([Bridge Transcripts].[SPKR] = [Forms]![BTF]![SPKR] Or
> [Forms]![BTF]![SPKR] Is Null)
> AND ([Bridge Transcripts].[TRANSCRIPT] = [Forms]![BTF]![TP] Or
> [Forms]![BTF]![TRANSCRIPT] Is Null)
> AND ([Bridge Transcripts].[STARS] = [Forms]![BTF]![TP] Or
> [Forms]![BTF]![STARS] Is Null)
> AND ([Bridge Transcripts].[WITH] = [Forms]![BTF]![TP] Or
> [Forms]![BTF]![WITH] Is Null)
> AND ([Bridge Transcripts].[KEYWORDS]= [Forms]![BTF]![TP] Or
> [Forms]![BTF]![KEYWORDS] Is Null)
> AND ([Bridge Transcripts].[LOC] = [Forms]![BTF]![TP] Or
> [Forms]![BTF]![LOC] Is Null);
>
> Any help greatly appreciated.
>
> -Mary
.
- Follow-Ups:
- Re: Trouble with a multiple parameter search
- From: maryrb
- Re: Trouble with a multiple parameter search
- References:
- Trouble with a multiple parameter search
- From: maryrb
- Trouble with a multiple parameter search
- Prev by Date: Re: Run-time error 2115
- Next by Date: Re: Access 97 - Subform Data Not Being Retrieved
- Previous by thread: Trouble with a multiple parameter search
- Next by thread: Re: Trouble with a multiple parameter search
- Index(es):
Relevant Pages
|