OnChange

pbuss2

New member
Local time
Today, 14:14
Joined
Jul 29, 2014
Messages
4
I have a form with an unbound textbox called EMPLOYEENO1 through EMPLOYEENO5 and FNAME1 through FNAME5. I would like to make it so that as the data inside of EMPLOYEENO changes the FNAME updates.
FNAME would update from a query called EMPLOYEENO that contains EMPLOYEENO and NAME.

I have gotten it so On Lost Focus updates the FNAME by using the following code.
Private Sub EMPLOYEENO2_LostFocus()
FNAME2 = DLookup("NAME", "EMPLOYEENO", "EMPLOYEENO=" & "EMPLOYEENO2")
End Sub

Is there a way to use On Change to update the information in real time?
 
Welcome to the Forum! :)

Normally, you would effect this kind of update in the After_Update event of the Control. However, the bigger question is why your Controls are labeled 1 thru 5. This suggests the Table may not be properly normalized. Perhaps you could explain your Table set and what it is you are trying to accomplish?
 
Thanks for the welcoming and the quick response!

After_Update worked as well.

What I have done is taken a form and created multiple entry spots for employee number and hours in order to enter multiple under the same job into a table that holds the employees hours. I used the following code to do it for 1-5.

Dim EMP1 As String
Dim TASK1 As String
Dim H1 As Integer


EMP1 = Forms!newentry!EMPLOYEENO1
TASK1 = Forms!newentry!TASKNO1
H1 = Forms!newentry!HRS1

CurrentDb.Execute " INSERT INTO DCreworked " _
& "([EMPLOYEENO],[TASKNO], [HRS]) VALUES " _
& "(" & EMP1 & ", " & TASK1 & ", " & H1 & ");"

I would like to make it so that when typing in the Employee Number (which ranges from 101-800) the information in FNAME1 would changerather than after hitting tab. This way the individual entering it could see who it is rather than tabbing then having to go back if it was the wrong person. I thought maybe On_Change would be able to do this, but have not been able to find many resources dealing with it.
 
Last edited:
The change event of a textbox fires for each keystroke. Given that Update has not yet occurred, you need to read the Text property of the control, not the Value property as you normally would after update is completed.

Sample code might look like . . .

Code:
private sub Text0_Change()
   debug.print "handling the change event of Text0"
[COLOR="Green"]   'shows the difference between the value and text properties for each keystroke
[/COLOR]   debug.print Me.Text0.Value
   debug.print Me.Text0.Text
end sub
A common pattern might be that you check the value of the Text property of the control during Change, to see if you can use it for some purpose, and if so, call the subroutine that would do the work . . .
Code:
private sub Text0_Change()
   if IsNumeric(Me.Text0.Text) then
[COLOR="Green"]      'do something with the validated Me.Text0.Text[/COLOR]
   end if
end sub
The Text property never returns Null, which is handy.
 
Thanks for your help! The problem was me using .txt rather than .text
 

Users who are viewing this thread

Back
Top Bottom