473,385 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Filtering a Combo box on a form

7
I am having trouble setting up a filter on my form. I have two tables that are linked through a text field (contact_id). This field is a combination of two other text fields (first_name and last_name).

When information is entered into my forms it is entered based on the two firled first_name and last_name. (which are already entered). It would be nice if second field on the form (first_name) was filtered by the selection made in last_name.

Currently I have createda form (Form2) and the two combo boxes are cboLName and cboFName. The name of the table that containts this information in tblContact_ID and the three text fields in the table are Contact_ID, first_name, and last_name.

cboLName has last_name as its rowsource
cboFName has first_name and last_name as its rowsource

cboLName has this AfterUpdate events proceedure:

Private Sub cboLName_AfterUpdate()
Dim lName As String

lName = "SELECT [tblContact_ID].[first_name], [tblContact_ID].[last_name] " & _
"FROM tblContact_ID " & _
"WHERE [last_name] = " & Me.cboLName.Value
Me.cboFName.RowSource = fName
Me.cboFName.Requery
End Sub

However this does not work, if I select the last name on the form then I get a select statement error. Could anyone please help me.


Thanks
Jackie
Sep 3 '06 #1
9 2350
MMcCarthy
14,534 Expert Mod 8TB
Try this:

Expand|Select|Wrap|Line Numbers
  1. Dim str as String
  2.  
  3. str = "SELECT [first_name], [last_name] FROM tblContact_ID" & _
  4. " WHERE [last_name] = '" & Me.cboLName.Value & "'"
  5.  
  6. Me.cboFName.RowSourceType = "Table/Query"
  7. Me.cboFName.RowSource = str
  8. Me.cboFName.ColumnCount = 2
  9. Me.cboFName.Requery
  10. End Sub
  11.  
You need to put single quotation marks on either side of Me.cboLName.Value because it's a string


I am having trouble setting up a filter on my form. I have two tables that are linked through a text field (contact_id). This field is a combination of two other text fields (first_name and last_name).

When information is entered into my forms it is entered based on the two firled first_name and last_name. (which are already entered). It would be nice if second field on the form (first_name) was filtered by the selection made in last_name.

Currently I have createda form (Form2) and the two combo boxes are cboLName and cboFName. The name of the table that containts this information in tblContact_ID and the three text fields in the table are Contact_ID, first_name, and last_name.

cboLName has last_name as its rowsource
cboFName has first_name and last_name as its rowsource

cboLName has this AfterUpdate events proceedure:

Private Sub cboLName_AfterUpdate()
Dim lName As String

lName = "SELECT [tblContact_ID].[first_name], [tblContact_ID].[last_name] " & _
"FROM tblContact_ID " & _
"WHERE [last_name] = " & Me.cboLName.Value
Me.cboFName.RowSource = fName
Me.cboFName.Requery
End Sub

However this does not work, if I select the last name on the form then I get a select statement error. Could anyone please help me.


Thanks
Jackie
Sep 3 '06 #2
jilppe
7
Thank you very much I was so frustrated with that. It works perfectly
Sep 3 '06 #3
jilppe
7
If I want to filter another field based on both last_name and first_name (the field is incident_date) can I just add an AND clause to the WHERE statement and include both conditions?
Sep 3 '06 #4
MMcCarthy
14,534 Expert Mod 8TB
str = "SELECT [incident_date] FROM tblContact_ID" & _
" WHERE [last_name] = '" & Me.cboLName.Value & "' AND [first_name]='" & Me.cboFName.Value & "'"


If I want to filter another field based on both last_name and first_name (the field is incident_date) can I just add an AND clause to the WHERE statement and include both conditions?
Sep 3 '06 #5
jilppe
7
For some reason it is not working. I have modified the text to match the tables

Private Sub cboFName_AfterUpdate()
Dim str As String
str = "SELECT [incident_date] FROM tblAccident_Illness" & _
" WHERE [last_name] = '" & Me.cboLName.Value & "' AND [first_name]='" & Me.cboFName.Value & "'"

Me.cboIDate.RowSourceType = "Table/Query"
Me.cboIDate.RowSource = str
Me.cboIDate.ColumnCount = 2
Me.cboIDate.Requery
End Sub

I am getting an error on all the .RowSource statements.

Ultimately what I am trying to do is to create a form of the three items where each box filters off the next and then have a command button that will open a report that will use the criteria selected in the three boxes.
Sep 3 '06 #6
MMcCarthy
14,534 Expert Mod 8TB
str = "SELECT [incident_date] FROM tblAccident_Illness" & _
" WHERE [last_name] = '" & Me.cboLName.Value & "' AND [first_name]='" & Me.cboFName.Value & "'"

Are last_name and first_name fields in the tblAccident_Illness table, if not what is the relationship between the two tables.

For some reason it is not working. I have modified the text to match the tables

Private Sub cboFName_AfterUpdate()
Dim str As String
str = "SELECT [incident_date] FROM tblAccident_Illness" & _
" WHERE [last_name] = '" & Me.cboLName.Value & "' AND [first_name]='" & Me.cboFName.Value & "'"

Me.cboIDate.RowSourceType = "Table/Query"
Me.cboIDate.RowSource = str
Me.cboIDate.ColumnCount = 2
Me.cboIDate.Requery
End Sub

I am getting an error on all the .RowSource statements.

Ultimately what I am trying to do is to create a form of the three items where each box filters off the next and then have a command button that will open a report that will use the criteria selected in the three boxes.
Sep 3 '06 #7
jilppe
7
last_name and first_name are in tblAccident_Illness.

Also how do I get the information on the form to be the record selected for a report that I already have? The report is based on a query called

Accident_report

The fields last_name first_name and incident_date are also available in the query. Should the selection form use these fields and then I could put a command button to run or print the report. Will I need to specify that I want the selected record to be the only one in the report of will that be the default if I use the query that runs the report to generate the form?


Also I have a report that uses a crosstab query called bodycrosstab. This cross tab counts the number of incidents for each body part for each month. The report and query are working fine but I need a way that each time the report is run it asks which year should be used (and then filters the data in the report for that year. This would also need to be made availabile in the header of the report so if I selected 2005 then the header of the report would be Body Parts 2005.

Thanks for your help. After these two things I will be done.
Sep 4 '06 #8
jilppe
7
I was able to solve the problem with the report and the form acting as a filter. The only question I still have is the one below

Also I have a report that uses a crosstab query called bodycrosstab. This cross tab counts the number of incidents for each body part for each month. The report and query are working fine but I need a way that each time the report is run it asks which year should be used (and then filters the data in the report for that year. This would also need to be made availabile in the header of the report so if I selected 2005 then the header of the report would be Body Parts 2005.
Sep 4 '06 #9
MMcCarthy
14,534 Expert Mod 8TB
It depends where you are opening the report from. I would suggest that if you are using a command button on a form you add a combobox (cboYear) above it with a list of Years (You could just design a lookup table or set a query to get the year field from the query grouped on year field).

Ask the user to select a year before running the report and in the On Click event of the button (cmdPrint) put the following code.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdPrint_OnClick()
  2. Dim stDocName As String
  3. Dim stLinkCriteria As String
  4.  
  5. If IsNull(Me.cboYear) Then
  6. Msgbox "You must select a year", vbOkOnly
  7. Exit Sub
  8. Else
  9. stDocName = "Name of Crosstab Query Report"
  10. stLinkCriteria = "[Year]=" & Me.cboYear
  11. DoCmd.OpenReport stDocName, , , stLinkCriteria
  12. End Sub
  13.  
Put a textbox control on the Report next to the Header Label and set the source to

=[Forms]![FormName]![cboYear]

I was able to solve the problem with the report and the form acting as a filter. The only question I still have is the one below

Also I have a report that uses a crosstab query called bodycrosstab. This cross tab counts the number of incidents for each body part for each month. The report and query are working fine but I need a way that each time the report is run it asks which year should be used (and then filters the data in the report for that year. This would also need to be made availabile in the header of the report so if I selected 2005 then the header of the report would be Body Parts 2005.
Sep 5 '06 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Dalan | last post by:
Okay, I have worked on this and then some, but cannot seem to crack it. So if someone can straighten my code out, or suggest a new approach, then I'm all ears. Here goes: I have two tables - one...
3
by: Jason | last post by:
I am trying to filter records in a primary form based on records in related tables. The data in the related tables is being displayed in the primary form through subforms. To be more specific, I...
2
by: Sean | last post by:
Greetings all, I am attempting to make a form that will filter through several tables that (I believe) have refretial integrity. I am pulling data from several tables into the form and i would...
0
by: Scott Loupin | last post by:
I've got two databases with similar data in them (WestSide and EastSide). I've set up two identical reports that is filtered by date and the client name. I'm using one form to do the filtering. ...
1
by: MLH | last post by:
I have a form (xxxxxxxxxxxxxx) with a combo-box control (yyyyyyyyyyyyyy). The rowsource property for that combo box is as follows: SELECT DISTINCTROW ., . FROM ; The SQL for qryVehicleList...
1
by: hackerslacker | last post by:
I have an ordering form that use two combo boxes to filter down the records of a Products table. This worked fine with the after_Update of the first filtering the records and creating the...
2
by: Dev1 | last post by:
All- I'm new to this forum and i've been working with acces for about 2 days now. I have a form in which I have two combo boxes and depending on what is selected on the first dropdown the second...
4
by: novoselent | last post by:
This seems like it should be an easy thing, but I think I'm missing something simple here...or I'm just going about it all wrong... Using Access 2003 I have a form that lists vehicle service...
3
by: flymo | last post by:
Hello All, I've bee trying out access 2007 and have a weird issue and would like to see if I'm issing something really basic. I have a form based on a query, I create a combo to look for records...
5
by: RHooper | last post by:
Hi, I'm new to Access, so I apologize if this question is trivial. I am trying to set-up a quick filter for users to define on a form bound to a table. I have a combo box called...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.