您好,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表列中插入或更新空值的结果。
如果在Microsoft SQL Server Management Studio中运行相同的查询,则可以进一步支持此功能,因此我可以INSERT和UPDATE Null值。
您可以在OLD ISSUE DETAILS下看到我当前正在使用的INSERT查询。
解决方案19/4/24下午1:35 PST:
似乎访问表链接未使用最新版本的SQL表。刷新链接后,一切正常运行
旧问题详情:
更新:19/4 / 24,10:33 AM PST:实现我忘记提供错误发生的位置。错误发生在QDF.Execute
据我所知,我没有这样做。所有可以为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
我很抱歉代码块墙。
如果有人知道为什么或可以弄清楚为什么会发生此错误,我将非常感谢。
UPDATE 4/24/19 11:10 AM PST:基于HansUp的建议,我使用RST.AddNew方法More details can be found here创建了一个备用插入方法。完成此操作后,我发现导致悲伤的变量是InsertProject函数中的projectCanceledDate。唯一的问题是我不知道为什么,该变量被定义为Variant。
UPDATE 4/24/19 11:43 AM PST:经过更多测试之后。我发现我可以在Microsoft SQL Server Management Studio中为project_cancleddate插入和更新NULL值。
答案 0 :(得分:1)
进行更多实验后,我发现了问题所在。似乎SQL Server中的更改未反映在访问数据库中(即使project_canceleddate
接受空值)。刷新ci_project
表上的链接后,一切正常。
答案 1 :(得分:0)
在“呼叫插入项目”中,未为字段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