【问题标题】:Access Vba can't find the Syntax error in Insert IntoAccess Vba 在 Insert Into 中找不到语法错误
【发布时间】:2019-05-19 09:49:18
【问题描述】:

我正在为 Access 表单中的按钮编写以下 VBA 代码。我希望用户插入的信息能够将新记录添加到特定表,即 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

现在,我在 INSERT INTO 语句中遇到语法错误,但我找不到错误。是在“客户”中吗? ?谢谢。

【问题讨论】:

  • 除了列名中的特殊字符 ? 之外,即使在 VALUES 中使用未引用或未连接的名称,该查询如何运行?:Id_Cliente E_ClienteSexoData_Nascimento。 MS Access 应该会引发有关这些未知值的错误。

标签: vba ms-access syntax-error


【解决方案1】:

带有空格和特殊字符的字段名称需要用括号括起来。 Data Nascimento 应改为[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 & "')"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多