Generating Reports from a form query

tmar89

Registered User.
Local time
Today, 15:41
Joined
Jun 16, 2003
Messages
30
I'm pretty new to Access and I was asked by my department to write a program to store alot of data, which I did already. They want to be able to do many types of reports based on many more combinations of data than I could set as static queries. For instance, we have 25 fields and we could have up to 5 combinations of those 25 fields in a query, which is a very large subset of all the possible combinations.

Now my question seems simple but I don't know how to approach it. I want to have a form with 5 combo boxes that I could select any one of the 25 fields from the table and then generate a report query with just those 5 fields and the primary key sorted by the field(s) that I check with a check box. I hope that makes sense and I would appreciate any help.
 
tmar,

Base your report on a query.

The query will have a criteria for each of the five important
fields like:

Forms![YourMainForm]![YourCombo]

The only provision is that your form must be open and have
the values in the appropriate fields for the query to work.

hth,
Wayne
 
It's a good suggestion but I would need to add all 25 fields in the query then. Any other thoughts?
 
Last edited:
tmar,

I just re-read your post.

You can have five combo-boxes each with an associated
text box. You can make a query (QueryDef) that will be
the source for your report.

Basically, you construct a SQL statement:

Code:
strSQL = "Select * " & _
         "From   YourTable " & _
         "Where " & Me.Combo1 & " = " & Me.TextBox1 & " And " & _
         "      " & Me.Combo2 & " = " & Me.TextBox2 & " And " & _
         "      " & Me.Combo3 & " = " & Me.TextBox3 & " And " & _
         "      " & Me.Combo4 & " = " & Me.TextBox4 & " And " & _
         "      " & Me.Combo5 & " = " & Me.TextBox5 & ";"

See the Help files and use the Search facility here for QueryDef.

You can refine the process by using an If statement, so that
the user doesn't have to fill out All five combos (field names).

Wayne
 
tmar,

Great!

Your report button, can do something like"

Code:
strSQL = "Select * from YourTable"
If Me.Combo1 <> "" Then
   strSQL = strSQL & " where " & Me.combo1 & " = '" & Me.Textbox1 &  "'"
End If
If Me.Combo2 <> "" Then
   strSQL = strSQL & " and " & Me.combo2 & " = '" & Me.Textbox2 &  "'"
End If

and so on.

You could also make an operator box for "=", "Like" and so forth.

You might also use search here for "Multi-select" listboxes.
Because the way you traverse them to generate SQL is
somewhat similar.

hth,
Wayne
 
one quick question.. i created the SQL statement and when I click the button it pops up a box asking for the criteria that I entered. So for instance, my combo box is "Lastname", my textbox is "Jones" and then when I hit SEARCH, a window pops up and says "Jones" and I have to type in the criteria. Why does it do that?
 
tmar,

Are you using a query? Did you create a QueryDef? You did
that pretty fast.

You can't use a query, because the criteria fields change. Are
you building Text SQL strings to put into a QueryDef?

Wayne
 
Private Sub Command5_Click()
Dim MyDB As Database
Dim qdf As QueryDef
Dim strSQL As String

Set MyDB = CurrentDb()

strSQL = "Select * " & _
"From tblX " & _
"Where " & Me.Combo0 & " = " & Me.Text0 & ";"

MyDB.QueryDefs.Delete "qryX"
Set qdf = MyDB.CreateQueryDef("qryX", strSQL)

DoCmd.OpenReport "qryX", acPreview
End Sub

how's that look?
 
its getting there. i still need to figure out a way to dynamically link the t fields in the report to only 5 field I select in the query
 
tmar,

First ...
Code:
Select fldA, fldC, fldD, fldF, fldI
From   YourTable
Where fldA = "..." And
            fldB = "..."

But you'll never "know" which fields they might choose. The
SQL generates OK, but the report doesn't know which fields.

Try:

Code:
Select fldA As Field1, fldC As Field2, fldD As Field3, 
           fldF As Field4, fldI As Field5
From   YourTable
Where fldA = "..." And
            fldB = "..."

Then the Report Writer knows their names.

You could even make new fields in the query:

"'" & Me.Combo1 & "' As Field1Label

Those could be your headings on the report.

Wayne
 
Wayne,

Thank you so much for your help! I have been messing with it and I now have a user defined field based search and they can sort the data based on any field they choose! Im learning as I go with this but you were a big stepping stone with this.

Tommy
 
Tommy,

Glad to help. Look forward to seeing you here again.

Wayne
 

Users who are viewing this thread

Back
Top Bottom