Using a Function to return values to unbound text boxes on a form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have created a Function that works on the Click Event of a button on a
form. The function takes the Caption of the button_label and does a DLookup
to retrieve associated data.

Within the Function I end up with two string values (strAirportID and
strCityState). I need to return these two values to their respective unbound
textboxes (frmID and frmCityState) on the form from where the button was
clicked.

Normally I could use the Me.frmID = strAirportID (for example) to do this if
I were using a Private Sub, but, of course, I can't use the Me. in this case.
So, how can I do this, and forgive me. I'm still learning VB.

(By the way, the form is being designed to work in two ways. First, the
user can select from a combo box the AirportID. The database will then
lookup the CityState and put the values in an Intinerary textbox. Or,
secondly, (the reason for the Function) I have placed several command buttons
on the form for "common" Airport IDs, where the user can click and have the
appropriate fields populate automatically without a search.)
 
Hi JHK

If i understand correctly you are using this function in a module outside
the form's class module.
You can access all opened forms with forms("Form_Name").
For example you can wirte forms("Form_Name").frmId=strAirportID

Hope this will help
 
JHK said:
I have created a Function that works on the Click Event of a button on a
form. The function takes the Caption of the button_label and does a DLookup
to retrieve associated data.

Within the Function I end up with two string values (strAirportID and
strCityState). I need to return these two values to their respective unbound
textboxes (frmID and frmCityState) on the form from where the button was
clicked.

Normally I could use the Me.frmID = strAirportID (for example) to do this if
I were using a Private Sub, but, of course, I can't use the Me. in this case.
So, how can I do this, and forgive me. I'm still learning VB.

(By the way, the form is being designed to work in two ways. First, the
user can select from a combo box the AirportID. The database will then
lookup the CityState and put the values in an Intinerary textbox. Or,
secondly, (the reason for the Function) I have placed several command buttons
on the form for "common" Airport IDs, where the user can click and have the
appropriate fields populate automatically without a search.)


If the function can be called from two or more differnt
forms, your best bet is to add an argument to the function
for the form's name.
Public Function SetID(FormName As String)
With Forms(FromName)
.frmID = strAirportID
. . .
and call it this way:
SetID("YourForm")

If the function is always called from the same form, then
you can skip using the argument:
Public Function SetID()
With Forms!YourForm
.frmID = strAirportID
. . .
then call it like this way
SetID()

But, if the function is only used by this one form, you
should put the function's code in the form's module and
avoid this question altogether.
 
This is precisely what I was looking for. I did it a bit differently, but
your idea is certainly better.
 
Back
Top