Diffuculty Using A Form to Collect Data for a Query/Report

D

danftz

All,

I am a returning user to Access after a 10 year absence. I am using Access
2007 and having trouble doing the following.
I would like to use a form to allow the user to select from a list of
training programs on which I would like to base a query/report. I am able to
successfully create that form and get the data in an unbound control. I do
not know then how to use that data to run the query/report. Ultimately, this
would run from a button on the form. So, user selects program from list,
clicks query button, records from another table are selected based on the
selected program.

Sorry to bother with something so basic, but I am stumped in making the
connection.

Dan
 
T

Tom Wickerath

Hi Dan,

You can have a command button on a form that opens your report, which
includes the optional WhereCondition argument. This argument could pick up
the required selection from your unbound combo box. The WhereCondition
argument is a valid SQL WHERE statement, but without this keyword included.

Alternatively, you can base the report on a query and have the query look to
the combo box on your open form to set a criteria. The criteria row in the
QBE grid would include a statement for the appropriate field that looks
something like this:

=[Forms]![Name of Form]![Name of Combo Box Control]

This will pick up the bound column for the combo box (even if the combo box
itself is unbound). Here is a sample that you can download, which you might
find helpful:

http://home.comcast.net/~tutorme2/samples/customdialogbox.zip


Tom Wickerath
Microsoft Access MVP
https://mvp.support.microsoft.com/profile/Tom
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
 
A

Albert D. Kallal

If it just one selection from a listbox, or drop down, then you simply build
a query, and place the condtion in the query builder



in the query grid, for hte condction, simply type int he name of hte form +
the unbound contorl

eg:


forms!MyPromptForm!txtTraining


If you have a buttion on your form, then you can have it launch a report
based on that query..and it will be filtered by the selection

The above appparch means that you actually don't even have to write ANY
code. However, you might want a case where the user does not sledct ANY
thing from the combo box (or list box). I you need more flecibty, then read
on....

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.

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


And, if you note close, in those screen shots, I often simply state (no
selection = all).

So, you can bold the report, place an actually condition that references the
form, and then when you launch the report..it will be filter..and it will
take ZERO code....

If your looking to make something more user friendly, and have users will
your hide the ms-access interface, then the above codng ideas should give
you a good start of this...
 
D

danftz

Tom,

Thanks for getting me on the right path. I seem to be getting close, but am
having a problem with the match. I am using the example code that you
mentioned almost exactly. However, I am getting no matches on the query. If I
run the query without the dialog box, I am prompted for a ProgramTitle. I
type one, and get records returned. However, if I select the same program
name form the combo box list in the form, it does not match. I have tried to
use a Trim function, but that made no difference. Any thoughts?


Tom Wickerath said:
Hi Dan,

You can have a command button on a form that opens your report, which
includes the optional WhereCondition argument. This argument could pick up
the required selection from your unbound combo box. The WhereCondition
argument is a valid SQL WHERE statement, but without this keyword included.

Alternatively, you can base the report on a query and have the query look to
the combo box on your open form to set a criteria. The criteria row in the
QBE grid would include a statement for the appropriate field that looks
something like this:

=[Forms]![Name of Form]![Name of Combo Box Control]

This will pick up the bound column for the combo box (even if the combo box
itself is unbound). Here is a sample that you can download, which you might
find helpful:

http://home.comcast.net/~tutorme2/samples/customdialogbox.zip


Tom Wickerath
Microsoft Access MVP
https://mvp.support.microsoft.com/profile/Tom
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________

danftz said:
All,

I am a returning user to Access after a 10 year absence. I am using Access
2007 and having trouble doing the following.
I would like to use a form to allow the user to select from a list of
training programs on which I would like to base a query/report. I am able to
successfully create that form and get the data in an unbound control. I do
not know then how to use that data to run the query/report. Ultimately, this
would run from a button on the form. So, user selects program from list,
clicks query button, records from another table are selected based on the
selected program.

Sorry to bother with something so basic, but I am stumped in making the
connection.

Dan
 
T

Tom Wickerath

Hi Dan,

What is the Rowsource for your combo box? It could include a key field that
is hidden, because the column width is zero. If this happens to be the bound
column, then that's what would be supplied to your query. For example, if I
have this SQL statement as the Rowsource:

SELECT EmployeeID, EmployeeName FROM Employees ORDER BY EmployeeName

with column count set to 2, column widths of 0";1.5" and bound column = 1
(likely default), then when I make a selection in the combo box, the query
criteria would see EmployeeID, not the EmployeeName. That's okay, and
actually preferred, as long as the criteria is associated with the EmployeeID
field. However, if you had the criteria in the query associated with the
EmployeeName, then you wouldn't want to feed it an EmployeeID value.


Tom Wickerath
Microsoft Access MVP
https://mvp.support.microsoft.com/profile/Tom
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
 
D

danftz

Thanks Tom, that got me up and running. (I was stumped again but forgot to
tell Access 2007 to allow the code after I had changed it.)

So, now I can move forward. Of course that means more questions for all of
you.
 
D

danftz

Thank you Albert. I was able to get working with what Tom had provided, but I
may need some of what you provided in other areas.

Albert D. Kallal said:
If it just one selection from a listbox, or drop down, then you simply build
a query, and place the condtion in the query builder



in the query grid, for hte condction, simply type int he name of hte form +
the unbound contorl

eg:


forms!MyPromptForm!txtTraining


If you have a buttion on your form, then you can have it launch a report
based on that query..and it will be filtered by the selection

The above appparch means that you actually don't even have to write ANY
code. However, you might want a case where the user does not sledct ANY
thing from the combo box (or list box). I you need more flecibty, then read
on....

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.

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


And, if you note close, in those screen shots, I often simply state (no
selection = all).

So, you can bold the report, place an actually condition that references the
form, and then when you launch the report..it will be filter..and it will
take ZERO code....

If your looking to make something more user friendly, and have users will
your hide the ms-access interface, then the above codng ideas should give
you a good start of this...

--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
[email protected]
http://www.members.shaw.ca/AlbertKallal
 
T

Tom Wickerath

Hi Dan,
... but forgot to tell Access 2007 to allow the code after I had changed it.)

Have you trusted the folder that your database is in?

Dealing with the Trust Center
http://www.access.qbuilt.com/html/trust_center.html

Of course that means more questions for all of you.

Great! We love questions, so bring 'em on!


Tom Wickerath
Microsoft Access MVP
https://mvp.support.microsoft.com/profile/Tom
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
 
Top