PDA

View Full Version : Using a Field From a Subform in a Calculation



Charity
02-24-2006, 01:19 PM
I am trying to reference a textbox on my subform from a button on my main form. When I click my button, I want it to look at the hourly pay rate on my subform and tell me if it is >= or < $10 by returning yes/no in a textbox on my mainform. Here is my code, but I want to replace txtHourlyRate (which is an unbound textbox I added to my mainform just to see if I could get the rest of my code to work) with the field from my subform. Any suggestions on what I need to do?

Private Sub btnDavisBacon_Click()
' When the button is clicked it will look at the hourly rate and
' if it is less than $10/hr it will return a result of no,
' if it is greater than $10/hr it will return a result of yes.
Dim Hourly_Rate As Double
Dim Result As String
Me.txtHourlyRate.SetFocus
Hourly_Rate = CDbl(Me.txtHourlyRate.Text)
If Hourly_Rate >= 10 Then
Result = "Yes"
End If
If Hourly_Rate < 10 Then
Result = "No"
End If
Me.Meets_Davis_Bacon_Requriements_.SetFocus
Me.Meets_Davis_Bacon_Requriements_.Text = Result

End Sub

Thanks,
Charity

XLGibbs
02-24-2006, 04:59 PM
You don't need to set focus to something to read its value..

the basic syntax for what you want to do is something along these lines:


Me only refers to the main form, not the subforms contained therein, so you need to identify which subform and which object you want to point to..



If Val(Subformname.TextBoxName.Text) >= 10 Then Me.Meets_Davis_Bacon_Requirements_.Text = "Yes"

Else Me.Meets_Davis_Bacon_Requirements_.Text ="No"


It seems like you are using events and focus to make things happen. This is unecessary,and it would be better to use change events. An object does not have to have focus to refer to properties...

Set Focus is better used to control the movement of the user through the form or to force focus to a tab stop that may be out of position based on the events occuring...(for example, if a certain item causes other entries to be skipped, you would set focus from there to the appropriate place)

Charity
03-02-2006, 02:55 PM
I tried your code, but I get an error that says: object requried. What do I need to do to fix that?

XLGibbs
03-02-2006, 03:01 PM
Did you place the correct form names where the belonged?

Me only refers to the active form so if the button is not contained on the form with the text box you want to update, you must identify the form by name instead of Me. Also, my syntax had subformname instead of an actual name....

You would have to make sure that you are referring to the objects correctly...if the correct object (Form, text box etc are objects) is not being properly referenced, that is the error that would result.

swoozie
06-16-2006, 12:40 PM
I found that .text you will receive an error message that the propert could not be read unless the control has focus, so I use the .value instead or nothing