我有這個用戶表單,我在其中使用property get. 它以這種方式作業:
1.如果單擊“確定”,則用戶表單隱藏并運行模塊中的程式。
2.如果未選擇任何內容,則會出現 MsgBox 并再次顯示用戶表單,直到選擇了一個選項
3.如果點擊“取消”,資訊應該被卸載并退出宏。
問題是第三步沒有發生。當它被點擊“取消”property get運行。此外,如果用戶沒有選擇任何選項兩次,模塊中的程序無論如何都會運行,這會產生錯誤。我猜這個問題與不在property get“OK”程序中呼叫有關。任何建議我怎么能管理它?
在用戶表單中
Private Sub cmdAceptar_Click()
Me.Hide
End Sub
Private Sub cmdCerrar_Click()
Unload Me
End Sub
Public Property Get Sup() As String
If optSupA = True Then
Sup = "SuplierA"
ElseIf optSupB = True Then
Sup = "SuplierB"
ElseIf optSupC = True Then
Sup = "SuplierC"
Else
MsgBox "A suplier should be selected", vbExclamation
Me.Show
End If
End Property
在模塊中
Dim result as String
Public Sub TEST()
'Show userform modal
frmSuplier.Show vbModal
result = frmSuplier.Sup
'Call Calculate_prices
Call Calculate_price(result)
'now unload the form
Unload frmSuplier
End Sub
uj5u.com熱心網友回復:
我的建議是將整個 IF/ELSE 樹移到Control_Change事件中,無論用戶表單的哪個部分正在獲取optSupA, optSupB,的值optSupC。本Property Get Sup應該像Sup = SavedValue沒有MSGBOX或與用戶表單控制元件的互動。
假設有 3 個選項按鈕,以下是我的設定方式:
Private SupValue As String
Private Sub cmdAceptar_Click()
If Me.OptionButton1.Value Or Me.OptionButton2.Value Or Me.OptionButton3.Value Then
Me.Hide
Else 'None of the options are currently selected
MsgBox "A suplier should be selected", vbExclamation
End If
End Sub
Private Sub cmdCerrar_Click()
Unload Me
End Sub
Public Property Get Sup() As String
Sup = SupValue
End Property
Private Sub OptionButton1_Change()
If Me.OptionButton1.Value Then
Me.OptionButton2.Value = False
Me.OptionButton3.Value = False
SupValue = "SupplierA"
End If
End Sub
Private Sub OptionButton2_Change()
If Me.OptionButton2.Value Then
Me.OptionButton1.Value = False
Me.OptionButton3.Value = False
SupValue = "SupplierB"
End If
End Sub
Private Sub OptionButton3_Change()
If Me.OptionButton3.Value Then
Me.OptionButton1.Value = False
Me.OptionButton2.Value = False
SupValue = "SupplierC"
End If
End Sub
通過這些更改,取消按鈕卸載用戶表單,擦除 SupValue。所以在你的模塊中,result將是空白的,你可以添加一個 if/else 來處理這些情況,比如If result = "" Then Exit Sub
使用取消按鈕卸載,然后呼叫frmSuplier.Sup實際上重新初始化表單。所以為了避免這種雙重初始化和雙重卸載,將取消按鈕更改為:
Private Sub cmdCerrar_Click()
SupValue = ""
Me.Hide
End Sub
這樣,result = ""當用戶按下取消時,您仍然會得到,但您只執行一次加載和卸載。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/371431.html
