ana
ana

Reputation: 15

Access Vba can't find the Syntax error in Insert Into

I'm writing the following 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 Cliente.

Private Sub novo_cliente_Click()

Dim Id_Cliente As Integer
Dim Primeiro_Nome As String
Dim Ultimo_Nome As String
Dim NIF As String
Dim E_Cliente As String
Dim Sexo As String
Dim Contacto As String
Dim Endereco As String
Dim Localidade As String
Dim Data_Nascimento As Date
Dim Email As String

Id_Cliente = Me.Id_Cliente.Value

Primeiro_Nome = Nz(Me.Primeiro_Nome.Value, Empty)
Ultimo_Nome = Nz(Me.Ultimo_Nome.Value, Empty)
NIF = Nz(Me.NIF.Value, Empty)
E_Cliente = Nz(Me.E_Cliente.Value, Empty)
Sexo = Nz(Me.Sexo.Value, Empty)
Contacto = Nz(Me.Contacto.Value, Empty)
Endereco = Nz(Me.Endereco.Value, Empty)
Localidade = Nz(Me.Localidade.Value, Empty)
Data_Nascimento = Nz(Me.Data_Nascimento.Value, Empty)
Email = Nz(Me.Email.Value, Empty)

'If IdCliente, NIF e e_Cliente empty

If IsNull(Me.Id_Cliente.Value) = True Or IsNull(Me.NIF.Value) = True Or IsNull(Me.E_Cliente.Value) = True Then
    MsgBox "Please insert data in the required fields", vbExclamation, "Warning"
Else
    DoCmd.RunSQL "INSERT INTO Cliente (Id_Cliente, Primeiro_Nome, Ultimo_Nome, NIF, Cliente?, Sexo, Contacto, Endereco, Localidade, Data Nascimento, Email) VALUES (Id_Cliente,'" & Primeiro_Nome & "', '" & Ultimo_Nome & "', '" & NIF & "', E_Cliente, Sexo, '" & Contacto & "', '" & Endereco & "', '" & Localidade & "', Data_Nascimento, '" & Email & "')"

    Me.Id_Cliente.Value = Empty
    Me.Primeiro_Nome.Value = Empty
    Me.Ultimo_Nome.Value = Empty
    Me.NIF.Value = Empty
    Me.E_Cliente.Value = Empty
    Me.Sexo.Value = Empty
    Me.Contacto.Value = Empty
    Me.Endereco.Value = Empty
    Me.Localidade.Value = Empty
    Me.Data_Nascimento.Value = Empty
    Me.Email.Value = Empty

End If
End Sub

Right now, I'm getting a syntax error in INSERT INTO statement, but I cannot find the error. Is it in "Cliente?" ? Thank you.

Upvotes: 1

Views: 98

Answers (1)

TinMan
TinMan

Reputation: 7759

Field names with spaces and special characters need to be bracketed. Data Nascimento should be changed to [Data Nascimento].

DoCmd.RunSQL "INSERT INTO Cliente (Id_Cliente, Primeiro_Nome, Ultimo_Nome, NIF, Cliente?, Sexo, Contacto, Endereco, Localidade, [Data Nascimento], Email) VALUES (Id_Cliente,'" & Primeiro_Nome & "', '" & Ultimo_Nome & "', '" & NIF & "', E_Cliente, Sexo, '" & Contacto & "', '" & Endereco & "', '" & Localidade & "', Data_Nascimento, '" & Email & "')"

Upvotes: 1

Related Questions