How do I use a form field to fill in multiple criteria into a quer

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a query that has criteria pulled from a form field. How do I build it
so I can enter more than one variable into the same form field?

Thanks,
Jim
 
J. Lumsden said:
I have a query that has criteria pulled from a form field. How do I build it
so I can enter more than one variable into the same form field?

Thanks,
Jim

Jim,

I think I know what you mean but I'm not sure, can you be a little more
specific?

Thanks!
 
Jim:

Do you mean enter a value list into the control, e.g. Apple,Orange,Banana?

If so the following link gives a couple of methods:


http://support.microsoft.com/kb/100131/en-us


If you use the second method, creating the module with the InParam and
GetToken functions, then you pass a reference to the control on the form as
the second argument when calling the InParam function in the query, e.g.

InParam(Fruit,Forms!MyForm!txtFruitList)

where Fruit is the name of the field and txtFruitList is the name of the
control on form MyForm into which the comma delimited list is entered. Note
that the value list is entered without quotes around each text item, unlike
the use of the IN operator with a list of literal text values.

Ken Sheridan
Stafford, England
 
The normal approach is to build a un-bound form (a un-bound form is a form
that is NOT attached to a table - these forms are typicaly desiged for user
interface face stuff like promtps, print buttions etc).

The following screen shots are all un-bound forms, and they simply prompt
the user for informaton.

http://www.members.shaw.ca/AlbertKallal/ridesrpt/ridesrpt.html

The above shold give you some ideas

So, the solution use now is simply to take the values from the form, and
build your own where clause in code. That way, you simply design the reports
(or forms), and attached them to the query. And, NO FORMS conditions are
placed in the query.

To "send" the conditions to the report (or form), you simply use the "where"
clause. This is exactly why ms-access has this feature...and it solves a
zillion problems...and will reduce your development costs by a substantial
amount.

note that you can use form!formname!fieldname expressions in the query, and
thus not need any code. however, this generally means that you have to fill
in all of the values you proompt for...and often you don't want to do this.
The next suggestion does take some code..but, results are better..

The code to make those above screens work and launch the report with the
selected restrictions when you hit the "print" button is easy:


dim strWhere as string

' select sales rep combo

if isnull(cboSalesRep) = false then

strWhere = "SalesRep = '" & cboSalesRep & "'"

end if

' select what City for the report

if isnull(cboCity) = false then
if strWhere <> "" then
strWhere = strWhere " and "
endif
strWhere = strWhere & "City = '" & cobCity & "'"
end if

Note how the 2nd combo test is setup. You can add as "many" more conditions
you want. Lets say we have a check box to only include Special Customers. We
can add to our very nice prompt screen a check box to

[x] Show Only Special customers

The code we add would be:

if chkSpeicalOnly = True then
if strWhere <> "" then
strWhere = strWhere " and "
endif
strWhere = strWhere & "SpecialCust = true"
endif

For sure, each combo and control we add to the nice report screen takes a
bit of code, but no more messy then the query builder..and this way, each
query is nice and clean, and free of a bunch of HIGHLY un-maintainable
forms! expressions.

Further, it means you can re-use the same query for different reports, and
have no worries about some form that is supposed to be open. So, a tiny bit
more code eliminates the messy query problem.. For me, this is very worth
while trade.

For a date range, we could put two calendar contorls on the screen. The code
could be:


dim strWhere as string
dim strStartDate as string
dim strEndDate as string


strStartDtae = "#" & format(me.StartDateContorl,"mm/dd/yyyy") & "#"
strEndDate = "#" & format(me.EndDateContorl,"mm/dd/yyyy") & "#"

strWhere = "InvoiceDate is between " & strStartDate & " and " & strEndDate

docmd.openReport "InvoiceReport",acViewPreview,,strWhere
 
Currently I have an unbound form called "Regional Service Log Binder Form"
that the user can input any of four possible fields
(GroupCode,AreaCode,SiteName,Employee#). I then have a make table query that
uses the following criteria to build the table:

([forms]![Regional Service Log Binder Form]![GroupCode])
([forms]![Regional Service Log Binder Form]![AreaCode])
([forms]![Regional Service Log Binder Form]![SiteName])
([forms]![Regional Service Log Binder Form]![Employee#])

The table is then used to mail merge to a Word document. It works well as
long as there is only one criteria (i.e. 12345 vs 12345,67890) in each of the
form fields.

How do I set this up so I could enter a string of different Employee#s,
GroupCodes, AreaCodes or SiteNames in each of the form fields?

Thanks,
Jim
 

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

Back
Top