Set Value Without Focus

doco

Power User
Local time
Today, 00:26
Joined
Feb 14, 2007
Messages
482
Sure wish Microsucks would get their head out of their collective a** and fix that bug! :mad:

There could be absolutely no reasonable explanation why values can not be set to controls at run time whether they had the focus or not! Purely and simply and absurdly rediculous!
 
Sure wish Microsucks would get their head out of their collective a** and fix that bug! :mad:

There could be absolutely no reasonable explanation why values can not be set to controls at run time whether they had the focus or not! Purely and simply and absurdly rediculous!

Umm, you can set the values without having the focus. The problem is that you aren't using the correct syntax.

If you use

Me.YourTextbox.Text = xxxx

then it has to have focus


If you use

Me.YourTextbox = xxxx

then you don't
 
Nope. Doesn't work with .text or .value.

Point is shouldn't be a problem either way. It isn't in any other MS product Excel, VB, etc.
 
TextBox control

Is there an explanation why this is unique to Access?
 
What is the exact code you are using and which event are you doing it on, because you should be able to set it without the focus. Can you post a sample where it isn't working for you so we can take a look?
 
I found an old article you posted and used

Me.txtDefaultYear = fnDefaultYear()
which works. ( fnDefaultYear() is a UDF querying a parameter table, etc )

Instead of
Me.txtDefaultYear.Text = fnDefaultYear() OR
Me.txtDefaultYear.Value = fnDefaultYear()

What really puzzles me know is *.Value is the default according to your post, so, the one I am using should not work either?
 
if it isnt working you must be getting a run time error

if so, which one is it?
 
If

Me.txtDefaultYear = fnDefaultYear()

works and

Me.txtDefaultYear.Value = fnDefaultYear()

doesn't work, and they've both been tried in the same event, then your form has some type of corruption.

As for

...Point is shouldn't be a problem either way. It isn't in any other MS product Excel, VB, etc.

The point is that Access isn't Excel, VB, etc. it's Access! Every language has its own rules, and if you want to program in it, you have to take the time to learn the rules, not sit around carping because you have to learn the rules!
 
not sit around carping because you have to learn the rules!

Actually, I was standing at the time.

Indeed and you are quite correct and yet it is no less frustrating to have a rule that makes no apparent sense.

I do appologize for the rant.
 
Actually it does make sense. .Text and .Value are used in totally different ways. The text typed into a textbox doesn't represent the value of the textbox until the cursor leaves the box and it is assigned to the textbox, and that’s as it should be. If you entered the word hemisemidemiquaver into a textbox, and each time a character was entered the Value changed, Access would have to update the field 18 times!

There are times when a programmer needs to check to see whether the current/new Value is the same as the OldValue for the textbox. Say this field was critical, and if it were changed you needed to confirm this change with the user.

Code:
If CriticalField.OldValue  < > CriticalField.Value Then
 Resp =  Msgbox ("Are you sure you want to change the CriticalField Value?", vbYesNo)
    If Resp = vbNo Then
      Me.CriticalField.Value = Me.CriticalField.OldValue 'Revert to original Value
    Else
      'Do nothing, accept the changed Value   
    End If 
End If


Now say this field originally had as its Value the word paradiddle. Using the above code and comparing paradiddle with hemisemidemiquaver would cause the code to execute and the warning message would appear. But if Access updated the Value every time a new character was entered, the OldValue and the current/new Value would both be hemisemidemiquaver, and the code wouldn’t execute!

So what do you use .Text for? You use it to check the text that been entered before it’s assigned as the textbox’s Value! Say you have a textbox for Comments, and since the field’s datatype is Text, it’s limited to 255 characters. As the user is entering data, you want to let them know how many characters they have left that can be entered. You’ve seen this before, on websites, for example, where you’re entering comments in response to a query. So you place a textbox named CharactersLeftTextbox beside your Comments box and use this code:

Code:
Private Sub YourTextbox_Change()
  Me.CharactersLeftTextbox = 255 - Len(Me.YourTextbox.Text)
End Sub
As each character is entered into the Comments box, the length of the text is subtracted from 255 and the results (the characters left that may be entered) is posted in the CharactersLeftTextbox. Obviously, YourTextbox has to have focus for this to work.

Or you have an unbound textbox for entering search criteria and you want to limit the search string to 12 characters. In a textbox bound to a field, you can set the Field Size to 12 in it’s table definition, and Access will stop allowing characters to be entered after 12, but that’s not possible in an unbound textbox, because there is no underlying field in a table! So you’d use code similar to the above to keep track of how many characters have been entered, then throw up a messagebox. Things like that!

Have a great week!

Linq
 

Users who are viewing this thread

Back
Top Bottom