![]() |
|
|||||||
![]() |
|
Data Grouping In Microsoft Access 2003?
|
![]() |
LinkBack | Thread Tools | ![]() |
Display Modes | ![]() |
|
|||
|
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? |
| Links |
|
|
|
|||
|
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 § |
|
|||
|
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. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|