My code is acting goofy

NJudson

Who farted?
Local time
Today, 13:34
Joined
Feb 14, 2002
Messages
297
I'm using Access 2000 and I have code that is written

If Table1.Field(0) = "JoeBob (104)" then
Test = True
ElseIf InStr(Table1.Field(0), "104") then
Test = True
ElseIf Right(recTable1.Field(0), 5) = "(104)" then
Test = True
End If

Where the value for recTable1.Field(0) = JoeBob (104)

This check should test true for all 3 conditions but it doesn't. It's driving me crazy and I can't figure out why it doesn't work. Has anyone ever experienced this before. Thanks for any help.
 
How would you know whether or not it was testing true for all three statements. Using the ElseIf format, the code would quit as soon as it found the first true condition. You would need 3 separate if statements, if you wanted to test each statement.
 
I'm sorry you're right. I actually wrote them as three seperate conditional statements but got careless when I posted the question and wrote it as I did. My apologizes. I guess the bottom line is that stepping through each one of them it fails. Any thoughts? Thanks.
 
Give the exact syntax of the statement or statements which are not returning true.
 
See if you are getting caught by having the wrong values for your Option Compare statement.

The best way I know to prove to myself what is happening is to set a breakpoint at the beginning of each IF. Open the Locals window and find out what is in the field.

I would have written this slightly differently for debugging purposes.

Dim Boo1 as boolean, boo2 as boolean, boo3 as boolean.

...

Boo1 = ( Table1.Field(0) = "JoeBob (104)")
Boo2 = ( Instr(Table1.Field(0),"104") > 0 )
Boo3 = ( Right(Table1.Field(0),5) = "(104)" )
Test = Boo1 or Boo2 or Boo3

Then, I could put a breakpoint on the Test = line and see what the three parts returned.

By the way, that Instr might be part of your problem. Suppose that the right side of the field, for whatever reason, is blank-padded. Then the first and third tests would fail. And I believe your second test is malformed. Or at least, logically ambiguous at execution time.
 
I really appreciate you all looking at this.

I've done what you guys suggested and it still didn't work. As far as the syntax went the value for recTable1.Field(0) showed to be exactly what I had written so in effect it was showing:

JoeBob (104) = JoeBob (104)
Test = False

???? Not sure why. But anyway I totally scraped that section of code and re-written it with the If then statements in a different location within my code and it works perfectly now. I'm totally willing to accept 'user error' but in this case I really can't explain what happened. I even had one of our IT/Programming guru's at work look at it and couldn't figure out why it wasn't working before. Thanks you for your time in looking at this.

Oh by the way Doc Man I like your method of the booleans for testing. I will use that my future debugging.
 

Users who are viewing this thread

Back
Top Bottom