Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

vba - How to query combo box of only current record/row in Access data entry form?

I have created a data entry form in Access that uses combobox for entering farmer name. The combobox is used for ease and to make sure only farmers from the list are entered. For ease combo box is re-queried as you type in. The combobox works well for the first entry but previous farmers' names are vanished when queried for the next row. I think, Access is requerying all dropdowns rather than the current drop-down/combo-box.

Form

The VBA for the querying drop down is given below:

 Public Sub FilterComboAsYouType(combo As ComboBox, defaultSQL As String, 
   lookupField As String)
   Dim strSQL As String
     If Len(combo.Text) > 0 Then
    strSQL = defaultSQL & " AND " & lookupField & " LIKE '*" & combo.Text & 
   "*'"
    Else
       strSQL = defaultSQL   'This is the default row source of combo box
   End If
    combo.RowSource = strSQL
    combo.Dropdown

  End Sub

Private Sub Combo137_Change()
    FilterComboAsYouType Me.Combo137, "SELECT  farmer.name,farmer.ID FROM farms INNER JOIN farmer ON 
 farms.ID = farmer.farm_id where farms.ID LIKE" & "'" & Form_Name & "*'", "farmer.name"
 End Sub

Private Sub Combo137_GotFocus()
If Form_Name <> "" Then
FilterComboAsYouType Me.Combo137, "SELECT  farmer.name,farmer.ID FROM farms INNER JOIN farmer ON 
farms.ID = farmer.farm_id where farms.ID LIKE" & "'" & Form_Name & "*'", "farmer.name"
Else
FilterComboAsYouType Me.Combo137, "SELECT  farmer.name,farmer.ID FROM farms INNER JOIN farmer ON 
  farms.ID = farmer.farm_id where farms.ID LIKE" & "'" & "NONE" & "*'", "farmer.name"
End If
End Sub
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, all records will show the same filtered list because there is only one combobox and property settings are reflected in all instances. Filtering a combobox RowSource based on value in another field/control is known as "cascading" or "dependent". Also, your RowSource has alias - value saved is not value displayed. When the list is filtered the display alias will not be available for records that have saved value which has been filtered out. This is a well-known issue of cascading combobox. Options for dealing with:

  1. for any form style, only filter the list for new record or when primary value is changed, then reset to full list for existing records

  2. for forms in Continuous or Datasheet view, include lookup table in form RecordSource, bind a textbox to descriptive field from lookup table, position textbox on top of combobox, set textbox as Locked Yes and TabStop No


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...