By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,470 Members | 1,696 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,470 IT Pros & Developers. It's quick & easy.

Backwards code works Forwards? Why?

100+
P: 135
I quickly put this code together to test if it would disable some of my functions OnChange when my text box (purchase order number) is empty...and if it would enable those functions when I added data to my field. The funny thing is that if you read the code it actually runs in reverse! When my field is null it is suppose to disable...instead it enables...Why? So I put my code in to do the opposite of what I want and it works!?!?! I don't mind having my code in logical reverse as long as I get the results I'm looking for...but...I don't get it....???? ;-)


Forwards: (works in reverse and enables features when Null)
Expand|Select|Wrap|Line Numbers
  1. Private Sub PurchaseOrderNumber_Change()
  2. If IsNull(Me.PurchaseOrderNumber) Then
  3. Me.Check107.Enabled = False
  4. Me.Check111.Enabled = False
  5. Me.Text77.Enabled = False
  6. Me.Label119.Visible = True
  7. Else
  8. Me.Check107.Enabled = True
  9. Me.Check111.Enabled = True
  10. Me.Text77.Enabled = True
  11. Me.Label119.Visible = False
  12. End If
  13.  
  14. End Sub
Backwards: (disables features when null)
Expand|Select|Wrap|Line Numbers
  1. Private Sub PurchaseOrderNumber_Change()
  2. If IsNull(Me.PurchaseOrderNumber) Then
  3. Me.Check107.Enabled = True
  4. Me.Check111.Enabled = True
  5. Me.Text77.Enabled = True
  6. Me.Label119.Visible = False
  7. Else
  8. Me.Check107.Enabled = False
  9. Me.Check111.Enabled = False
  10. Me.Text77.Enabled = False
  11. Me.Label119.Visible = True
  12. End If
  13.  
  14. End Sub
May 17 '09 #1
Share this Question
Share on Google+
18 Replies


FishVal
Expert 2.5K+
P: 2,653
Hello.

I suggest you to perform a little experiment - add two controls on the form and in PurchaseOrderNumber_Change event handler assign PurchaseOrderNumber.Value property and PurchaseOrderNumber.Text property values to them.

Regards,
Fish.
May 17 '09 #2

100+
P: 135
@FishVal
Ooook that was interesting.....So....what is that showing me. A little bizarre, but I have no idea what these results tell me... =-)




"So, where did you learn Access?"

"Learn Access? Who? Me?"
May 17 '09 #3

FishVal
Expert 2.5K+
P: 2,653
@MyWaterloo
I guess it explains why your code behaves in a way you don't expect.

Kind regards,
Fish.
May 17 '09 #4

100+
P: 135
@FishVal
I don't understand how it explains what my code is doing. Why do the two controls behave the way they do? Why does the code work, but in a way that appears to be in reverse? I just don't see how the two controls reveal why the code behaves the way it does.






"I can design a fantastic looking form for you. With buttons and menus and everything."
"What do they do?"
"What?"
"The buttons and menus."
"I have no idea, but they sure look good."
May 17 '09 #5

100+
P: 135
OK. I see that (for whatever reason I don't know) this current process of mine will not work. I guess I'm at a dead end. I was hoping to disable certain features if one of my fields was not filled in i.e. null. =(
May 17 '09 #6

FishVal
Expert 2.5K+
P: 2,653
@MyWaterloo
I think it is quite straightforward.

PurchaseOrderNumber.Value property is a default property you implicitly invoke when checking condition for enabling/disabling.
This little experiement shows you what this property returns when PurchaseOrderNumber_Change event fires thus making clear why the code behaves the way it behaves. At the same time value returned by PurchaseOrderNumber.Text property is , I guess, a more relevant criteria for making decision whether the controls should be enabled or disabled.

Does it make more sense now?
May 17 '09 #7

100+
P: 135
Yes, it kind of makes sense to me. I guess what it really means is that I need to come up with some other way to disable my print buttons if my P.O. number field is null. Any ideas? (Thanks for the lesson.)
May 17 '09 #8

mshmyob
Expert 100+
P: 904
subscribing just to watch the fun.

cheers,
May 17 '09 #9

NeoPa
Expert Mod 15k+
P: 31,709
Did the values ever get posted?

Fish & the OP seem to be discussing something they can both see but I for one, don't see any values posted (Emperor's clothes! I was never one to try to hide my ignorance so I will always ask the questions).
May 18 '09 #10

mshmyob
Expert 100+
P: 904
I think what you were told was to use the .text property instead of the .value property.

You could also the the NZ function and then check the value of the variable to see if NULL and do the appropriate commands.

cheers,

@MyWaterloo
May 19 '09 #11

mshmyob
Expert 100+
P: 904
Just to reword Fishval's response....

There is a slight difference between the .Value and .Text property. It is subtle but an important difference.

The .Value property refers to the saved value of the textbox while the .text property refers to the current contents of the textbox. Therefore, until you lose focus you should use the .text property to determine the contents of the textbox.

Does this help explain why your code works in reverse?

cheers,
May 19 '09 #12

100+
P: 135
@mshmyob
Yes, that helped bring light to the subject. So when I lose focus of the textbox I have to use the .value property?
May 20 '09 #13

mshmyob
Expert 100+
P: 904
I am glad it helps you a bit but you do not need to use the .value property you can use the .text property all the time.

cheers,

@MyWaterloo
May 20 '09 #14

100+
P: 135
@mshmyob
Ok. I will give it a go.
May 20 '09 #15

100+
P: 135
OK. So I still don't understand this. =-( I can't figure out how to code this so if my Purchase Order field is Null my print features are disabled, and if it has a number my print features are enabled. ????? Help?????
May 21 '09 #16

mshmyob
Expert 100+
P: 904
Maybe you are getting confused with NULL and zero length strings?

Anyways try this in the AfterUpdate event

Expand|Select|Wrap|Line Numbers
  1. If Len(Me.Text0.Text) = 0 Then
  2.     'your code if no value is present
  3. Else
  4.     'your code if data is present
  5. End If
  6.  
You would also need to check your LostFocus event in case there is no data in the text control when first loaded and the user just tabs off and tries to print.

I would really put the code in the OnClick event of your button for printing.

cheers,

@MyWaterloo
May 21 '09 #17

FishVal
Expert 2.5K+
P: 2,653
  • If you want print features state to reflect [Purchase Order] textbox content immediately, then you should handle Change event which fires whenever user changes it. The code in the event handler should check whether [Purchase Order].Text property returns empty string ("") - note, Text property returns String type value, it couldn't be Null. If it is so, then code disables print features, otherwise - enables.
  • If you want print features state to reflect [Purchase Order] textbox content as soon as user confirmed input by moving focus to another control, then you should handle AfterUpdate event. The code in the event handler should check whether [Purchase Order].Value property returns Null. See the difference? Value property returns Variant type value, moreover it is generally not the same that Text property returns but more like a result of input interpretation in context of what the textbox control expects to get as input.
May 21 '09 #18

100+
P: 135
@mshmyob
Thanks. Yes, I was getting confused with Null... I actually have some code similar on my print button. I wanted this code to disable my print buttons and then display a label that is draped over the print buttons telling the user "P.O. number required to enable print features." I don't want to require a P.O. number on the form to enable saving of the record...just encourage it. Some P.O.'s don't have numbers as they are being filled out and then the user comes back later to add data. The number is from an outside source and is given when it si time to print/save. So the number must be present to print/save as a file. I just wnated it clear right from the start that a P.O. is required to do any kind of output. Thnaks, I'll try this and let you know how it goes.
May 21 '09 #19

Post your reply

Sign in to post your reply or Sign up for a free account.