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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about coding

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I am using VBA for Access, and some code that I am using that works in VB 6, gives me an error about getting focus. The code that I am trying to run is

txtYield.Text = txtInterest.Text * txtPrice.Text
 
Not sure but it may be due to attempting to commit math on text fields.

Access has a function called Val that will, I believe, treat values in text fields as numbers. You might try Help for info on the Val function to see if that would help. [sig]<p>Larry De Laruelle<br><a href=mailto:larry1de@yahoo.com>larry1de@yahoo.com</a><br><a href= > </a><br> [/sig]
 
Hi Homer,

no VB and VBA are a litle different when it comes to text boxes in access (97)

Yield.Text = val(txtInterest.Text) * val(txtPrice.Text)
val() returns a numerical equivelent to the text (string) in the textbox.
in vb i would use

If IsNumeric(controlName) Then
aVarName = val(controlName)
end if

unlike VB the VBA (access 97) text property is a little different

on a form you have a textbox not to different here
but!
vb YourTextBox.Text = &quot;Something&quot;
vba YourTextBox = &quot;Something&quot;

vb if YourTextBox.Text = &quot;SomeThing&quot; then
vba if YourTextBox = &quot;Something&quot; Then

mmm just to make it difficult ;-)

HTH
Robert




[sig][/sig]
 
I had many problems with this when I first started writing code in VBA.

Larry and Robert are correct, in stating that you should use the Val() function, to return the Value of the string contained in the TextBox, however- this is the problem I was having, and that I assume you are having.

I believe Access is telling you that &quot;You are referencing a control that does not have focus&quot;, or something of that type.
What a pain! Set the focus of every control you want to modify? Ah, but no.

Try:
txtYield.Value = Val(txtInterest.Value) * Val(txtPrice.Value)
Instead of:
txtYield.Text = txtInterest.Text * txtPrice.Text

The Text property of a control, is only what's viewed, where-as the Value property, is te actual underlying data. The underlying data CAN be set, where-as you can't set the Viewable data directly.

Hope this helps.
-MoGryph
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top