我有一個帶有 2 個組合框(Combobox1 和 Combobox2)以及“保存”和“取消”按鈕的用戶表單。我現在的目標是,每當我選擇一個組合框中的一項時,另一項應該被“阻止”或禁用。因此,當按下保存按鈕時,它只能從這兩個組合框中的一個中保存一項。
這就是我已經走了多遠:
If ComboBox1.Text = "" Then Cancel = 1
MsgBox "Data couldn't be saved. Insert item."
ElseIf Combobox1.Value > 0 And Combobox2.Text = "" Then
If Combobox2.Text = "" Then Cancel = 1 MsgBox "Data couldn't be saved. Insert item."
ElseIf Combobox2.Value > 0 And Combobox1.Text = "" Then
If Combobox1.Value > 0 And Combobox2.Value > 0 Then Cancel = 1 MsgBox "Select only one item."
現在的問題是,當我為 combobox1 選擇一個專案,為 combobox2 選擇一個專案時,它仍然會保存它。
uj5u.com熱心網友回復:
在您的用戶表單中使用以下內容:
Option Explicit
Private Sub CancelButton_Click()
'reset boxes
Me.ComboBox1.Value = vbNullString
Me.ComboBox2.Value = vbNullString
End Sub
Private Sub ComboBox1_Change()
' disable box 2 if box 1 has a value
Me.ComboBox2.Enabled = (Me.ComboBox1.Value = vbNullString)
End Sub
Private Sub ComboBox2_Change()
' disable box 1 if box 2 has a value
Me.ComboBox1.Enabled = (Me.ComboBox2.Value = vbNullString)
End Sub
Private Sub SaveButton_Click()
If Me.ComboBox1.Value <> vbNullString Then
MsgBox "Box 1 has the value"
ElseIf Me.ComboBox2.Value <> vbNullString Then
MsgBox "Box 2 has the value"
Else
MsgBox "In no box was selected a value"
End If
End Sub

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/440546.html
上一篇:每個鍵的值的字典回圈
下一篇:如何計算檔案夾中的檔案
