【问题标题】:A character is cut off when data from a textfield in a form containing two dots has to be inserted into a table当必须将包含两个点的表单中的文本字段中的数据插入表中时,字符被截断
【发布时间】:2016-05-06 10:47:08
【问题描述】:

当我想使用以下代码将表单中的数据插入到 MS Access 2010 中的表中时,我收到运行时错误“3075”。 它说:即使文本字段“AbsErst”包含“123.11.11”,查询表达式“123.11.1”中的语法错误。当我在“AbsErst”中输入不带点或只有一个点的内容时,代码会完美运行并将数据插入表中。 我寻找具有相同错误代码的其他问题,但在那里没有发现相同的问题。 期待您的答案或想法 亨里克

Private Sub cmdStore_Click()
  CurrentDb.Execute "INSERT INTO tblAbschnitt(INST_F,GDE_F,ABT_F,RW_F,Erst,Stand) " & " VALUES(" & _
                            Me.cboInst & "," & Me.cboGem & "," & Me.cboAbt & "," & Me.cboRW & "," & Me.AbsErst & "," & Me.absStan & ")"       
End Sub

【问题讨论】:

标签: ms-access syntax vba ms-access-2010 runtime-error


【解决方案1】:

如果要在表格中插入文本(而“123.11.1”文本),则必须在 SQL 语句中用单引号将其括起来。

CurrentDb.Execute "INSERT INTO tblAbschnitt" & _
  "(INST_F,GDE_F,ABT_F,RW_F,Erst,Stand) " & _
  " VALUES(" & Me.cboInst & _
         "," & Me.cboGem & _
         "," & Me.cboAbt & _
         "," & Me.cboRW & _
         ",'" & Me.AbsErst & "'" & _
         "," & Me.absStan & _
         ")"

不仅可以使用Me.AbsErst,还可以使用所有文本列。您必须确保对于所有这些列,要插入的值本身不包含任何单引号。他们需要被另一个单引号转义。 (提示:SQL 注入)

如果您不使用INSERT 语句,所有这些可能会更容易和更安全地完成,而是这样:

With CurrentDb.OpenRecordset("tblAbschnitt")
    .AddNew
    .Fields("INST_F") = Me.cboInst
    .Fields("GDE_F") = Me.cboGem
    .Fields("ABT_F") = Me.cboAbt
    .Fields("RW_F") = Me.cboRW
    .Fields("Erst") = Me.AbsErst
    .Fields("Stand") = Me.absStan
    .Update
End With

这样所有转义和单引号都会自动处理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-12
    • 2013-10-15
    • 2021-04-25
    • 1970-01-01
    • 2013-10-20
    • 2016-09-28
    • 2020-11-04
    • 2023-03-05
    相关资源
    最近更新 更多