Reputation: 15
I'm writing the VBA code for a button in an Access form. I want the information inserted by the user to add a new record to a specific table, the table Funcionario
. When I insert data only into the required fields, I get an error message if I have this line of code enabled:
Observacoes = Me.Observacoes.Value
The error says Invalid use of Null
.
Here's the full code, that I'm working on:
Private Sub new_func_Click()
Dim Id_Funcionario As Integer
Dim Primeiro_Nome, Ultimo_Nome, NIF, Sexo, Morada, Localidade, Email, Contacto, Foto, Observacoes As String
Dim Data_Nascimento, Data_contrato_i, Data_contrato_f As Date
Id_Funcionario = Me.Id_Funcionario.Value
Primeiro_Nome = Me.Primeiro_Nome.Value
Ultimo_Nome = Me.Ultimo_Nome.Value
NIF = Me.NIF.Value
Data_Nascimento = Me.Data_Nascimento.Value
Sexo = Me.Sexo.Value
Morada = Me.Morada.Value
Localidade = Me.Localidade.Value
Email = Me.Email.Value
Contacto = Me.Contacto.Value
Data_contrato_i = Me.Data_contrato_i.Value
Observacoes = Me.Observacoes.Value
Foto = Me.Foto.Value
Data_contrato_f = ""
'If IdFunc, NIF and Data_contrato_i empty:
If IsNull(Me.Id_Funcionario.Value) = True Or IsNull(Me.NIF.Value) = True Or IsNull(Me.Data_contrato_i.Value) = True Then
MsgBox "Please insert data in the required fields", vbExclamation, "Warning"
Else
DoCmd.RunSQL "INSERT INTO Funcionario (Id_Funcionario, Primeiro_Nome, Ultimo_Nome, NIF, Data_Nascimento, Sexo, Morada, Localidade, Email, Contacto, Data_contrato_i, Data_contrato_f, Observacoes, Foto) VALUES ('" & Id_Funcionario & "', '" & Primeiro_Nome & "', '" & Ultimo_Nome & "', " & NIF & ", '" & Data_Nascimento & "', '" & Sexo & "', '" & Morada & "', '" & Localidade & "', '" & Email & "', '" & Contacto & "', '" & Data_contrato_i & "', '" & Data_contrato_f & "', '" & Observacoes & "', '" & Foto & "')"
End If
End Sub
Can someone please help me? I don't know what I'm doing wrong, thank you.
Upvotes: 0
Views: 618
Reputation: 19737
This line
Dim Primeiro_Nome, Ultimo_Nome, NIF, Sexo, Morada, Localidade, Email, Contacto, Foto, Observacoes As String
only declares Observacoes
as a String
, all the others are Variants
. To have them all as strings you should use:
Dim Primeiro_Nome As String, Ultimo_Nome As String,....
A Variant
can contain a Null
value, while a String
can't.
You could change the line to:
Observacoes = NZ(Me.Observacoes.Value,"")
which will replace a Null
value with an empty string.
Edit after answer accepted:
As @Erik commented using parameters makes the process a whole lot simpler. The parameters in the code below are MyID
and MyPrimeiro
, but you can use pretty much any text for the parameter name.
Private Sub new_func_Click()
If IsNull(Me.Id_Funcionario) Or IsNull(Me.Primeiro_Nome) Then
MsgBox "Please insert data in the required fields.", vbOKOnly + vbExclamation, "Warning"
Else
With CurrentDb.CreateQueryDef("", _
"PARAMETERS MyID LONG, MyPrimeiro TEXT(255); " & _
"INSERT INTO Funcionario (Id_Funcionario, Primeiro_Nome) VALUES (MyID, MyPrimeiro)")
.Parameters("MyID") = Me.Id_Funcionario
.Parameters("MyPrimeiro") = Me.Primeiro_Nome
.Execute
End With
End If
End Sub
Upvotes: 1