2012-07-26 94 views
0

复制从:https://softwareengineering.stackexchange.com/questions/158330/cascading-comboboxes级联组合框

行,所以我有一个表格,在访问2010中,用1个文本框和3个组合框(1启用& 2关闭)。

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

这一切都适用于新信息,但当我通过窗体回顾信息时,它回到控件上的新模式。

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

如我所表达出来的逻辑(其C & VB的混合体,我知道,但应该得到的PT acrossed):

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

'如果Me.NewRecord Then',则不需要记录集。 – Fionnuala 2012-07-26 15:44:10

回答

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 
+0

你正在为自己生活困难。 #1您不需要记录集即可从当前记录集中获取值。 #2您可以引用组合的行源中的字段或控件。 #3您可以将组合的行源设置为一个sql字符串。 #4我从来没有模拟点击。 – Fionnuala 2012-07-26 16:14:20

+0

我点击模拟,执行其他进程,我真的不想复制/粘贴到此方法。设计的很大一部分原因是我可能不是支持这个文档的人(提示,提示;)),我想这样做让下一个人知道发生了什么。我尽量不要有太多隐含的引用,并尽量明确地尽可能明确,当它不是我会积极支持的东西时。 – GoldBishop 2012-07-26 17:55:18

+0

我不确定对付MS Access会让下一个人更容易。 – Fionnuala 2012-07-26 18:37:21