【问题标题】:Access VBA Parameter Query Changes Integer to String访问 VBA 参数查询将整数更改为字符串
【发布时间】:2018-01-26 16:15:04
【问题描述】:

好的,我需要帮助来解决应该是一个简单的问题。我正在尝试对 Access 表执行 UPDATE 查询。我在表单的隐藏文本框中有要更新的记录 ID。发生的情况是查询 def 将我的 Integer 存储在参数中时将其更改为字符串。即使在我将值转换为整数之后,它也会这样做。 p6 是参数名称。请参阅下面的代码。我在所有其他具有整数值的字段上都收到数据类型不匹配错误。

    Private Sub SubmitButton_Click()
    Dim db              As DAO.Database
    Dim qdf             As DAO.QueryDef
    Dim strSql          As String
    Dim frm             As Object

    If IsRequiredFilled(Me) = False Then
        MsgBox "Please fill out all required fields.", vbCritical
        Exit Sub
    End If

    Set db = CurrentDb
    strSql = "UPDATE [Batches_T] " & _
             "SET [BatchName] = [BatchName] + [p1], " & _
             "[StatusID] = [StatusID] + [p2], " & _
             "[InternalStatusID] = [InternalStatusID] + [p2], " & _
             "[ReviewerID] = [ReviewerID] + [p3], " & _
             "[StartDate] = [StartDate] + [p4], " & _
             "[PowerPointFilePath] = [PowerPointFilePath] + [p5] " & _
             "WHERE [ID] = [p6]"

    Set qdf = db.CreateQueryDef(vbNullString, strSql)
    With qdf
        .Parameters("p1").Value = Me.BatchName
        .Parameters("p2").Value = Me.StatusID
        .Parameters("p3").Value = Me.InternalStatusID
        .Parameters("p4").Value = Me.StartDate
        .Parameters("p5").Value = Me.PowerPointFilePath
        .Parameters("p6").Value = CInt(Me.ID)
        .Execute dbFailOnError
    End With

    Set qdf = Nothing
    Set db = Nothing

    Forms![Dashboard_F]![Batches_DS_F].Requery
    If Me.keepOpenCheckBox = False Then
        DoCmd.Close acForm, "AddBatch_F", acSaveYes
    End If
End Sub

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:

    尝试为参数添加显式类型声明:

    strSql = "PARAMETERS [p6] INTEGER; " & _
             "UPDATE [Batches_T] " & _
             "SET [BatchName] = [BatchName] + [p1], " & _
             "[StatusID] = [StatusID] + [p2], " & _
             "[InternalStatusID] = [InternalStatusID] + [p2], " & _
             "[ReviewerID] = [ReviewerID] + [p3], " & _
             "[StartDate] = [StartDate] + [p4], " & _
             "[PowerPointFilePath] = [PowerPointFilePath] + [p5] " & _
             "WHERE [ID] = [p6]"
    

    【讨论】:

    • 谢谢!这就是我一直在寻找的。我知道必须有办法做到这一点,只是不知道,也找不到任何地方。完美运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    相关资源
    最近更新 更多