user3781528
user3781528

Reputation: 639

How to correctly query if checkbox is checked in Access from Excel

Checkbox6 on a userform in Excel determines the selection criteria for a query. I need to use the property of checkbox6 True/False to query column [Archived] that is located in a table in Access. For example, if the Checkbox6 is checked on the userform the column [Archived] is queried for “True” values. The column [Archived] is data type Yes/No. I’ve tried both “True” and “False” and also -1 and 0 but it doesn’t seem to work. Please look at my code below and suggest how to make this work.

Thanks

Dim sar4 As String
sar4 = "False"

SQLwhere = "WHERE "


If Len(sar2 & vbNullString) <> 0 Then

If CheckBox7 = True Then
        SQLwhere = SQLwhere & "[MDL_Comments].[AA_change] = '" & sar2 & "' AND "
    Else

SQLwhere = SQLwhere & "[MDL_Comments].[AA_change] LIKE '" & "%" & sar2 & "%" & "' AND "
End If

End If

If CheckBox6 = True Then
SQLwhere = SQLwhere & "[MDL_Table1].[Archived] = '" & sar4 & "' AND "
End If

StrSql = "SELECT * FROM [MDL_Comments] "
 'Remove the last AND applicable
If SQLwhere = "WHERE " Then
    SQLwhere = ""
Else
    SQLwhere = Left(SQLwhere, Len(SQLwhere) - 5)
End If
StrSql = StrSql & SQLwhere

Upvotes: 0

Views: 1018

Answers (1)

jhTuppeny
jhTuppeny

Reputation: 970

I think the problem is your use of single quotes around your variable - ('" & sar4 & "'). This will set the variable to be a string regardless of the value you assign to it.

Upvotes: 1

Related Questions