2016-06-08 32 views
0

來填充這個問題就是從這裏繼續。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 

它應該工作,但繼續給我

ERROR: 13. Type Mismatch

+0

錯誤在哪一行? – newguy

+0

我假設它在CheckInputs()函數的某個地方,但沒有具體顯示在調試器中。當所有的輸入字段都是空的,它很高興地接受上傳,但是當我把它們全部填入(它們應該是的方式)時,我得到這個錯誤。 – cdomination

+0

在「如果Me.SANumber.Value或....」上添加一個斷點。當代碼暫停時,檢查立即窗口中所有這些字段的值,看看是否有任何給你具體的錯誤。如果他們這樣做,那些信息對我們診斷問題將會有幫助。 –

回答

2

您需要特別檢查每個字段爲空 - 你不能做這個方法:

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」有點不具說明性。

+0

我只是將其更改爲:Me.SANumber.value = Null或Me.SerialNumber.value = Null或Me.CustomerName.value = Null或Me.LyoSize.value = Null ...我現在不再收到錯誤,但代碼完全瀏覽了該函數(如果沒有填充任何值,它仍然接受導入) – cdomination

+0

這很好。問題可能是因爲我擁有我。*。價值?我也改變了函數名稱。謝謝! – cdomination

+1

你的代碼只測試了* last *字段的Null值 - 所有其他字段都只是針對「真實性」進行了有效測試(我不熟悉Access,所以我不能確切地說在實際中這將意味着什麼) –

1

您的CheckInputs()函數邏輯不正確。或者如果滿足任何一個條件將返回true。
要得到您想要的結果,您可以這樣做:
如果Condition1 = true AND Condition2 = true AND ...
否則,您可以詢問If Condition1 = false OR 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 
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 
相關問題