级联组合框

时间:2012-07-26 15:38:26

标签: access-vba ms-access-2010 office-2010

复制自:https://softwareengineering.stackexchange.com/questions/158330/cascading-comboboxes

好的,所以我在Access 2010中有一个表单,包含1个文本框和3个组合框(1个已启用&2个已禁用)。

第一个ComboBox与数据源无关,但对其他2个组合框是主观的。所以我处理了第一个Combobox的Click事件,然后启用了另外两个,并使用基于第一个ComboBox值动态构建的自定义RowSource SQL脚预加载第二个ComboBox。

这一切都适用于新信息,但是当我通过表格回复查看信息时,它会回到控件上的新模式。

问题: 我需要处理什么事件来检查当前表单数据是否包含控件源控件的数据?

正如我在Logic中表达的那样(它是C& VB之间的混合,我知道但是应该让pt交叉):

DataSet ds = Form.RowSet
if (ds = Null) then 
  cbo2.enabled = false
  cbo3.enabled = false
else
  cbo2.rowsource = "select id, nm from table"
  cbo2.value = ds(3)
  cbo3.value = ds(4)
end if
... do some other logic ...

更新逻辑 - 仍有问题,由于某些原因无法捕获RecordStatus(提供3251运行时错误)

Private Sub Form_Current()
    Dim boolnm As Boolean: boolnm = (IsNull(txtName.Value) Or IsEmpty(txtName.Value))
    Dim booltype As Boolean: booltype = IsNull(cboType.Value)
    Dim boolfamily As Boolean: boolfamily = IsNull(cboType.Value)
    Dim boolsize As Boolean: boolsize = IsNull(cboType.Value)

    Dim rs As DAO.Recordset: Set rs = Me.Recordset
    MsgBox rs.AbsolutePosition

'    If rs.RecordStatus = dbRecordNew Then
'        MsgBox "New Record being inserted, but not committed yet!", vbOKOnly
'    Else
'        MsgBox rs(0).Name & " - " & rs(0).Value & vbCrLf & _
'            rs(1).Name & " - " & rs(1).Value & vbCrLf & _
'            rs(2).Name & " - " & rs(2).Value & vbCrLf & _
'            rs(3).Name & " - " & rs(3).Value
'    End If
    'MsgBox "Name: " & CStr(boolnm) & vbCrLf & _
            "Type: " & CStr(booltype) & vbCrLf & _
            "Family: " & CStr(boolfamily) & vbCrLf & _
            "Size: " & CStr(boolsize), vbOKOnly

End Sub

1 个答案:

答案 0 :(得分:0)

这是最终结果,在Remou的帮助下,这只是最终结果的前奏(这不在问题的背景下)。

Private Sub Form_Current()

    If Me.NewRecord Then   <=======================
        cboType.Value = 0
        cboType.Enabled = True
        cboFamily.Enabled = False
        cboSize.Enabled = False
    Else
        Dim rs As DAO.Recordset: Set rs = Me.Recordset
        'get Family ID
        Dim fid As String: fid = rs(2).Value
        'Build SQL Query to obtain Type ID
        Dim sql As String
        sql = "select tid from tblFamily where id = " & fid
        'Create Recordset
        Dim frs As DAO.Recordset
        'Load SQL Script and Execute to obtain Type ID
        Set frs = CurrentDb.OpenRecordset(sql, dbOpenDynaset, dbReadOnly)
        'Set Type ComboBox Value to Type ID
        cboType.Value = frs(0)
        cboType_Click 'Simulate Click Event since the Value has changed

        'Make sure all 3 Comboboxes are enabled and useable
        cboType.Enabled = True
    End If

End Sub