子窗体上的空文本框停止VB直到进行父选择

问题描述 投票:-2回答:4

我收到运行时错误

运行时错误2427您输入的表达式没有值。

我知道为什么我得到它我只是不知道如何解决它。 ctrl1ctrl2在子表单上,是两个文本框,在运行后在表单上执行一些If语句,因此文本框为空或者甚至没有数据。它们在子窗体上是空白的。

这是我的代码:

Dim ctrl1 As Control
Dim ctrl2 As Control

Set ctrl1 = Me.Parent.frmRequirementsSubform.Form.txtSumOfCompleted
Set ctrl2 = Me.Parent.frmRequirementsSubform.Form.txtTotalRequirementsNeeded


If ctrl1 = ctrl2 Then
    Call SetLevel(cboArea, txtEmpID, txtDateFunctionCompleted)
End If

这是它正在调用的函数......

    Function SetLevel(lngFuncID As Long, lngEmpID As Long, varDateCompleted As Variant)

 Dim lngPosID As Long
 Dim lngEmpPosID  As Long
 Dim strSQL As String
 Dim strCriteria As String

 strCriteria = "EmpID = " & lngEmpID

 If DCount("*", "tblEmployeeFunctions", strCriteria) = 8 Then
     lngPosID = DLookup("PosID", "tblLevel", "Position = ""Operator 5""")

 ElseIf DCount("*", "tblEmployeeFunctions", strCriteria) = 7 Then
     lngPosID = 0
    Exit Function
 ElseIf DCount("*", "tblEmployeeFunctions", strCriteria) = 6 Then
     lngPosID = DLookup("PosID", "tblLevel", "Position = ""Operator 4""")

 ElseIf DCount("*", "tblEmployeeFunctions", strCriteria) = 5 Then
      lngPosID = 0
   Exit Function
 ElseIf DCount("*", "tblEmployeeFunctions", strCriteria) = 4 Then
     lngPosID = DLookup("PosID", "tblLevel", "Position = ""Operator 3""")

 ElseIf DCount("*", "tblEmployeeFunctions", strCriteria) = 3 Then
      lngPosID = 0
   Exit Function
 ElseIf DCount("*", "tblEmployeeFunctions", _
      strCriteria & " And (FuncID = 1 Or FuncID = 2)") = 2 Then
      lngPosID = DLookup("PosID", "tblLevel", "Position = ""Operator 2""")

 ElseIf lngFuncID = 1 Or lngFuncID = 2 Then
     lngPosID = DLookup("PosID", "tblLevel", "Position = ""Operator 1""")
 End If

 'Debug.Print "lngPosID: " & lngPosID

 If lngPosID > 0 Then

      lngEmpPosID = Nz(DMax("EmpPosID", "tblEmployeeLevel"), 0) + 1
  strSQL = "INSERT INTO tblEmployeeLevel(EmpPosID, EmpID, PosID, DateAchieved) " & _
           "VALUES(" & lngEmpPosID & "," & lngEmpID & "," & lngPosID & "," & _
             IIf(IsNull(varDateCompleted), "NULL", "#" & Format(varDateCompleted, "yyyy-mm-dd") & "#") & ")"

       CurrentDb.Execute strSQL, dbFailOnError
 End If

结束功能

ms-access access-vba
4个回答
0
投票

如果控件在子窗体上,只需直接引用它们,Me.TextBox1无需爬到Me.Parent.SubForm.Form.TextBox1


0
投票

如果我不得不猜测你在某处使用了一个不允许Null值的函数。 @C Perkins是对的,我们需要更多关于错误的细节。但是,如果文本框为空,则可以检查代码,如果不执行代码则可以。

If IsNull(ctrl1) or IsNull(ctrl2) Then
   ExitSub
Else
   'Do Stuff
End If

0
投票

首先,frmRequirementsSubform必须是子窗体控件的名称(可能与子窗体的名称不同)。

然后,插入一些行来显示值,并告诉我们你看到了什么:

Debug.Print "ctrl1: >" & ctrl1.Value & <", ctrl2: >" & ctrl2.Value & "<"
If ctrl1.Value = ctrl2.Value Then
    Debug.Print "cboArea: >" & cboArea & "<, txtEmpID: >" & txtEmpID & "<,  txtDateFunctionCompleted: >" & txtDateFunctionCompleted & "<"
    Call SetLevel(cboArea, txtEmpID, txtDateFunctionCompleted)
End If

要比较Null值,请使用文本:

If Nz(ctrl1.Value, "") = Nz(ctrl2.Value, "") Then

或者,对于数字:

If Nz(ctrl1.Value, 0) = Nz(ctrl2.Value, 0) Then

编辑:

还要检查空对象:

If Not (ctrl1 Is Nothing Or ctrl2 Is Nothing) Then
    If Nz(ctrl1.Value, 0) = Nz(ctrl2.Value, 0) Then

0
投票
Function SetLevel(lngFuncID As Long, lngEmpID As Long, varDateCompleted As Variant)

  Dim lngPosID As Long
  Dim lngEmpPosID  As Long
  Dim strSQL As String
  Dim strCriteria As String

  strCriteria = "EmpID = " & lngEmpID

  If DCount("*", "qryMetalShopEmployeeFunctions", strCriteria) = 8 Then
      lngPosID = DLookup("PosID", "tblMetalShopLevel", "Position = ""Operator 5""")

  ElseIf DCount("*", "qryMetalShopEmployeeFunctions", strCriteria) = 7 Then
      lngPosID = 0

  ElseIf DCount("*", "qryMetalShopEmployeeFunctions", strCriteria) = 6 Then
      lngPosID = DLookup("PosID", "tblMetalShopLevel", "Position = ""Operator 4""")

  ElseIf DCount("*", "qryMetalShopEmployeeFunctions", strCriteria) = 5 Then
       lngPosID = 0

  ElseIf DCount("*", "qryMetalShopEmployeeFunctions", strCriteria) = 4 Then
      lngPosID = DLookup("PosID", "tblMetalShopLevel", "Position = ""Operator 3""")

  ElseIf DCount("*", "qryMetalShopEmployeeFunctions", strCriteria) = 3 Then
       lngPosID = 0

  ElseIf DCount("*", "qryMetalShopEmployeeFunctions", _
       strCriteria & " And (FuncID = 1 Or FuncID = 2)") = 2 Then
       lngPosID = DLookup("PosID", "tblMetalShopLevel", "Position = ""Operator 2""")

  ElseIf lngFuncID = 1 Or lngFuncID = 2 Then
      lngPosID = DLookup("PosID", "tblMetalShopLevel", "Position = ""Operator 1""")
  End If

  If lngPosID > 0 Then

     lngEmpPosID = Nz(DMax("EmpPosID", "tblMetalShopEmployeeLevel"), 0) + 1

     strSQL = "INSERT INTO tblMetalShopEmployeeLevel(EmpPosID, EmpID, PosID, DateAchieved) " & _
            "VALUES(" & lngEmpPosID & "," & lngEmpID & "," & lngPosID & "," & _
              IIf(IsNull(varDateCompleted), "NULL", "#" & Format(varDateCompleted, "yyyy-mm-dd hh:nn:ss") & "#") & ")"

        CurrentDb.Execute strSQL, dbFailOnError
  End If
End Function
© www.soinside.com 2019 - 2024. All rights reserved.