2012-07-26 95 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