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

Calling a Function that can be used multiple text boxes

Status
Not open for further replies.

acct98

IS-IT--Management
Aug 15, 2002
194
US
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.

Good Luck!
 
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

'Dim FirstName As String
'FirstName = strconv(Me!FirstName, 3)
'Me!FirstName = FirstName

Now I am trying to call this function from the Lost Focus section of the event tab for the text box:

Private Sub firstname_LostFocus()

Call ConvertText()

End Function

Needless to say I'm getting a variable error. Can someone tell me what I'm doing wrong?
 
You function is expecting something, the name of the control. Here is a very simple example from my own code

Grant

'txt3 is a textbox
Call webGetSelection(txt3)


Function webGetSelection(ctrlName)
ctrlName.Value = SelectedText
End Function
 
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.

Good Luck!



 
SBendBuckeye,

I tried you suggestion but nothing happened. Any other ideas.
 
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.

Good Luck!
 
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

Didn't mean to mislead you. Good Luck!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top