Does anyone know how to create a function that can called upon multiple times? I dont want to write the following piece of code 20 times.
private sub Lost focus
Dim txtTemp as string
txtTemp = strconv(me!textbox,3)
me!textbox = txtTemp
Instead of using code in your form events, use the function option instead and pass it Screen.CurrentControl. Sorry that I don't have time to give you a tested example but the following should get you started.
Public Function LostFocus(ctl As Control)
LostFocus = strconv(ctl ,3)
End Function
I would suggest either the BeforeUpdate or AfterUpdate event for your code. You don't need to do anything unless the user changed something.
If you need to do any data validation then use BeforeUpdate because it passes a Cancel parm which if set to True will not allow the user off of the control in question regardless of what they clicked or what key they pressed.
Otherwise, use the AfterUpdate event.
For our example, assume you are using the AfterUpdate event. In the AfterUpdate event of your textboxes, put the function call something like this (I don't remember whether or not you need the = sign).
=LostFocus(Screen.CurrentControl)
You may have to tweak the syntax a little bit since this is off the top of my (admittedly flat) head.
Background: I trying to call the following function the modules section of Access to change the text in a text box.
Public Function ConvertText(ctl As TextBox)
Dim ctl As String
ctl = strconv(Me!Screen, vbProperCase, 3)
'ctl = strconv(Me!Screen.currentcontrol, 3)
Me!Screen.currentcontol = ctl
I would suggest the After Update event instead because you don't care unless something has changed. You cannot use the Before Update event because it will not allow you to make changes to the control. Try the following for your code:
Public Function ConvertText(ctl As TextBox) As String
If Len(ctl) = 0 Then 'Just return the parm as is
ConvertText = ctl
Else
ConvertText = strconv(ctl, vbProperCase, 3)
End If
End Function
To pass this as a function in the After Update event set it up as follows:
=ConvertText(screen.activecontrol)
If might be currentcontrol, I don't remember, but hopefully this will get you started.
What do you mean when you say nothing happened? Did the function get called and not return what you expected, did it not fire, etc? If you can give me some more information, maybe we can solve the problem.
The function fired [I did not get any error]. I changed the value in the field and then went to the next record and the text in the field did not change.
Sorry, you have to modify the control value within the function body itself as below:
Public Function ConvertText(ctl As TextBox) As String
If Len(ctl) > 0 Then
ctl = strconv(ctl, vbProperCase)
ConvertText = "OK"
End If
End Function
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.