2013-11-26 69 views
2

我得到一個錯誤,當我使用這個按鈕,因爲如果,給一個錯誤無效的使用null,有人可以解釋嗎?無效使用空Access-VBA

Sub Concluído_Click() 

    Dim novonome As String 
    Dim dataadmi As Date 
    Dim datanasc As Date 
    Dim Email As Hyperlink 
    Dim Cargo As String 
    Dim Categoria As String 
    Dim ingless As String 
    Dim inglesw As String 
    Dim coordeq As String 
    Dim relcli As String 
    Dim Java As String 
    Dim SQL As String 
    Dim PHP As String 
    Dim wserv As String 
    Dim Vendas As String 
    Dim dgraf As String 
    Set table = New ADODB.Recordset 

    With Form_Funcionários 


**If Len(.Nome.value & "") = 0 Or _ 
    ValornaColuna(.Nome.value, "Funcionários", "Nome") = True Then** 
      MsgBox "Campo Nome do Formulario encontra-se vazio ou ja existe! Por favor altere o seu valor!", vbOKOnly, "Campo Vazio" 
     End If 
    End With 


    HideAll 

End Sub 

Private Function ValornaColuna(ByVal value As String, ByVal formTable As String, ByVal formColumn As String) As Boolean 

    Dim valueToCompare As String 
    Dim C As Boolean 
    Set table = New ADODB.Recordset 

    table.Open formTable, CurrentProject.Connection, adOpenDynamic, adLockReadOnly 
    C = False 

    While table.EOF = False And ExistsInColumn = False 
     valueToCompare = table.Fields(formColumn) 

     If StrComp(valueToCompare, value, vbTextCompare) = 0 Then 
      C = True 
     End If 
     table.MoveNext 
    Wend 

    table.Close 
End Function 

回答

2

將拋出一個錯誤(無效使用Null)每當.Nome.value爲Null,因爲ValornaColuna需要字符串作爲第一個參數的數據類型。而Null不是字符串類型。

If Len(.Nome.value & "") = 0 Or _ 
    ValornaColuna(.Nome.value, "Funcionários", "Nome") = True Then 

您可以通過使用Nz一個空字符串傳遞每當.Nome.value爲Null避免錯誤。

If Len(.Nome.value & "") = 0 Or _ 
    ValornaColuna(Nz(.Nome.value, vbNullSting), "Funcionários", "Nome") = True Then