Go Back   Talk Microsoft > Microsoft Operating Systems & Software > Microsoft Office Family

Reply
     Data Grouping In Microsoft Access 2003?  
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-10-2007, 02:40 PM
Ali Ali is offline
Junior Member
 
Join Date: Sep 2007
Posts: 24
Default Data Grouping In Microsoft Access 2003?

I want to make a table which it's fields are "continents" and "countries".
I want to do this with combo box in this way when I select a continent in first field for example "europe" I only see that continent's countries in second field combo box only.
How can I do it?
Reply With Quote
Links
  #2 (permalink)  
Old 10-10-2007, 02:41 PM
Junior Member
 
Join Date: Oct 2007
Posts: 11
Default

Here is another approach

Use two tables. Call them CONTINENT and COUNTRY.

CONTINENT
con_id .... Long (or Integer)
name ...... Text (50)

COUNTRY
con_id .... Long (or Integer)
name ...... Text (50)

Optional (but recommended):
Create a One-To-Many relationship between CONTINENT.con_id and COUNTRY.con_id.

Put two ComboBoxes on a form. Call them cboContinent and cboCountry.

RowSource for cboContinent:
Select CONTINENT.con_id, CONTINENT.name From CONTINENT;
(Hide the Bound Column of cboContinent and set Column Count to 2)


Use NO RowSource for cboCountry (leave it blank).


Enter this code for the OnChange event of cboContinent:

Private Sub cboContinent_Change()
cboCountry.RowSource = "Select COUNTRY.name From COUNTRY Where (COUNTRY.con_id = " & cboContinent & ");"
cboCountry.Requery
End Sub

§
Reply With Quote
  #3 (permalink)  
Old 10-10-2007, 02:41 PM
Junior Member
 
Join Date: Sep 2007
Posts: 12
Default

I think the best way to do this would be to create a method in the VBA code to execute a query based on which continent is selected. For example :

Dim selection As String
Dim sql As String

selection = cboContinentPick.Value

Select case selection
Case "Africa"
sql = "SELECT * FROM tbContinents WHERE Continent = 'Africa'"
Case "Asia"
sql= "SELECT * FROM tbContinents WHERE Continent = 'Asia'"
...

Of course, there are other ways to compact this, such as making a parameter query, but I think you get the gist of it. You will need to create a recordset to hold the countries returned, then add them to the second combobox, something like

cboCountries.AddItem rs1.Fields(i).Value

..where i is a counter in a loop of the number of records returned. There may be easier ways to do this, but I work in Access using ADO, as it is compatible (more portable) than DAO, the native data access technology in Access. If you get something going, and need more help, just let me know. Good luck.
Reply With Quote
Reply


Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 10:03 PM.