2016-07-26 102 views
0

我有兩個組合框需要在窗體的其餘部分之前填寫。如果用戶移動到第三個框,而前兩個框中的其中一個仍然爲空,我想顯示一條錯誤消息。檢查一個字段在Access 2013中是否沒有焦點

我試過在Form_Current中使用以下內容,但是如果我從Combo1移動到Combo2,它會顯示錯誤。它應該只顯示我是否移動到不是這兩個對象之一的對象。編號: 這是我的工作代碼。

Dim frm As Form 
Dim ctl As Control 

'loop through all controls in form 
For Each ctl In frm.Controls 
    'only combo boxes 
    If ctl.ControlType = acComboBox Then 
     'ensure both boxes are filled in 
     If Not IsNull(Combo1.Value) And _ 
     Not IsNull(Combo2.Value) Then 
      'enable controls on form 
      ctl.Enabled = True 
     Else 
      'disable controls on form 
      ctl.Enabled = False 
     End If 
    End If 
Next 

回答

1

你可以做一個On Current宏設置所有其他領域的Enabled屬性No除非前兩個字段具有有效條目的形式。

+0

你必須指向父窗體子窗體控件的名稱。所以像'[Form]![SubformControl]![Field]' –

+0

謝謝。實際上,我發現它就像你發佈的那樣。我忘了定義變量。 – jjjjjjjjjjj

+0

工程很棒。我用我使用的代碼編輯了我的問題。 – jjjjjjjjjjj

1

試試這個:

If Me.ActiveControl.Name <> Me!Combo1.Name And _ 
    Me.ActiveControl.Name <> Me!Combo2.Name Then 

    If IsNull(Me!Combo1.Value + Me!Combo2.Value) Then 
     ' Error 
    Else 
     ' No Error 
    End If 

End If 
+0

IsNull不能給出類型不匹配,所以仔細檢查你的組合框。 – Gustav

1

Form_Current以及大多數事件只會火如果從當前記錄的記錄來說以某種方式。因此,如果組合框未綁定到記錄,則無法檢測表單中記錄事件的更改。因此我建議使用綁定到comboxes的事件。對兩個組合框都使用Combo_LostFocus(),或者如果您只有一個其他元素,則使用該組合框的Combo_GotFocus()或_MouseDown。

樣品溶液可能是這樣的: 選項比較數據庫

Private Function IsValid() As Boolean 
    IsValid = False 
    If Not IsNull(Me.Combo0.Value + Me.Combo1.Value) Then IsValid = True 
End Function 

Private Sub Combo0_LostFocus() 
    Me.Combo2.Locked = Not IsValid() 
End Sub 

Private Sub Combo1_LostFocus() 
    Me.Combo2.Locked = Not IsValid() 
End Sub 

Private Sub Combo2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) 
    If Not IsValid() Then 
    MsgBox "you need to fill in the others first!" 
    Me.Combo2.Value = "" 
    End If 
End Sub 
相關問題