强制填写所有字段

时间:2016-06-08 19:03:10

标签: vba ms-access access-vba

这个问题从这里继续:Add user input to Excel table upon upload to Access database

现在,我的字段已连接到数据库中的表,我想确保每个人填写它们。单击“导入”按钮后,我想检查字段(SANumber,SerialNumber,CustomerName和LyoSize)以确保它是一个有效的上传'。

到目前为止我有这个代码:

Function CheckInputs() As Boolean

If Me.SANumber.value Or Me.SerialNumber.value Or Me.CustomerName.value Or Me.LyoSize.value = Null Then
 CheckInputs = True
Else
 CheckInputs = False
End If

 End Function

'Import MCL Files Code
Private Sub ImportMCL_Click()

On Error GoTo ErrorHandler
'disable ms access warnings
DoCmd.SetWarnings False

Call CheckInputs
If CheckInputs = True Then
MsgBox "All inputs must be entered!"
Exit Sub
Else

'load spreadsheet in .xls format
DoCmd.TransferSpreadsheet acImport, 8, "_MCL_UPLOAD", selectFile(), True
DoCmd.OpenQuery "UpdateMCL"
Call InsertInto_MASTER_UPLOAD
Call Delete_MCL_UPLOAD
MsgBox "MCL Imported Successfully!"
're-enable ms access warnings
DoCmd.SetWarnings True    
End If

Exit Sub

ErrorHandler:
MsgBox "There was an Error: " & Err & ": " & Error(Err)

End Sub

它应该有用,但不断给我

  

错误:13。键入不匹配

3 个答案:

答案 0 :(得分:2)

您需要专门检查每个字段是否为null - 您不能这样做:

If Me.SANumber.value Or Me.SerialNumber.value Or _
   Me.CustomerName.value Or Me.LyoSize.value = Null Then

这样的东西
If IsNull(Me.SANumber) Or IsNull(SerialNumber) Or _
   IsNull(Me.CustomerName) Or IsNull(Me.LyoSize) = Null Then

您应该将您的功能重命名为" EmptyInputs"使您的代码更自我记录。 " CheckInputs"有点不具描述性。

答案 1 :(得分:1)

CheckInputs()函数逻辑错误。或者如果符合任何一个条件,则返回true。
为了得到你想要的结果,你可以问:是 如果Condition1 = true且Condition2 = true AND ....
否则你可以询问如果Condition1 = false或Condition2 = false OR .... 试试这个....

Function isFormValid() As Boolean

    If isTextFieldInvalid(Me.SANumber) Or isTextFieldInvalid(Me.SerialNumber) Or isTextFieldInvalid(Me.CustomerName.Value) Or Me.LyoSize.Value = Null Then
        isFormValid = False
    Else
        isFormValid = True
    End If

End Function

Function isTextFieldInvalid(FieldControl) As Boolean
    If Not IsNull(FieldControl) Then
        If Len(Trim(FieldControl.Value)) Then
            isFieldValid = True
        End If
    End If
End Function

'Import MCL Files Code
Private Sub ImportMCL_Click()

    On Error GoTo ErrorHandler
    'disable ms access warnings
    DoCmd.SetWarnings False

    If isFormValid Then
        MsgBox "All inputs must be entered!"
        Exit Sub
    Else

        'load spreadsheet in .xls format
        DoCmd.TransferSpreadsheet acImport, 8, "_MCL_UPLOAD", selectFile(), True
        DoCmd.OpenQuery "UpdateMCL"
        Call InsertInto_MASTER_UPLOAD
        Call Delete_MCL_UPLOAD
        MsgBox "MCL Imported Successfully!"
        're-enable ms access warnings
        DoCmd.SetWarnings True
    End If

    Exit Sub

ErrorHandler:
    MsgBox "There was an Error: " & Err & ": " & Error(Err)

End Sub

答案 2 :(得分:1)

此外,如果你之后通过像SANumber =“”这样的东西清理,那么测试Null可能不起作用。我会检查空值和空白。这是您可以使用的通用模板。

Dim LResponse As Integer
If (Nz(Me.SANumber.Value) = "") Then
    MsgBox "Please enter a SA Number.", vbCritical + vbOKOnly, "Error"
ElseIf (Nz(Me.SerialNumber.Value) = "") Then
   MsgBox "Please enter a Serial Number.", vbCritical + vbOKOnly, "Error"

'All criteria met
Else
    LResponse = MsgBox("Would you like to submit? ", vbQuestion + vbYesNo, "Question")
    If LResponse = vbYes Then
        'enter code here
    ElseIf LResponse = vbNo Then
        MsgBox ("Not submitted.")
    End If
End If