0

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

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

第一个 ComboBox 不绑定到数据源,但对其他 2 个组合框是主观的。所以我处理了第一个 Combobox 的 Click 事件,然后启用其他 2 个,并使用基于第一个 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
4

1 回答 1

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
于 2012-07-26T16:10:14.873 回答