简体   繁体   English

访问无法识别 SQL boolean 值

[英]Access not recognizing SQL boolean values

I have the following SQL statement for adding records in Microsoft Access.我有以下 SQL 语句用于在 Microsoft Access 中添加记录。 When I run the statement, every field gets added correctly except the boolean field, which should always be false.当我运行该语句时,除了 boolean 字段外,每个字段都被正确添加,该字段应始终为 false。 However, it always adds the record with the default boolean value (true).但是,它总是添加具有默认 boolean 值 (true) 的记录。

Dim strSQL As String
strSQL = "INSERT INTO Part VALUES ('" & Me.IdPartPrimary.Value & "', '" & Me.NamePartPrimary.Value & "', '" & Me.BrandPartPrimary.Value & "', '" & Me.ModelPartPrimary.Value & "', '" & Me.FunctionPartPrimary.Value & "', 0, '" & Me.FatherPartPrimary.Value & "', '" & Me.ProviderPartPrimary.Value & "', '" & Me.AmountPartPrimary.Value & "');"
DoCmd.RunSQL strSQL

Things I've tried:我尝试过的事情:

  • Putting the 0 between ' '将 0 放在 ' ' 之间
  • Replacing the 0 with the word false, False and No用单词 false、False 和 No 替换 0

I can't set false to be the default value because what I need is for it to be different from the default value我不能将 false 设置为默认值,因为我需要的是它与默认值不同

桌子

形式

Binding form to table and running SQL to create record into same table will result in two new records.将表单绑定到表并运行 SQL 以在同一个表中创建记录将产生两条新记录。 Do one or the other.做一个或另一个。

Do not include autonumber field in the INSERT action.不要在 INSERT 操作中包含自动编号字段。 Will have to explicitly list the fields for update.必须明确列出要更新的字段。

If you want new record to always have 0 value in the Yes/No field, then set that as DefaultValue in table design, otherwise use a constant in the SQL concatenation to specify.如果您希望新记录在 Yes/No 字段中始终具有 0 值,则在表设计中将其设置为 DefaultValue,否则在 SQL 连接中使用常量来指定。

strSQL = "INSERT INTO Part(Part_Name, ..., Primary_Part, ...) VALUES(" &... & ", 0," &... & ")"

AFAIK, Access cannot execute multi-action SQL statements but SQL injection is still possible. AFAIK,Access 无法执行多动作 SQL 语句,但 SQL 注入仍然是可能的。 If you want to explore use of Parameters in VBA, review How do I use parameters in VBA in the different contexts in Microsoft Access?如果您想探索 VBA 中参数的使用,请查看如何在 Microsoft Access 的不同上下文中使用 VBA 中的参数?

Another alternative to avoid SQL injection is to open a recordset, use its AddNew method to create a new record, and set value of each field.另一种避免 SQL 注入的方法是打开一个记录集,使用它的 AddNew 方法创建一个新记录,并设置每个字段的值。 DAO.Recordset AddNew DAO.Recordset AddNew

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM