我有数据库表,需要使用表单更新表。 但是,表格中的所有字段都不必强制填写。 我正在编写下面的VB代码用于插入但是我得到一个错误,声明语句中存在语法错误。据我所知,这是因为变量中的空值。我知道我需要对所有null值使用DBNull.Value。 在这里Thing是有太多的字段来检查值是否为null。 任何机构建议是否有办法对输入的值进行质量检查为空?
VBCode:
Dim StrSQL As String
Dim StrSQL1 As String
Dim tktID As Variant
Dim Assi As Variant
Dim reopened As Variant
Dim valid As Variant
Dim Reopenreason As Variant
Dim ReassignmentAG As Variant
Dim RBSCollab As Variant
Dim SMEconf As Variant
Dim Cloabag As Variant
Dim smeName As Variant
Dim Updat As Variant
Dim Closed As Variant
Dim iss As Variant
Dim ana As Variant
Dim res As Variant
Dim rVariant As Variant
' Assigining values
tktID = Ticket_number.Value
reopened = Ticket_Reopened.Value
valid = Valid_Reopen.Value
Assi = Assiginee.Value
Reopenreason = Reopen_reason.Value
ReassignmentAG = Reassignment_AG.Value
RBSCollab = RBS_Collab.Value
SMEconf = CkBxSMEConfirmation.Value
Cloabag = Collabarated_AG.Value
smeName = SME_Name.Value
Updat = Update.Value
Closed = Issue_Closed.Value
iss = Issue.Value
ana = Analysis.Value
res = Resolution.Value
rdate = Resolve_date.Value
'Insert values into the tables
StrSQL = "INSERT INTO Updates (TicketID,Assiginee,ReassignmentAG,RBSCollab,CollabAG,SMEconfirmation,SMEName,Update,IssueClosed,Issue,Analysis,Resolution,ResolveDate,TicketReopened,ValidReopen,ReopenReason) VALUES ('" & tktID & "','" & Assi & "','" & ReassignmentAG & "','" & RBSCollab & "','" & Cloabag & "','" & SMEconf & "','" & smeName & "','" & Updat & "','" & Closed & "','" & iss & "','" & ana & "','" & res & "','" & rdate & "','" & reopened & "','" & valid & "','" & Reopenreason & "' ) "
DoCmd.RunSQL StrSQL
提前感谢您的帮助
答案 0 :(得分:2)
我也使用大量未绑定的控件和SQL语句来设置我的第一个Access数据库,以便移动数据。但这不是使用Access的方式。您应该使用绑定表单,其中控件根据绑定的表/列自动知道约束是什么。
这意味着您编写(和维护)的代码要少得多,未来的开发人员可以查看您的Access应用程序并知道发生了什么,因为它将在Access范例中完成,并且如果您在表中放置尽可能多的逻辑/关系尽可能然后如果有人出现并链接到您的ACCDB或打开后端,他们可以输入数据而不输入错误数据。
所以最终这是一个XY问题。摆脱所有这些代码(尤其是DoCmd.RunSQL
)并创建一个绑定表单。将验证逻辑放在表中的外键,验证规则和/或数据宏中。
答案 1 :(得分:0)
这个问题的简单解决方案可能是在使用inset查询之前检查元素的值,以及元素的值是否未定义而不是设置为null。 将此值传递给查询中的数据库。
答案 2 :(得分:0)
为每个字段执行此操作:
CREATE TRIGGER dbo.UpdateIncrementalID
ON dbo.MyTable
AFTER INSERT
AS
UPDATE x
SET incrementalId = ROW_NUMBER() OVER(PARTITION BY category ORDER BY myKey DESC)
FROM dbo.MyTable x
然后代替:
tktID = IIF(ISNULL(Ticket_number.Value),"Null","'" & Ticket_number.Value & "'")
这样做
VALUES ('" & tktID & "',...
或者,如果您不介意用空字符串替换空值:
VALUES (" & tktID & ",...
会这样做。
当然你应该至少确保没有单引号,所以更好:
tktID = Nz(Ticket_number.Value)
答案 3 :(得分:0)
为INSERT语句构建字符串后
strSql = "INSERT INTO tblmovimentiServizi ( msid, " _
& " msData, msIdDestinazione, msOraInizioServizio, msOraFineServizio, msAndataRitorno, " _
& " msOraVincoloEntrata, msOraVincoloUscita, msidAnagraficaAutomezzi, msServizioEseguito, msNoteCommenti) " _
& " VALUES ('" & rst!newid & "', #" & rst!msData + Me.g1 & "#, '" _
& rst!msIdDestinazione & "', #" _
& rst!msOraInizioServizio & "#, #" _
& rst!msOraFineServizio & "#, '" _
& rst!msAndataRitorno & "', #" _
& rst!msOraVincoloEntrata & "#, #" _
& rst!msOraVincoloUscita & "#, '" _
& rst!msidAnagraficaAutomezzi & "', " _
& eseguito & ", '" _
& rst!msNoteCommenti & "'" _
& ");"
我添加了
strSql = Replace(strSql, "''", "null")
strSql = Replace(strSql, "##", "null")
它有效。