Passing Field Names to a Public Function (1 Viewer)

Randomblink

The Irreverent Reverend
Local time
Yesterday, 23:32
Joined
Jul 23, 2001
Messages
279
I have a function stored in a module.
I want to be able to run this function from several forms...
Each form will have a listbox that is named lst_SearchWindow.
Each form will have two labels named: btn_Delete and btn_CancelDelete.

What I want to do is affect these objects.
But everytime I try to reference these forms from a Function, I have to verify which form in the Public Function which does me no good...
I want to write code like this:

Public Function ButtonCheck(Slctn As String, Trgt as Control)
Select Case Slctn
Case "Delete"
Trgt.SetFocus
Call BttnAct("Show", "Hide")
Case "Search"
Call BttnAct("Hide", "Show")
Case "Blank"
Trgt.SetFocus
Call BttnAct("Hide", "Hide")
End Select
End Function

Public Function BttnAct(Dlt As String, Srch As String)
Select Case Dlt
Case "Hide"
BttnVs ("False")
Case "Show"
BttnVs ("True")
End Select

Select Case Srch
Case "Hide"
Call SrchWndwVs("False", lst_SearchWindow.Width)
Case "Show"
Call SrchWndwVs("True", lst_SearchWindow.Width)
End Select
End Function

Public Function BttnVs(DltLgc As String)
btn_Delete.Visible = DltLgc
btn_CancelDelete.Visible = DltLgc
End Function

Public Function SrchWndwVs(SrchLgc As String, Wdth As Integer)
Wdth = Wdth * 1440
lst_SearchWindow.Visible = SrchLgc

Select Case SrchLgc
Case "True"
lst_SearchWindow.Width = Wdth
Case "False"
lst_SearchWindow.Width = 0
End Select
End Function

Then, from ANY form I can Call the ButtonCheck Function, and it will activate the buttons and search window correctly.

But it doesn't work...

lst_SearchWindow.Tag = 3.5

I have it set up so that as a user presses the Search button, it closes the Delete buttons and Vice Versa. I want to use this code to use a simple search and delete button set up on all my forms...

What am I doing wrong...?

Argh!

When I click on the button to search or delete...

The Search Window button has this code in it:

Call ButtonCheck("Search", "Me!Dept_ID")

I get this message...

The expression On Click you entered as the event property setting produced the following error: Type mismatch
* The expression may not result in the name of a macro, the name of a user-defined function, or [Event Procedure].
* There may have been an error evaluating the function, event, or macro.

What am I doing wrong...?

Help!
 

DALeffler

Registered Perpetrator
Local time
Yesterday, 22:32
Joined
Dec 5, 2000
Messages
263
did you try getting rid of the quotes around "Me!Dept_ID"?

Call ButtonCheck("Search", Me!Dept_ID)

Doug.
 

Randomblink

The Irreverent Reverend
Local time
Yesterday, 23:32
Joined
Jul 23, 2001
Messages
279
Quotes

If I get rid of the quotes it isn't referencing a field...It references the variables assigned to the field...

For instance...
If the current entry in Dept_ID is 10

Then trying to set Focus to Dept_ID would be setting focus to "10" not to a field...

But the problem is coming from Variables...
Since I am referencing Fields from a form that has it's own module...and I am using Functions from another Module...
I am getting the errors...
 

DALeffler

Registered Perpetrator
Local time
Yesterday, 22:32
Joined
Dec 5, 2000
Messages
263
Real simple code I just tried:

Label36 is on my subform of my main form.

Private Sub Label36_Click()

fncTesting Me!Text49

End Sub

and in a module called Functions:

Function fncTesting(cntr As Control)

Debug.Print cntr.Name

End Function

Debug prints:

Text49

Maybe lose the call statement and ():

ButtonCheck "Search", Me!Dept_ID

Doug.

(edited 2nd sentence - added "sub form of my main form")
 
Last edited:

Randomblink

The Irreverent Reverend
Local time
Yesterday, 23:32
Joined
Jul 23, 2001
Messages
279
Improper Use of Null

I got it...

Next question...

How do I pass variables to a Function?

Explanation:

How do I send a field name to a variable?

FieldName: txt_RandomTextBox

***************FORM MODULE*****************
Private Sub
TextMagic(txt_RandomTextBox, True)
End Sub
********************************************

***************DIFFERENT MODULE*************
Function TextMagic(GenericVariable As Control, Setting As String)
GenericVariable.Visible = Setting
End Function
********************************************

If I do this I get errors...can someone help me?
 

DALeffler

Registered Perpetrator
Local time
Yesterday, 22:32
Joined
Dec 5, 2000
Messages
263
I think I can help.

You can't call a function that passes two arguments that the function isn't expecting. IOW, if you call a function and try to pass a control argument and a string argument, the called function has to be coded before hand to receive those specific type arguments.

So when your code says this:

***************FORM MODULE*****************
Private Sub
TextMagic(txt_RandomTextBox, True)
End Sub
********************************************

You're calling the TextMagic function and passing TextMagic two arguments: txt_RandomTextBox, and True. The keyword True is a boolean argument. So your function should be set up to recieve a boolean argument as the second argument.

Instead of this:

***************DIFFERENT MODULE*************
Function TextMagic(GenericVariable As Control, Setting As String)
GenericVariable.Visible = Setting
End Function
********************************************

Try this:

Function TextMagic(GenericVariable As Control, Setting As Boolean)
GenericVariable.Visible = Setting
End Function

See how that goes? If not, let's try again!

Doug.
 

Randomblink

The Irreverent Reverend
Local time
Yesterday, 23:32
Joined
Jul 23, 2001
Messages
279
!?!

Wait...

Are you saying that the first part is correct...?

Function TextMagic(GenericVariable As Control, Setting As String)

The GenericVariable As Control

????

I keep getting undefined this or that whenever I try to do that...!

If I try to send it...

Call TextMagic(Me!txt_Field, True)

It gives me errors!

What am I doing wrong...?
 

DALeffler

Registered Perpetrator
Local time
Yesterday, 22:32
Joined
Dec 5, 2000
Messages
263
I just tried this:

Private Sub Label36_Click()

fncTesting True, Me!Text49

End Sub

And the Function module code reads:

Function fncTesting(strin As String, cntr As Control)
Debug.Print strin
Debug.Print cntr.Name

End Function

And debug prints:

True
Text49

So your problem is not with what I just said it was - I apologize - thinking before I tested...

Do this one step at a time.

If you're trying to pass a control to a function, make sure that the controls' properties and methods are available in the function to insure you're indeed passing the control and not just a property of the control.

If your function has recieved the control as a control, then the function should be able to use every property of the control in the function. Like this (tested):

Function fncTesting(strin As String, cntr As Control)
Debug.Print strin
Debug.Print cntr.Name
cntr.Visible = False

End Function

When I click on Label36 (in the subform), Text49 (in the same subform) is no longer visible. It disappears. Even when I have another textbox called Text49 on the Main Form...

Call your function using one argument at a time. Manipulate that argument in your function enough to make sure the function's getting what it needs to work. Then add functionality to the function by passing other arguments.

The only reason I say his is because you're getting "Type Mismatch " errors, right?

Where is that error occuring?

Still with you,

Doug.
 

Randomblink

The Irreverent Reverend
Local time
Yesterday, 23:32
Joined
Jul 23, 2001
Messages
279
There we go...

Ok...Next question...

You figured it out for me...

I was creating functions like:
Public Function Magic(ABC as Control)
ABC.Visible = False
End Function

Then I would run the function like this:
Private Sub buttonA_OnClick()
Magic("txt_BoDiddly")
End Sub

And there was my problem...
Now then...
I have this figured out...BUT

What if this is my setup...

I will go back to my earlier code and put together a new question.

******************OPEN MODULE********************
Public Function ButtonCheck(Slctn As String, Trgt as Control)
Select Case Slctn
Case "Delete"
Trgt.SetFocus
Call BttnAct("Show", "Hide")
Case "Search"
Call BttnAct("Hide", "Show")
Case "Blank"
Trgt.SetFocus
Call BttnAct("Hide", "Hide")
End Select
End Function

Public Function BttnAct(Dlt As String, Srch As String)
Select Case Dlt
Case "Hide"
BttnVs ("False")
Case "Show"
BttnVs ("True")
End Select

Select Case Srch
Case "Hide"
Call SrchWndwVs("False", lst_SearchWindow.Width)
Case "Show"
Call SrchWndwVs("True", lst_SearchWindow.Width)
End Select
End Function

Public Function BttnVs(DltLgc As String)
btn_Delete.Visible = DltLgc
btn_CancelDelete.Visible = DltLgc
End Function

Public Function SrchWndwVs(SrchLgc As String, Wdth As Integer)
Wdth = Wdth * 1440
lst_SearchWindow.Visible = SrchLgc

Select Case SrchLgc
Case "True"
lst_SearchWindow.Width = Wdth
Case "False"
lst_SearchWindow.Width = 0
End Select
End Function

******************CLOSE MODULE********************

Now then, in ALL of my user forms...

I create two buttons and one listbox

The buttons have the names:
btn_Delete
btn_CancelDelete
The listbox has the name:
lst_SearchWindow

I then create two more buttons...
btn_InitiateDelete
btn_Search

The code I run for them both is:

Private Sub btn_InitiateDelete_OnClick()
Call ButtonCheck("Delete", <see note #1>)
End Sub

Private Sub btn_Search_OnClick()
Call ButtonCheck ("Search", <see note #1>)
End Sub

Private Sub <insert any other button/object here>_OnClick()
Call ButtonCheck ("Blank", <see note #1>)
End Sub

The idea is:

When you open a form.
You have two important buttons.
Delete and Search
If you click on Delete, it checks, hides any search window, and makes visible two new buttons.
The two new buttons are:
Delete or No!Cancel Delete
If you click on No! Cancel Delete
Then the following code is run:
Call ButtonCheck ("Blank", <see note #1>)

And if you click on the Search button,
It checks the delete buttons, hides them, and makes the search window visible...

BUT...
I can't seem to GRAB the fields from the followup functions...

For instance...

Function SrchWndwVs(SrchLgc As String, Wdth As Integer)

I want it to recognize the lst_SearchWindow and hide it or show it as needed...
But, I get error messages...

Can you help? Did I explain well enough...?
 

DALeffler

Registered Perpetrator
Local time
Yesterday, 22:32
Joined
Dec 5, 2000
Messages
263
I think I see what you're after.

What I think you need to do is pass to the ButtonCheck function all the controls that are to be manipulated in the other functions called within ButtonCheck. Then the code needs to pass those controls onto the other functions so the other functions can use them.

So you're code that calls ButtonCheck would look something like this:

Private Sub btn_InitiateDelete_OnClick()
'pass condition string and all controls
Call ButtonCheck("Delete", lst_SearchWindow, btn_Delete,btn_CancelDelete)
End Sub

And the ButtonCheck code would look something like:

Public Function ButtonCheck(Slctn As String, Trgt as Control, btnDel as Control, btnCancelDel as Control)
Select Case Slctn
Case "Delete"
Trgt.SetFocus
'pass the controls needed by the next function(s)
Call BttnAct("Show", "Hide", btnDel, btnCancelDel)
.
.
.
End Function

Public Function BttnAct(Dlt As String, Srch As String, btnDel as Control, btnCancelDel as Control)
Select Case Dlt
.
.
.
Case "Show"
'keep passing the controls
BttnVs ("True", btnDel, btnCancelDel)
End Select
.
.
.
End Function

Public Function BttnVs(DltLgc As String, btnDel as Control, btnCancelDel as control)
'set the visible property of the passed controls
btnDel.Visible = DltLgc
btnCancelDel.Visible = DltLgc
End Function

Try something like that.

Sorry about leaving you earlier - got very busy.

Doug.
 
Last edited:

Randomblink

The Irreverent Reverend
Local time
Yesterday, 23:32
Joined
Jul 23, 2001
Messages
279
Yippeee!

That sounds like it will work...

I don't have time to try it out just yet...

But I do have a question that stopped me from trying it earlier on my own...

When you create a function...eg
Public Function Goof(Freak As String)
Psycho(Freak)
...End Function

Public Function Psycho(Freak As String)
whatever
...End Function

Won't the Freak As String in the Psycho Function clear out the variable? Doesn't that Freak As String bit Dim or Clear the Variable...?

Just a question...

Oh, and THANKS for the help man.
I gotta tell you, this board is the greatest in the freaking world...

If I made real money I would donate or something...
In fact, I wonder why they haven't set up a paypal button or something for people to donate...
Even tho I barely scrape by I would drop a buck here and there for the help I get...Sheesh! This place rocks...
Ah well...<raises glass>
To the day when I make enough money to buy and sell boards this good...<drink>
 

Users who are viewing this thread

Top Bottom