Results 1 to 2 of 2

Thread: function that passes the values

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2000
    Posts
    140

    function that passes the values

    Hi,

    Can anyone please help me with creating a function that passes the value.

    Two Access tables:

    Table1 has two fields, Country and Id. And it only has two records in it, let say under Country we have US and Canada, and under Id we have 1 and 2 accordingly.

    Table2 also has two fields, States and Id. and it has 64 records in it, let say under States we have all the name of the states in US and canada, and under id we have 1s and 2s.

    now on form load, comb1 gets populated fron table1. What i would like to be able to do is to get cmb2 populate the name of the states that bases on the country that I've pick from cmb1.

    In other words is. we need to pass the values of the id of the country that we have pick from cmb1. And use that to populate appropriate states to cmb2.

    Thank you in advance

  2. #2
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    If I understand you right, you'd need to add some code to the click event of the first combo (the one with US & Canada in it) tha changes the recordsource of the second combo (the one with all the states) that would be along the lines of
    VB Code:
    1. Private Sub DataCombo1_Change()
    2.  
    3. Dim cn As Connection
    4. Dim rs As Recordset
    5.  
    6. If Len(DataCombo1.BoundText) > 0 Then
    7.     Set cn = New Connection
    8.    
    9.     With cn
    10.         .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\My Documents\db1.mdb;Persist Security Info=False"
    11.         .Open
    12.     End With
    13.  
    14.     Set rs = New Recordset
    15.     rs.Open "SELECT States.StateID, States.State FROM States WHERE States.CountryID=" & DataCombo1.BoundText & " ORDER BY States.State;", cn, adOpenStatic, adLockOptimistic
    16.  
    17.     Set DataCombo2.RowSource = rs
    18.     DataCombo2.BoundColumn = "StateID"
    19.     DataCombo2.ListField = "State"
    20.     DataCombo2.Refresh
    21. End If
    22.  
    23. End Sub
    24.  
    25. Private Sub Form_Load()
    26.  
    27. Dim cn As Connection
    28. Dim rs As Recordset
    29.  
    30. Set cn = New Connection
    31.  
    32. With cn
    33.     .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\My Documents\db1.mdb;Persist Security Info=False"
    34.     .Open
    35. End With
    36.  
    37. Set rs = New Recordset
    38. rs.Open "SELECT Countries.CountryID, Countries.Country FROM Countries ORDER BY Countries.Country;", cn, adOpenStatic, adLockOptimistic
    39.  
    40. Set DataCombo1.RowSource = rs
    41. DataCombo1.BoundColumn = "CountryID"
    42. DataCombo1.ListField = "Country"
    43. DataCombo1.Refresh
    44.  
    45. End Sub
    This requires to data combo boxes to work called DataCombo1 and DataCombo2. Don't forget to change the connection sources to reflect where your data source is.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width