Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ignoring if and elseif statements????

Status
Not open for further replies.

Ali29J

Technical User
May 13, 2004
203
GB
Hi All

I am slightly perplexed, i m using the below code to do a quick check that data is entered as it should, but it seems to bypass all the if and elseif statements and carries out the else statement regardless...

can anyone offer any thoughts...

Private Sub Command138_Click()
On Error GoTo Err_Command138_Click

Dim stDocName As String
Dim stLinkCriteria As String

If IsNull(Me.Combo102.Value) Then
Me.Label140.Visible = True
Me.Combo102.SetFocus
ElseIf IsNull(Me.DeliveryDetails.Value) Then
Me.Label142.Visible = True
Me.DeliveryDetails.SetFocus
ElseIf IsNull(Me.DeliveryContact.Value) Then
Me.Label143.Visible = True
Me.DeliveryContact.SetFocus
Else
stDocName = "frmSampleStep2"
stLinkCriteria = "[SRNumber]=" & Me![SRNumber]
DoCmd.OpenForm stDocName, , , stLinkCriteria
Exit Sub
End If

Exit_Command138_Click:
Exit Sub

Err_Command138_Click:
MsgBox Err.Description
Resume Exit_Command138_Click

End Sub


using MS Access 2003 but saved in format for 2000/XP

Ta

Ali
 
First lets try to see if there is anything in Combo102. Assuming this is a combo box then add the following line in your code, put a break in the code on this new line and see what the results are in the immediate window.

Right after your last dim statement add
debug.print combo102.text
debug.print len(combo102.text)

Post the results back here.
Thanks


Andy Baldwin

"Testing is the most overlooked programming language on the books!
 
BTW my bet is that the last item you are checking has data and thus the Else is executing.

I ususally stick to one set of if thens for each field. Creating a chain can confuse the logic and wind up executing the last esle as I think you code is.

Thanks


Andy Baldwin

"Testing is the most overlooked programming language on the books!
 
Hi

I have tried leaving just one if statement and it does nt carry that out either...

Tried to use your statement as described and it says the following:

"You cant reference a property or method for a control unless control has focus" so i set focus to combo first and it just ignores it and opens the form as previously....

help....
 
Sorry, Try to replace the
debug.print combo102.value
debug.print len(combo102.value)

Make sure you put a break on the first line here and have your immediate window open so you can see the results as you step through the code.


Andy Baldwin

"Testing is the most overlooked programming language on the books!
 
sorry i m a little confused i ve never carried this process ot before,

1. how do i put a break in the code...
2. what are you referring to on the immediate window?

sorry but i am still learning...

thanks

Ali
 
Get to the design of the code window.

Click inn the margin to the left of the code. This should put a maroon colord dot there. This is a break point. Code execution will stop at this point.

From the view window choose immediate window. This will bring up another window.

When the code is executed and stops at the break point you can use F8 to walk through each line of code. The output from the two debug.print lines will show up in the immediate window.

Post the results here.


Andy Baldwin

"Testing is the most overlooked programming language on the books!
 
ok so i got no output in the imeediate window just as below, a single zero after the first isnull, it then stepped through the rest with no output....

0



 
You may need to refresh/requery the form or controls to ensure that any changes are committed to the underlying data source. Often what you see on the screen is not what is still stored as the value of the control or associated field (if bound).

Also the isNull check is very limited. You may want something like this

If trim(Me.DeliveryDetails.Value & " ") = ""

Check for:
null
empty string
spaces


me.refresh
If trim(Me.Combo102.Value & " " ) = "" Then
Me.Label140.Visible = True
Me.Combo102.SetFocus
elseif trim(Me.DeliveryDetails.Value & " " ) = "" then
Me.Label142.Visible = True
Me.DeliveryDetails.SetFocus
ElseIf trim(Me.DeliveryContact.Value & " " ) = "" Then
Me.Label143.Visible = True
Me.DeliveryContact.SetFocus
Else
stDocName = "frmSampleStep2"
stLinkCriteria = "[SRNumber]=" & Me![SRNumber]
DoCmd.OpenForm stDocName, , , stLinkCriteria
'Exit Sub (remove this is unnecessary)
End If

Now if you want to check for 0 values as well you need to first ensure that it is not null and do individual checks like Andy said:

If trim(Me.Combo102.Value & " " ) = "" Then
Me.Label140.Visible = True
Me.Combo102.SetFocus
exit sub
elseif (Me.Combo102.Value) = 0 then
Me.Label142.Visible = True
Me.DeliveryDetails.SetFocus
exit sub
end if

do the same structure for each control. The exit subs will ensure that the code does not fall out to the open form
 
One more thing. The only way your else statement fires is if none of the if and elseif statements are true. I do not believe that this is a possibility:

the last item you are checking has data and thus the Else is executing.

When this program executes, the computer will check all conditions in order until one of them matches its concept of truth. As soon as this occurs, the program will execute the statement immediately following the condition and continue on, without checking any other condition for truth. For this reason, when you are trying to optimize a program, it is a good idea to sort your If..Then..Else conditions in order of descending likelihood. This will ensure that in the most common scenarios, the computer has to do less work, as it will most likely only have to check one or two branches before it finds the statement which it should execute.
 
Hi Majp / Abaldwin

thankyou for all your help finally managed to get it working using the trim command, not sure what the problem with isnull, have used it on many other occasions in the same BD and worked fine

but thankyou both for adding to my knowledge and helping ot think about these things a little different

Ali
 
Your welcome.

Andy Baldwin

"Testing is the most overlooked programming language on the books!
 
There is most likely nothing wrong with the trim although it is possible that vb functions can not work if the DB has corruption. The thing about is null is that it only checks for null. The other code is more robust checking for
" " or "" which are spaces and null strings which are not null. The bottom is from the immediate window. Here is three cases where X has no real value.

Code:
x = ""
?x

?isnull(X)
False

x =" "
?x
 
?isnull(X)
False

x = empty
?x

?isnull(X)
False
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top