【问题标题】:Why does "Enter parameter value" appears using INSERT INTO VALUES?为什么使用 INSERT INTO VALUES 会出现“输入参数值”?
【发布时间】:2021-05-22 14:06:31
【问题描述】:

当我尝试运行此代码时,我总是收到“输入参数值错误”。错误涉及 idPat、idSP、idPar 和 measure_value。 我已经检查了调试 idPar、idPat、idSP 和 measure_value,它们假定了正确的值;运行 sql 时出现错误。

     Dim idPar As Integer
     Dim idPat As Integer
     Dim idSP As Integer
     Dim current_fc As String
     Dim namePar As String
     Dim sql As String
     Dim measureValue As Integer
    
    current_fc = TempVars!CurrentUsername
    namePar = Me.cmbManualDailyPar.Value
    measureValue = Me.txtMeasureValue

    idPar = Nz(DLookup("ID_par", "Parameter", "name_par = '" & namePar & "'"), 0)
    idPat = Nz(DLookup("ID_patient", "Patient", "fiscal_code = '" & current_fc & "'"), 0)
    idSP = Nz(DLookup("ID_SP", "Diagnosis", "ID_patient = " & idPat & ""), 0)

    
    sql = "INSERT INTO Measure(ID_par, measure_value,ID_SP,ID_patient)" & _
            " VALUES(idPar,measureValue,idSP,idPat);"
            
    DoCmd.RunSQL sql

【问题讨论】:

  • idPat 在分配给idSP 时尚未定义。你的意思是idPar
  • 不,我的错误我已经编辑了它,但仍然出现同样的错误
  • 我们正在讨论的错误与此错误无关。
  • @Lea 。 . .打印出 SQL 并查看它是否直接在 MS Access 中运行。从那里开始调试它。

标签: sql vba ms-access


【解决方案1】:

您的VALUES 部分只是文本。你需要的是:

" VALUES(" & idPar & "," & measureValue & "," & idSP & "," & idPat & ")"

您可能还需要将文本值括在数据库引号中,例如:

" VALUES(" & idPar & ", '" & measureValue & "'," & idSP & "," & idPat & ")"

如果是文本,请附上measureValue

【讨论】:

  • 问题是我想添加整数类型,用VALUES可以吗?
  • 是的,如上图。如果idSP是一个整数,它的值会被插入到VALUES的文本中。
  • 试一试,在DoCmd之前使用调试器检查sql
  • 在我尝试过你的建议之前,但正如我所料,我无法追加行,因为表中的字段是整数类型而不是字符串类型
  • 哦,对不起。如何进行检查?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-26
  • 1970-01-01
  • 2012-07-06
  • 1970-01-01
  • 1970-01-01
  • 2014-03-02
相关资源
最近更新 更多