473,594 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Loading a combobox with values that are dependant on another combobox.

54 New Member
HI I have a form with 2 comboBoxes.

The first comboBox gives me a list of customers to select from.

I would like to have the second comboBox populated with the names of Departments. I only want the departments listed that match the Customer selected in the first combobox.

Unfortunatly the original database that the tables were drawn from (SQLServer) has one big Customers Table. It has a CustomerID for every Customer. There are multiple 'Company Name' entries that are the same in the table with different departments.

Expand|Select|Wrap|Line Numbers
  1. i.e.
  2. CustomerID....Customer Name....Department
  3. ....1.........SomeHospital1......dep1
  4. ....2.........SomeHospital1......dep2
  5. ....3.........SomeHospital1......dep3
  6. ....4.........Another Hos........dep1
I Didn't write the old SQL database!!

So I use a SELECT DISTINCT statement to load ComboBox 1 with values. (no repeat Customer Name's loaded).

In ComboBox1 After Update event I have put the following:


Expand|Select|Wrap|Line Numbers
  1. Dim sDepartment As String
  2.  
  3. sDepartment = "SELECT tblCustomers.Department FROM tblCustomers WHERE [tblCustomers.Company Name] =" & Me.cboSelectCustomer.Value
  4.  
  5. Me.cboSelectDepartment.RowSource = sDepartment
  6.  
  7. Me.cboSelectDepartment.Requery
Yet every time I click on the 2nd ComboBox (cboSelectDepar tment) i get a syntax error. I have played around with brackets etc but no go. This is the error I keep getting.

Syntax error (missing operator) in query expression '[tblCustomers.Co mpany Name] = Auckland City Hospital' (That is if I have selected Auckland City Hospital in the first combobox.

If anyone has any ideas how to do this I would be grateful, I cant use the CustomerID to sort unfortunatly. So far I have spent about 2 hours this afternoon reading books and searching the net trying to find why it doesn't work.

Many Thanks
Mike
Dec 10 '06 #1
10 2714
NeoPa
32,566 Recognized Expert Moderator MVP
Replace your line setting sDepartment with :
Expand|Select|Wrap|Line Numbers
  1. sDepartment = "SELECT [Department] " & _
  2.               "FROM tblCustomers " & _
  3.               "WHERE tblCustomers.[Company Name]='" & _
  4.               Me.cboSelectCustomer & "'"
Use Copy / Paste to get it all exactly right.
Dec 10 '06 #2
NeoPa
32,566 Recognized Expert Moderator MVP
FYI You had [] around the whole reference rather than individual reference elements ([tblCustomers.Co mpany Name] =).
The selection had no quotes (') around the value as passed to SQL (...=" & Me.cboSelectCus tomer.Value).
The .Value at the end was unnecessary.
Dec 10 '06 #3
ZaphodBBB
54 New Member
HI NeoPa

Thanks again for your help. I tried it exactly as you have it and what happens is that after I have selected a Value from the first ComboBox and go to click on the arrow in the second ComboBox I get a PopUp box on screen asking me to
"Enter Parameter Value"..."tblCu stomers.Company Name".

Just for interest sake if I do enter a Customers Name in the pop up box and then click O.K. it loads the second combo box with All possible departments not just those from the Customer that has been selected in the first combo.

Again thanks so much for your assistance.

Regards
Mike
Dec 10 '06 #4
ZaphodBBB
54 New Member
HI NeoPa

GOT IT!!!

Sorry your code is 100% as usual!

Problem was when I was opening the Customers Table, at the top where the field names are it was called "Customer Name" out of frustration I went into design view and the Field is called (As might have expected) CustomerName. Feed that into the expression you gave me and all is well.

Is there anywhere (I have about 4 good solid books on Access) any solid info on the Syntax for these type of things? None of the books I have spells it out , matter of fact they barely mention the subject at all. They go into SQL and creating Queries and using the Query Design tool in Access but nothing useful about syntax. Before I typed in your version I swore it wouldn't work! What possible part do the single quotes play or apostrephees play? And why the & sign after the .....Me.cboSele ctCustomer & "'"

Again thanks for all your assistance. There has to be somewhere not just a list of correct syntax but some sort of guide to How and Why...Surely?

Regards
Dec 10 '06 #5
ZaphodBBB
54 New Member
Never realized until now that you could have a Caption for a Field Name that is different from the Field Name itself. Never done any access courses so excuse the ignorance please!

Thanks
Dec 10 '06 #6
NeoPa
32,566 Recognized Expert Moderator MVP
Never realized until now that you could have a Caption for a Field Name that is different from the Field Name itself. Never done any access courses so excuse the ignorance please!

Thanks
To rename fields :
In SQL just say Formula AS NewName.
In the design grid say NewName: Formula.

For quote problems see (Quotes (') and Double-Quotes (") - Where and When to use them.)
Dec 10 '06 #7
ZaphodBBB
54 New Member
Thanks NeoPa
For the reference!!
Dec 12 '06 #8
NeoPa
32,566 Recognized Expert Moderator MVP
No Problems.
There should be a new 'Articles' section up shortly and they will be in there and more visible.
Dec 12 '06 #9
ZaphodBBB
54 New Member
No Problems.
There should be a new 'Articles' section up shortly and they will be in there and more visible.
Great look forward to seeing more of it as it is still confusing to me!!
Dec 16 '06 #10

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

Similar topics

0
1949
by: Ken Arway | last post by:
Using .Net framework 1.1 I've got a Windows Forms application with a tab control having four tab pages. TabPage4 contains a Combobox and a TextBox, which are bound to the same XML file as a DataSource. The ComboBox displays the values from the "name" column of the DataSource and the TextBox displays the "location" value. The two controls are co-ordinated by a BindingManagerBase (bmInfo). When a different "name" item in the ComboBox is...
6
1773
by: Doug Bell | last post by:
Hi I have a datagrid with a combo box, I need to populate the combo with data dependant on the record value. eg for record 1, field Warehouse = 2R so combo would allow selection of locations valid for 2R record 2 has Warehouse = 2M so combo on that row has a selection of locations valid for 2M I can set the combo datasource but can't see how to set it on a row by row basis.
2
1196
by: Richard | last post by:
I have a form with tabpages on them. On one tabpage I have three comboboxes all of which derive their datasource from the same table:- TimePeriodID TimePeriod 1. Day(s) 2. Week(s) 3. Month(s) 4. Year(s)
30
4557
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a corresponding field in the lookup table. In my data table we store the ID in what I will call the 'key' field. == Description of the desired operation:
4
4606
by: jon f kaminsky | last post by:
Hi- I've seen this problem discussed a jillion times but I cannot seem to implement any advice that makes it work. I am porting a large project from VB6 to .NET. The issue is using the combo box bound to a table as a lookup, drawing values from another table to populate the available selections. This all worked fine in VB6. I have distilled the problem down to a simple form drawing data from the Northwind database for a representative...
6
2864
by: dbuchanan | last post by:
VS2005 I've been reading all the help I can on the topic (MSDN, other) but I can't make sense of this. Desired behavior; The user is to choose from the displayed list of the databound combobox and the coresponding 'Id' from the lookup table is to be inserted into the field of the new record. I have two simple tables. "tblPerson" is the data table. The lookup
2
8657
by: shumaker | last post by:
I have a combobox that is very much like the one found in the RSS project here: http://msdn.microsoft.com/vstudio/express/visualCSharp/learning/ My projectNameComboBox basically is filled with a list of values from a table, and as the user selects values, a datagrid displays related records from another table because it is bound via FK relationship. My table: /****** Object: Table . Script Date: 06/19/2006
1
1633
by: Dale | last post by:
I have a user control on a Windows Form that, in its Load event handler, calls a method from a class library to initialize an array of objects that are then used to populate a ComboBox. When I run the app, the method runs just fine. The class library returns the array of objects and the ComboBox is loaded. The problem I am having is when I try to open the form that contains the user control in the designer. Instead of the form, I get...
12
5442
by: Gerhard | last post by:
This is bizarre... Im having problems with the combobox AfterUpdate event: Im running Access 2003. I created an unbound combobox with 3 columns on a form. The Row Source is from a table. Bound Column = 1 and LimitToList = No,,, thus the user can edit the data in the 1st-Column after they selected one of the rows in the combobox. However, I want to save the data of 2nd-Column and 3rd-Column, because if the user edits the 1st-column,...
0
7946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7877
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8253
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8009
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5739
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5411
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3867
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3903
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1216
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.