Run-time Error assigning text to/from textbox - error 2185
I am getting the following error when I try to assign the value of a textbox to an integer in my Spin Button's SpinUp event handler. This error also occurs if I try to assign the textbox.text property to a string variable......
Run-time error 2185:
You can't reference a propert or method for a control unless the control has focus.
Perhaps this is a stupid question, but I just do NOT understand why this is happening.
H E L P ! ! !
CODE:
Sub SpinBtnDonationAmt_SpinUp()
Dim iValue As Integer
Dim iIncrementer As Integer
iIncrementer = 5
'Get the value out of the text box and store it in a variable
iValue = Val(txtDonationAmt.Text)
'Increment the variable
iValue = iValue + iIncrementer
'Put the value back into the text box
txtDonationAmt.Text = str$(iValue)
End Sub
The error occurs on the iValue assignment line
(iValue = Val(txtDonationAmt.Text)
NOTE:
The value of txtDonationAmt is set to 0 in Design mode, but when I run the Form, it displays #Name? which I am figuring is part of the problem because the value is no longer a number.
1) Why would the textbox value change to "#Name?" at runtime?
2) Do I need to set some property of the textbox to allow reading/writing from vb code?
I appreciate any help.
- Bill
[Edited by ba1959nh on 17-05-2006 at 02:04 AM GMT]
Re: Run-time Error assigning text to/from textbox - error 2185
OK....I hope this isn't too weird, but I am posting a solution for my own question.
It appears that assignments cannot be made using the .Text property of some controls - like a Textbox. Instead....I used just the control name when assigning from the control:
sAmt = txtboxAmount
NOT
sAmt = txtbox.Text
AND.....
I used the .Value property when assigning a variable TO the textbox
txtboxAmount.Value = sAmt
NOT
txtboxAmt.Text = sAmt
I hope this is right !!!
VB sure is finicky and not very intuitive and the help is like a maze
If my assumptions and/or findings are in error please post
Thanks,
Bill
17-05-2006 at 04:12 AM
|
yronium Level: Moderator Registered: 14-04-2002 Posts: 907
Re: Run-time Error assigning text to/from textbox - error 2185
Hello. Just let me drop a comment here.
This behaviour - not exposing a control's properties when the control has not the focus - is only present in MSAccess VBA (please specify the db and the environment you're working in, next time), in VB you can always refer to any existing object's property.
I find it nasty, and it's one of the reason for I quitted programming MSAccess forms.
Anyway, plus than the variable assignment, you can bypass it by setting the focus on the textbox control right before reading its Text property, by the txtbox.SetFocus method.
Hope it helps
____________________________
Real Programmer can count up to 1024 on his fingers
Re: Run-time Error assigning text to/from textbox - error 2185
yronium,
Yes, I am working with VB in MS Access and I apologize for not posting that information. I will make sure to do that next time.
I agree with you concerning the quirkiness of VB in MS-Access. I usually write C# Windows applications and never ran into all the peculiar behavior which is seemingly undocumented (or carefully hidden).
Thanks.
Bill
[Edited by ba1959nh on 17-05-2006 at 05:42 PM GMT]