473,399 Members | 2,858 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,399 software developers and data experts.

Re: Cascading Combo boxes

hi, i hope someone can help, i have read a lot of tutorials relating to this matter but im afraid VBA isnt my strong point and i just need a touch of guidance.

I have one table called [tbl Site Info] which has three fields [Site], [Department] and [Manager]. On a form i have three combo boxes which show data from each field on the table along with an extra 'All Records'. The following code enabling this is in the 'Row Source' of each combo box:

[SELECT [tbl Site Info].[Site] AS Filter, [Site] FROM [tbl Site Info] UNION SELECT "*" AS Filter, "All Records" AS [Site] FROM [tbl Site Info];]

In conjunction with this, i need/would like the option of limiting the data in the combo boxes based on the selection of the last, i.e. if i select 'UK' from the cboSite box then it will only give me the Departments from the UK in the cboDept box and similarly when i select a department then the cboMgr box will only show the managers associated with that department.

However i need to keep the 'All Records' option as i will need to show all departments in UK, etc...

I tried using the following code in the row source of the cboDept box:

[SELECT [tbl Site Info].[Department] AS Filter, [Department] FROM [tbl Site Info] UNION SELECT "*" AS Filter, "All Records" AS [Department] FROM [tbl Site Info] WHERE [tbl Site Info].[Site]=Forms![frm Main copy]!cboSite;]

which i believed would only show the departments related to the site, but it did'nt.

Any help would be greatly appreciated,
Jun 6 '07 #1
15 2157
puppydogbuddy
1,923 Expert 1GB
hi, i hope someone can help, i have read a lot of tutorials relating to this matter but im afraid VBA isnt my strong point and i just need a touch of guidance.

I have one table called [tbl Site Info] which has three fields [Site], [Department] and [Manager]. On a form i have three combo boxes which show data from each field on the table along with an extra 'All Records'. The following code enabling this is in the 'Row Source' of each combo box:

[SELECT [tbl Site Info].[Site] AS Filter, [Site] FROM [tbl Site Info] UNION SELECT "*" AS Filter, "All Records" AS [Site] FROM [tbl Site Info];]

In conjunction with this, i need/would like the option of limiting the data in the combo boxes based on the selection of the last, i.e. if i select 'UK' from the cboSite box then it will only give me the Departments from the UK in the cboDept box and similarly when i select a department then the cboMgr box will only show the managers associated with that department.

However i need to keep the 'All Records' option as i will need to show all departments in UK, etc...

I tried using the following code in the row source of the cboDept box:

[SELECT [tbl Site Info].[Department] AS Filter, [Department] FROM [tbl Site Info] UNION SELECT "*" AS Filter, "All Records" AS [Department] FROM [tbl Site Info] WHERE [tbl Site Info].[Site]=Forms![frm Main copy]!cboSite;]

which i believed would only show the departments related to the site, but it did'nt.

Any help would be greatly appreciated,
Hi Jon,
You are much closer to what you want then you realize.

All you needed to make the Department combobox work was an afterUpdate event for cboSite that will requery the Department list. In other words, if you want the Department combobox list to reflect only the Departments for the site(s) selected in cboSite, you need to requery the Department list. To do that, place the following code in the afterUpdate event of cboSite.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboSite_AfterUpdate()
  2.  
  3. Me!cboDept.Requery
  4.  
  5. End Sub
  6.  
Similarly, if you make a selection of a department, you need to requery the managers list to update the managers list based on the department selected.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboDept_AfterUpdate()
  2.  
  3. Me!cboMgr.Requery
  4.  
  5. End Sub
  6.  
Jun 7 '07 #2
Thanks Puppydogbuddy,

When i put the code into the 'after_update' of cboSite, there is no difference in the outcome of the cboDept box (i.e. all the results are shown, not just the ones related to that site) . I have tried removing the Row Source details from cboDept but that does not give me any entries.

Is there anything that i am missing?.. if i remove the row source then i wont have the option of 'All Records' which is necessary. Is it possible to have both??

If there is anything you (or anyone else) can suggest then i am open to all ideas, if not then i will keep the boxes as they are.

Again, i do appreciate the help.
Jun 7 '07 #3
puppydogbuddy
1,923 Expert 1GB
Jon,
Be patient. We will get there. In the meantime, can you ry the code below in the after update event of cboSite. Just tell me what happens that is different than before. Thanks.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboSite_AfterUpdate()
  2. cboDept.RowSource = cboSite.RowSource
  3. Me!cboDept.Requery
  4. End Sub
  5.  
Jun 7 '07 #4
puppydogbuddy
1,923 Expert 1GB
Hi Jon,
I see what the problem is now. For cboDept, you should have the following code as the row source:

[SELECT [tbl Site Info].[Department] AS Filter, [Department] FROM [tbl Site Info] UNION SELECT "*" AS Filter, "All Records" AS [Department] FROM [tbl Site Info];]

and the following code in the after update of cboSite:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboSite_AfterUpdate()
  2. cboDepartment.RowSource = “[SELECT [tbl Site Info].[Department] AS Filter, [Department] FROM [tbl Site Info] UNION SELECT "*" AS Filter, "All Records" AS [Department] FROM [tbl Site Info] WHERE [tbl Site Info].[Site]=Forms![frm Main copy]!cboSite;]”
  3.  
  4. Me!cboDept.Requery
  5.  
  6. End Sub
  7.  
Jun 7 '07 #5
Hi,

I used the code from your second post and after selecting from cboSite, cboDept then mirrored the information from cboSite.

Now, using the code from your last reply (i amended cboDepartment to cboDept), there seems to be a syntax error in line 2.
Jun 7 '07 #6
puppydogbuddy
1,923 Expert 1GB
Hi,

I used the code from your second post and after selecting from cboSite, cboDept then mirrored the information from cboSite.

Now, using the code from your last reply (i amended cboDepartment to cboDept), there seems to be a syntax error in line 2.
Ok, jon. It may take a couple of tries to get the syntax correct, but we are making progress. I assuming that Site is a text data type. If it isn't, I will need to correct the syntax accordingly. Also, I am trying single quotes around * . If that does not work, I will have to replace the single quotes with the ascii equivalent for double quotes.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cboSite_AfterUpdate()
  2. Dim strSQL As String
  3.  
  4.  
  5. strSQL = “SELECT [tbl Site Info].[Department] AS Filter, [Department]“
  6. strSQL = strSQL & “ FROM [tbl Site Info]”
  7. strSQL = strSQL & “ UNION SELECT ‘*’ AS Filter, ‘All Records’ AS [Department]”
  8. strSQL = strSQL & “ FROM [tbl Site Info]”
  9. strSQL = strSQL & “ WHERE [tbl Site Info].[Site] = ‘” & Forms![frm Main copy]!cboSite & “’” & “;”
  10.  
  11. cboDept.RowSource = strSQL
  12.  
  13. Me!cboDept.Requery
  14.  
  15. End Sub
Jun 7 '07 #7
Site is a text data type, however when pasting the code lines 5-9 are highlighted in red and when selecting the first combo box, once i click on one of the items the same 'Syntax Error' msg box is shown and line 5 is highlighted.
Jun 7 '07 #8
puppydogbuddy
1,923 Expert 1GB
Site is a text data type, however when pasting the code lines 5-9 are highlighted in red and when selecting the first combo box, once i click on one of the items the same 'Syntax Error' msg box is shown and line 5 is highlighted.
Ok, Jon.

Please change line 7 from this:
Expand|Select|Wrap|Line Numbers
  1. strSQL = strSQL & “ UNION SELECT ‘*’ AS Filter, ‘All Records’ AS [Department]”
  2.  
  3. ' to:
  4. strSQL = strSQL & “ UNION SELECT “ & Chr(34) & * & Chr(34) & “ AS Filter, ‘All Records’ AS [Department]”
  5.  

If you still get a syntax error, I need you to hit CTRL + G which will take you to the debug window. The debug window will appear at the bottom of the code window with a carot prompt. At the prompt I need you to type as shown below:
>? strSQL

Access will respond by displaying its interpretation of the SQL syntax that was used. Take a look at it and see if you spot where the syntax was not interpreted correctly. If you can't see anything, copy the string that was displayed and post it here so that I can see it..
Jun 7 '07 #9
Ok, Jon.

Please change line 7 from this:
Expand|Select|Wrap|Line Numbers
  1. strSQL = strSQL & “ UNION SELECT ‘*’ AS Filter, ‘All Records’ AS [Department]”
  2.  
  3. ' to:
  4. strSQL = strSQL & “ UNION SELECT “ & Chr(34) & * & Chr(34) & “ AS Filter, ‘All Records’ AS [Department]”
  5.  

If you still get a syntax error, I need you to hit CTRL + G which will take you to the debug window. The debug window will appear at the bottom of the code window with a carot prompt. At the prompt I need you to type as shown below:
>? strSQL

Access will respond by displaying its interpretation of the SQL syntax that was used. Take a look at it and see if you spot where the syntax was not interpreted correctly. If you can't see anything, copy the string that was displayed and post it here so that I can see it..

Right, still coming up with a Syntax error. When i goto the debug window and type >?strSQL a message box appears with the words

Compile Error:
Expected: line number or label or statement or end of statement

but nothing else is displayed.

Where am i going wrong?
This is the code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cboSite_AfterUpdate()
  2. Dim strSQL As String
  3.  
  4.  
  5. strSQL = “SELECT [tbl Site Info].[Department] AS Filter, [Department]“
  6. strSQL = strSQL & “ FROM [tbl Site Info]”
  7. strSQL = strSQL & “ UNION SELECT “ & Chr(34) & * & Chr(34) & “ AS Filter, ‘All Records’ AS [Department]”
  8. strSQL = strSQL & “ FROM [tbl Site Info]”
  9. strSQL = strSQL & “ WHERE [tbl Site Info].[Site] = ‘” & Forms![frm Main copy]!cboSite & “’” & “;”
  10.  
  11. cboDept.RowSource = strSQL
  12.  
  13. Me!cboDept.Requery
  14.  
  15. End Sub
  16.  
Jun 7 '07 #10
puppydogbuddy
1,923 Expert 1GB
Jon,
I think the problem is with the syntax for the wildcard and the ending semi-colon. Try the syntax below for lines #7 and #9.

strSQL = strSQL & “ UNION SELECT “ & Chr(34) & “*” & Chr(34) & “ AS Filter, ‘All Records’ AS [Department]”

strSQL = strSQL & “ WHERE [tbl Site Info].[Site] = ‘” & Forms![frm Main copy]!cboSite & “’;”


If that doesn't get it, can you send me a copy of your mdb, with any sensitive info stripped out? My email is with my profile.
Jun 7 '07 #11
Jon,
I think the problem is with the syntax for the wildcard and the ending semi-colon. Try the syntax below for lines #7 and #9.

strSQL = strSQL & “ UNION SELECT “ & Chr(34) & “*” & Chr(34) & “ AS Filter, ‘All Records’ AS [Department]”

strSQL = strSQL & “ WHERE [tbl Site Info].[Site] = ‘” & Forms![frm Main copy]!cboSite & “’;”


If that doesn't get it, can you send me a copy of your mdb, with any sensitive info stripped out? My email is with my profile.

Ok, its almost started to work, at least the Syntax error has stopped but cboDept doesnt seem to be taking the value of cboSite. Im unable to get your e-mail address from your profile, can you enable access to it and ill forward you the mdb.
Jun 7 '07 #12
puppydogbuddy
1,923 Expert 1GB
Ok, its almost started to work, at least the Syntax error has stopped but cboDept doesnt seem to be taking the value of cboSite. Im unable to get your e-mail address from your profile, can you enable access to it and ill forward you the mdb.
Jon.
I enabled my email, but it is still inaccessable and I will have to find out why. I was able to download my vCard, which also has my email address on it, so you should be able to get it from my vCard.

Before you send me your file, try changing the line that says Me!cboDepartment.Requery to Me!cboSite.Requery and see what happens, just for kicks.
Jun 7 '07 #13
Ive sent you an email, thanks.
Jun 7 '07 #14
MMcCarthy
14,534 Expert Mod 8TB
Jon.
I enabled my email, but it is still inaccessable and I will have to find out why. I was able to download my vCard, which also has my email address on it, so you should be able to get it from my vCard.

Before you send me your file, try changing the line that says Me!cboDepartment.Requery to Me!cboSite.Requery and see what happens, just for kicks.
Hi pdog

The email link doesn't work but the vcard should work fine.

Mary
Jun 13 '07 #15
puppydogbuddy
1,923 Expert 1GB
Hi pdog

The email link doesn't work but the vcard should work fine.

Mary
Thanks, Mary. The vcard link works fine. After I received the application in the email from Jon, I was able to diagnose and fix the problem with these comboboxes. The AfterUpdate event used a union query with a where clause...and the Where clause was placed after the Union Select statement. The Where clause in a Union Query has to be placed with the first select statement or it will not work properly. The correct syntax is shown below.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboSite_AfterUpdate()
  2.  
  3. strSQL = ""    'to initialize string variable
  4. strSQL = strSQL & "SELECT Distinct Department AS Filter, Department"
  5. strSQL = strSQL & " FROM [tbl Site Info]"
  6. strSQL = strSQL & " WHERE [tbl Site Info].[Site] = '" & Forms![frm Main copy]!cboSite.Value & "'" 
  7. strSQL = strSQL & " UNION SELECT Distinct " & Chr(34) & "*" & Chr(34) & " AS Filter, 'All Records' AS Department"
  8. strSQL = strSQL & " FROM [tbl Site Info]"
  9. strSQL = strSQL & " Order By Department;"
  10.  
  11. cboDept.RowSource = strSQL
  12.  
  13. Me.Requery
  14.  
  15. End Sub
  16.  
Jun 13 '07 #16

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

Similar topics

0
by: cognoscento | last post by:
I'm currently putting together a database for my work (not an expert by any stretch, so muddling through as best as I can... you know the story...) and I could use some advice and hand-holding ...
9
by: Edwinah63 | last post by:
Hi everyone, Please let there be someone out there who can help. I have two BOUND combo boxes on a continuous form, the second being dependent on the first. I have no problem getting the...
6
by: visionstate | last post by:
Hi there, I am building a database that requires cascading lists on a form. I currently have (I may be adding more later) 3 combo boxes on my form - Department, Surname and Forename. The user...
4
Rabbit
by: Rabbit | last post by:
Cascading Combo/List Boxes This tutorial is to guide you in the creation of Cascading combo/list boxes. That is when you have multiple combo/list boxes where the selection of an option in one...
3
by: buddyr | last post by:
Hello, Yesterday I recieved help with two cascading combo boxes on an access form. I went the link http://www.fontstuff.com/access/acctut10.htm And basically used their first example. Now I...
4
klarae99
by: klarae99 | last post by:
Hello, I am working on an Access 2003 Database. The tables that pertain to this issue are tblOrg, tblState, tblCity, and tblZip. I have posted the table structure with only the pertinant fields...
1
kcdoell
by: kcdoell | last post by:
Good Morning: I have a form where I am trying to create cascading combo boxes. I have done this before but I am getting the following error that is throwing me off: Procedure declaration...
3
kcdoell
by: kcdoell | last post by:
I have 5 cascading combo boxes on a form. Below is a sample of my vb in the first combo box: Private Sub CboDivision_AfterUpdate() 'When the Division is selected, the appropriate Segment...
7
by: Toireasa | last post by:
Hi, Newbie Access developer here, and my first post on this forum, so I might not get everything right - thanks in advance for your help and your patience! I'm using Access 2007, in XP. I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.