访问不允许SQL-Server列中的空值的INSERT或UPDATE(访问运行时错误3162)

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

你好stackoverflow的广阔世界,

问题: 我目前正在开发一个使用访问表单来获取和插入数据的sql-server数据库。在昨天之前一切都很好。现在的问题是我在尝试运行插入查询时收到以下错误。

Access Runtime Error '3162': You tried to assign the Null value to a variable that is not a Variant data type.

问题详情: 这似乎是Access不允许我在允许空值的SQL表列中INSERT或UPDATE空值的结果。 如果我在Microsoft SQL Server Management Studio中运行相同的查询,我可以进一步支持此操作,我可以INSERT和UPDATE Null值。 您可以在OLD ISSUE DETAILS下查看我当前正在使用的INSERT查询。

解决方案4月24日下午1:35太平洋标准时间: 似乎访问表链接没有使用最新版本的SQL表。刷新链接后,一切正常


旧问题详情: 更新:太平洋标准时间4/24/19,太平洋标准时间上午10:33:已实现我忘了提供错误发生的位置。错误发生在QDF.Execute的InsertProject函数中

据我所知,我不是这样做的。所有可以为null的表列都是这样设置的,并且vba中的变量似乎可以从我看到的内容中正确设置。

我提供了与此问题相关的代码,查询和其他信息。

这是我正在使用的查询:


    INSERT INTO ci_project 
    (project_id, 
    project_group, 
    project_projecttype, 
    project_hasmilestones, 
    project_hasgantt, 
    project_category, 
    project_difficulty, 
    project_notes, 
    project_completiondetails, 
    project_statuscode, 
    project_inprogressdate, 
    project_completeddate, 
    project_datecreated, 
    project_lastupdated, 
    project_canceleddate, 
    project_isActive) 
    VALUES (@projectID
    ,@projectGroup
    ,@projectType
    ,@projectHasMilestones
    ,@projectHasGantt
    ,@projectCategory
    ,@projectDifficulty
    ,@projectNotes
    ,@projectCompletionDetails
    ,@projectStatus
    ,@projectDateInProgress
    ,@projectDateComplete
    ,@projectDateCreated
    ,@projectDateUpdated
    ,@projectCanceledDate
    ,@projectIsActive);

这是我为使其更容易多次调用而创建的函数:


    'This is located in another Module called OtherFunctions
    Dim dbs as DAO.Database

    Public Function InsertProject(projectID As String, _
                                  projectGroup As Integer, _
                                  projectType As Integer, _
                                  projectHasMilestones As Boolean, _
                                  projectHasGantt As Boolean, _
                                  projectCategory As Integer, _
                                  projectDifficulty As Integer, _
                                  projectNotes As Variant, _
                                  projectCompletionDetails As Variant, _
                                  projectStatus As Integer, _
                                  projectDateInProgress As Variant, _
                                  projectDateComplete As Variant, _
                                  projectCanceledDate As Variant, _
                                  projectIsActive As Integer)

        OtherFunctions.Initialize

        Dim QDF As DAO.QueryDef

        If FindQuery("InsertProject") = True Then OtherFunctions.dbs.QueryDefs.Delete "InsertProject"

        Set QDF = OtherFunctions.dbs.CreateQueryDef("InsertProject", SQLInsertProject)

        QDF.Parameters("@projectID").Value = projectID
        QDF.Parameters("@projectGroup").Value = projectGroup
        QDF.Parameters("@projectType").Value = projectType
        QDF.Parameters("@projectHasMilestones").Value = projectHasMilestones
        QDF.Parameters("@projectHasGantt").Value = projectHasGantt
        QDF.Parameters("@projectCategory").Value = projectCategory
        QDF.Parameters("@projectDifficulty").Value = projectDifficulty
        QDF.Parameters("@projectNotes").Value = projectNotes
        QDF.Parameters("@projectCompletionDetails").Value = projectCompletionDetails
        QDF.Parameters("@projectStatus").Value = projectStatus
        QDF.Parameters("@projectDateInProgress").Value = ConvertDateToUnix(projectDateInProgress)
        QDF.Parameters("@projectDateComplete").Value = ConvertDateToUnix(projectDateComplete)
        QDF.Parameters("@projectDateCreated").Value = ConvertDateToUnix(Now())
        QDF.Parameters("@projectDateUpdated").Value = ConvertDateToUnix(Now())
        QDF.Parameters("@projectIsActive").Value = projectIsActive
        QDF.Parameters("@projectCanceledDate").Value = ConvertDateToUnix(projectCanceledDate)

        QDF.Execute

        If FindQuery("InsertProject") = True Then OtherFunctions.dbs.QueryDefs.Delete "InsertProject"

        Set QDF = Nothing

    End Function

这是我调用函数的地方:


    'These are set in the same sub as the insert project call
    Dim projectID As String
    Dim CancelDate As Variant
    Dim canceledStatus As Integer

    'These are located in a different module called OtherFunctions
    Public IDEASUGGESTION_HASGANT As Boolean
    Public IDEASUGGESTION_HASMILESTONES As Boolean
    Public IDEASUGGESTION_PROJECTTYPE As Integer


    ' /\/\/\ THERE IS CODE ABOVE THIS /\/\/\
        canceledStatus = 12
    If Me.IdeaStatus = canceledStatus And DatabaseQueries.CheckIdeaSuggestion(Me.IdeaID) = True Then
       CancelDate = Now()
       If MsgBox("Are you sure you want to do this? Canceling a idea will make it un-editable.", vbYesNo) = vbYes Then
           GoTo IdeaCancel
       Else
           GoTo GotoEnd
       End If
    ElseIf Me.IdeaStatus = canceledStatus And DatabaseQueries.CheckIdeaSuggestion(Me.IdeaID) = False Then
       MsgBox "You cannot cancel an idea that does not exist.", vbExclamation
       CancelDate = Null
       GoTo GotoEnd
    Else
       'other code run here not pertaining to the insert
    End If

    Call DatabaseQueries.InsertProject(
         projectID, _
         Me.IdeaGroup, _
         OtherFunctions.IDEASUGGESTION_PROJECTTYPE, _
         OtherFunctions.IDEASUGGESTION_HASMILESTONES, _
         OtherFunctions.IDEASUGGESTION_HASGANT, _
         Me.IdeaCategory, _
         Me.IdeaDifficulty, _
         Null, _
         Null, _
         Me.IdeaStatus, _
         Me.IdeaInprogressDate, _
         Me.IdeaCompleteDate, _
         CancelDate, _
         1)

    ' \/\/\/ THERE IS CODE BELOW THIS \/\/\/

当我运行它时,这些是表单值:


    Me.ideaID = Null
    Me.IdeaGroup = 1
    Me.IdeaCategory = 2
    Me.IdeaDifficulty = 1
    Me.IdeaStatus = 1
    Me.IdeaInprogressDate = Null
    Me.IdeaCompleteDate = Null
    OtherFunctions.IDEASUGGESTION_PROJECTTYPE = 1
    OtherFunctions.IDEASUGGESTION_HASMILESTONES = False
    OtherFunctions.IDEASUGGESTION_HASGANT = False

表结构是:

    Column Name                 Data Type   Can be Null
    project_id                  varchar(45) No
    project_group               int         No
    project_projecttype         int         No
    project_hasmilestones       bit         No
    project_hasgantt            bit         No
    project_category            int         No
    project_difficulty          int         No
    project_notes               text        Yes
    project_completiondetails   text        Yes
    project_statuscode          int         No
    project_inprogressdate      bigint      Yes
    project_completeddate       bigint      Yes
    project_datecreated         bigint      No
    project_lastupdated         bigint      No
    project_canceleddate        bigint      Yes
    project_isActive            int         No

我对代码块墙感到抱歉。

如果有人知道为什么或可以弄清楚为什么会发生这种错误,我将非常感激。

更新4月24日上午11:10太平洋标准时间:根据HansUp的赞扬,我使用RST.AddNew方法More details can be found here创建了一个替代插入方法。执行此操作后,我发现引起悲伤的变量是InsertProject函数中的projectCanceledDate。唯一的问题是我不知道为什么,变量被定义为Variant。

更新4月24日上午11:43太平洋标准时间:做了一些更多的测试。我发现我能够在Microsoft SQL Server Management Studio中插入和更新project_cancleddate的NULL值。

sql sql-server ms-access access-vba
2个回答
1
投票

在做了一些实验之后我发现了问题。似乎SQL服务器中的更改未反映在访问数据库中(即使project_canceleddate接受空值)。刷新ci_project表上的链接后一切正常。


0
投票

在Call InsertProject中,字段projectID和CancelDate没有给出任何值。他们用的是什么?

添加这个简单的代码来查看参数中的内容。

Dim parm As DAO.Parameter
For Each parm In QDF.Parameters
    Debug.Print parm.Name, parm.Value
Next parm

这就是我得到的

  @projectID                  MyProjId
  @projectGroup               1
  @projectType                1
  @projectHasMilestones       0
  @projectHasGantt            0
  @projectCategory            2
  @projectDifficulty          1
  @projectNotes               Null
  @projectCompletionDetails   Null
  @projectStatus              1
  @projectDateInProgress      Null
  @projectDateComplete        Null
  @projectDateCreated         1556103601
  @projectDateUpdated         1556103601
  @projectCanceledDate        Null
  @projectIsActive            1
© www.soinside.com 2019 - 2024. All rights reserved.