Home > other >  Access not recognizing SQL boolean values
Access not recognizing SQL boolean values

Time:09-01

I have the following SQL statement for adding records in Microsoft Access. When I run the statement, every field gets added correctly except the boolean field, which should always be false. However, it always adds the record with the default boolean value (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 ' '
  • Replacing the 0 with the word false, False and No

I can't set false to be the default value because what I need is for it to be different from the default value

Table

Form

CodePudding user response:

Binding form to table and running SQL to create record into same table will result in two new records. Do one or the other.

Do not include autonumber field in the INSERT action. 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.

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

AFAIK, Access cannot execute multi-action SQL statements but SQL injection is still possible. 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?

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. DAO.Recordset AddNew

  • Related