We help IT Professionals succeed at work.

vba if statement returns false

gbnorton
gbnorton asked
on
I'm using the if statement below to modify the string strMySql.
' initialize
   strMySQL = "Update Part_Serial_Number Set "
   
    'build sql statement from text boxes that have data
    If Me.txtPCBoard_Lot_No1 <> "" Then
        strMySQL = strMySQL & "[PCBoard_Lot_No1] = " & str_txtPCBoard_Lot_No1
    End If

 DoCmd.RunSQL strMySql
 
When Me.txtPCBoard_Lot_No1 is empty, strMySql is being set to "False".  I would expect it to just leave strMySql as it was.  

I don't recall seeing this before.  Any ideas?

Thanks,
Brooks
Comment
Watch Question

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
case sensitive issue?
strMySQL  <> strMySsql....
please check/confirm, also by using OPTION EXPLICIT to ensure you declare all your variables.

Author

Commented:
In the code I posted I see I used both upper and lower case.  In the project code I verified the variable name is correct and consistant.  Option Explicit set.

Author

Commented:
This code is illogical to me but works:
    If IsNull(Me.txtPCBoard_Lot_No1) Then
        strDoNothing = "Doing Nothing"
    Else
        strMySQL = strMySQL & "[PCBoard_Lot_No1] = " & str_txtPCBoard_Lot_No1
    End If

    If IsNull(Me.txtPCBoard_Lot_No2) Then
        strDoNothing = "Doing Nothing"
    Else
        strMySQL = strMySQL & [PCBoard_Lot_No2] = " & str_txtPCBoard_Lot_No2"
    End If
MIS Liason
CERTIFIED EXPERT
Most Valuable Expert 2012
Commented:
1. Is what you posted originally the *entire* code...?
I don't see where you are declaring: strMySQL

2. To be sure, I always check for null as well as an empty string:
If Me.txtPCBoard_Lot_No1 <> "" Or not isnull(Me.txtPCBoard_Lot_No1) Then
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
do you have something a variable or string with the & directly attached, aka with no space?

"string"&  vs "string" &
or
variable& vs variable &
Hamed NasrRetired IT Professional
CERTIFIED EXPERT

Commented:
If Nz(Me.txtPCBoard_Lot_No1, "")  <> "" Then
        strMySQL = strMySQL & "[PCBoard_Lot_No1] = " & str_txtPCBoard_Lot_No1
End If
Walter RitzelSenior Software Engineer
CERTIFIED EXPERT
Commented:
The code you posted is very logical, and you should do what boag2000 mentioned... Test the variable for NULL and <> "" to make sure you caught all the situations where you dont want to issue your update.

Author

Commented:
I'll update you tomorrow.  Out today.  Thanks, Brooks

Author

Commented:
thank you,
Brooks

Explore More ContentExplore courses, solutions, and other research materials related to this topic.