Microsoft Access Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft Access Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 6th, 2004, 05:04 AM
qazig qazig is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 2 qazig User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
using combo boxes and option groups to pass criteria to a report

Hello everybody !

I have a table with student's data with columns: name, gender, nationality, city.
I've created form with combo boxes for nationality and city and option group (male/female).
I'd like to be able to pass all choosen criterias from this form to a report (of course I've got query with these fields).
How should it be done ? (as for now I can do it only with one combo-box).

Thanx for any help.

Martinezzz

Reply With Quote
  #2  
Old November 7th, 2004, 08:07 PM
lwells lwells is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 632 lwells User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 21 h 59 m 38 sec
Reputation Power: 5
Hi Martinezzz

The best recommendation I can offer is to use a QBF (query by form) method. This involves making a form with all the controls you want your user to select by and placing them in unbound controls much like you have already done. In your form add a command button and give it a suitable name, like cmdSearch.

Open your code window from design view of your form (Ctl G) and at the top add Option Explicit directly below the Option Compare Database (Declarations area). Below this statement make a Function code similar to below:

Private Function BuildSQLString (strSQL As String) As Boolean
Dim strSelect As String, strFrom As String, strWhere As String

strSelect = "*"
strFrom = "NameOfTable" 'This is the name of the table that is in your query

If IsNull(comboboxNationality) Then 'Insert the name of your combobox between the ()
strWhere = strWhere
Else
strWhere = strWhere & " And Nationality = '" & comboboxNationality & "'"
End If

If IsNull(comboboxCity) Then
strWhere = strWhere
Else
strWhere = strWhere & " And City = '" & comboboxCity & "'"
End If

Do the same for each additional comboboxes on your form.

Your option group will not pass a text value of (male/female), so assuming you have the value of 1 for the male and 2 for the female use something like this:

Select Case OptionGroupMaleFemale 'Use the name of your option group
Case 1
strWhere = strWhere & " And Gender = ""Male"""
Case 2
strWhere = strWhere & " And Gender = ""Female"""
End Select

strSQL = "SELECT " & strSelect
strSQL = strSQL & "FROM " & strFrom
If strWHERE <> "" Then strSQL = strSQL & " WHERE " & Mid$(strWHERE, 6)
BuildSQLString = True

End Function
*Make note of where there are spaces before and after the quote marks
This function will return either True or False and will pass the SQL statement to your query if True.

In the OnClick event of the command button add the following similar code

Private Sub cmdSearch_Click()
On Error GoTo Err_Handler

Dim strRptName As String

strRptName = "NameOfYourReport"

If Not BuildSQLString(strSQL) Then
MsgBox "There was a problem building the search string"
Exit Sub
End If
CurrentDb.QueryDefs("NameOfYourQuery").SQL = strSQL 'Use the name of the query not the table
RefreshDatabaseWindow
DoCmd.OpenReport strRptName,acViewPreview
Exit_Handler:
Exit Sub
Err_Handler:
Msgbox Err.Description & Err.Number
Resume Exit_Handler
End Sub

Set your report's record source to this query.

If you are using more than one table in your query, you will have to modify the above Function code to include the additional tables to something like this:

strSelect = "Table1.*,Table2.*,*"
strFrom = "Table1 INNER JOIN Table2 ON Table1.ID = Table2.ID"

This should get you started, if you have any problems post back with details and I will help you.

lwells

Reply With Quote
  #3  
Old November 24th, 2004, 01:40 PM
Ericed Ericed is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 6 Ericed User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by qazig
Hello everybody !

I have a table with student's data with columns: name, gender, nationality, city.
I've created form with combo boxes for nationality and city and option group (male/female).
I'd like to be able to pass all choosen criterias from this form to a report (of course I've got query with these fields).
How should it be done ? (as for now I can do it only with one combo-box).

Thanx for any help.

Martinezzz



Microsoft has an example of this feature that I used at:

http://support.microsoft.com/kb/q208529/

It explains everything, and also has a link to the sample database "RptSmp00.mdb" along with other examples on how use reports.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft Access Development > using combo boxes and option groups to pass criteria to a report


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT