访问VBA参数查询将整数更改为字符串

问题描述 投票:0回答:1

好的,我需要帮助解决应该是一个简单的问题。我正在尝试对Access表执行UPDATE查询。我在我的表单上的隐藏文本框中有要更新的记录的ID。会发生的是,查询def将我的Integer在将其存储到参数中时将其更改为字符串。即使我将值转换为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
access-vba
1个回答
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]"
© www.soinside.com 2019 - 2024. All rights reserved.