Set form's properties from a Public Function in a utilities module

J

Jim Evans

I am trying to build a reusable function in a utilities module which will
set the calling form's .Visible property to False and then run a
DoCmd.OpenForm procedure to open another form. This function will be called
from a form's Close button click event.

I am able to open the new form with no problem but, I am not able to get the
code correct to set the calling form's .Visible property.

This is in Access 2003 and the function code that works is below:
--------------------
Public Function fOpenFormsTest(frmOpenForm As String, frmCloseForm As
String)
Dim strOpen As String
Dim strClose As String

--------- This is where I would like to set the .Visible property of
"frmCloseForm"---------

DoCmd.OpenForm frmOpenForm

End Function
 
D

Dirk Goldgar

Jim Evans said:
I am trying to build a reusable function in a utilities module which will
set the calling form's .Visible property to False and then run a
DoCmd.OpenForm procedure to open another form. This function will be called
from a form's Close button click event.

I am able to open the new form with no problem but, I am not able to get
the code correct to set the calling form's .Visible property.

This is in Access 2003 and the function code that works is below:
--------------------
Public Function fOpenFormsTest(frmOpenForm As String, frmCloseForm As
String)
Dim strOpen As String
Dim strClose As String

--------- This is where I would like to set the .Visible property of
"frmCloseForm"---------

DoCmd.OpenForm frmOpenForm

End Function


Assuming your argument frmCloseForm is a string containing the name of the
form you want to hide:

Forms(frmCloseForm).Visible = False

Be aware that that line of code just hides the form; it doesn't close it.
 
J

Jim Evans

Thanks, Dirk.

Dirk Goldgar said:
Assuming your argument frmCloseForm is a string containing the name of the
form you want to hide:

Forms(frmCloseForm).Visible = False

Be aware that that line of code just hides the form; it doesn't close it.

--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)
 
J

Jack Leach

I believe the form's Close event is to late to try and hide it... the form at
that point is already commited to closing. I'm not sure what you need this
for, but you may want to use the Unload event instead (utilizing Cancel =
True). This *may* keep the form from a commitment to close, thus allowing it
to be hidden.

I *think* that you can use Cancel = True in the unload event, and subsequent
code will still be run. It is my practice to immediately use redirect the
code to the exit procedure as soon as Cancel is set in order to keep any
other code from running.

In this case, you may want to Cancel the Unload, and immediately after that,
call your public function. I think this will run the function with the
Cancel in place, and allow the form to be Visible at that point.

Not sure of the details, but this may get you on some sort of path.

hth

--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)
 
J

Jack Leach

Well, aside from having to change the Event that the function is called from,
you want to pass the instance of your form to the public module as a Type
Form rather than String of the form name.

Ex:

Public Function SetVisible(frm As Form, bVis As Boolean)
frm.Visible = bVis
End Function

and to call it from the form you want to set...

SetVisible Me

this passes the form (Me) rather than the name of the form as you would in a
string. To reference that form passed as a string, you would need:

Public Function SetVisible(formname As String)
Dim frm As Form
Set frm = Forms(formname)
frm.Visible = False
Set frm = Nothing
End Function



hth
--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
J

Jim Evans

Jack,

Thanks for your input, but I don't believe that you understand what I want
to do.

Dirk's example was perfect for the task I want to do.
 
D

Dirk Goldgar

Jim Evans said:
Jack,

Thanks for your input, but I don't believe that you understand what I want
to do.

Dirk's example was perfect for the task I want to do.

Jim, I'm glad I was able to help, but you should know that Jack's suggested
SetVisible function is a more versatile approach to the problem. I just
gave you the quick and simple answer to the specific question you asked.

When the name of the form is passed as a string, the code to hide it has to
create a reference to the form object in order to set that object's .Visible
property. If you were in a position to pass a reference to the object
instead of its name, that would be more efficient, since you could operate
directly on the object without the intervening step.

In the context of your original function, fOpenFormsTest, the function would
have to be changed from:

Function fOpenFormsTest(frmOpenForm As String, frmCloseForm As String)

Forms(frmCloseForm).Visible = False
' ...

End Function

to:

Function fOpenFormsTest(frmOpenForm As String, frmCloseForm As Form)

frmCloseForm.Visible = False
' ...

End Function

Then the code that calls this function would be changed from:

fOpenFormsTest "FormNameToOpen", Me.Name

to:

fOpenFormsTest "FormNameToOpen", Me

That is, in fact, the way I would handle a problem of this nature. However,
it's no big deal either way.
 
J

Jim Evans

Dirk and Jack,

I re-read my response to Jack's reply to my question and I hope that neither
of you interpreted it as being offensive. It certainly was not meant in that
spirit. I am amazed by and extremely appreciative of all of you that are
willing to share your time and knowledge with those of us who are running
into a problem and can't get something we are attempting to do to work as we
imagined it.

When I read your response, Jack, I did not really understand your thoughts.
I should have read it more carefully.

If I have offended either of you, I apologize full heartedly and I
appreciate the assistance both of you give so freely. Many of us benefit
greatly from solutions that you offer, and probably most often, you never
hear about the help you have provided well beyond the scope of answering the
question of an individual.
 
J

Jack Leach

No worries Jim!
Public Function SetVisible(formname As String)
Dim frm As Form
Set frm = Forms(formname)
frm.Visible = False
Set frm = Nothing
End Function

After posting this I saw Dirk's original reply, and had to slap myself...

Forms(formname).Visible = True

would work fine without having to make the variables, set them, close them
after. Duh.


I find the passing of the Form itself to be very handy in other cases as
well. For instance, all of the controls in whatever form gets passed are
available for your coding please in a public function as well. A common
example is a public function to set the Create and Modified controls from a
public function...

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.NewRec
SetTimeStamps Me, 0
End If
SetTimeStamps Me, 1
End Sub

Public Function SetTimeStamps(frm As Form, iCrOrMod)
If iCrOrMod = 0 Then
'set created
frm.ctlDateCreated = Now()
frm.ctlCreatedBy = "youruser"
Else
'set modified
frm.ctlDateModified = Now()
frm.ctlModifiedBy = "youruser"
End If
End Function



So now, if you use a common naming scheme of controls called ctlDateCreated
and ctlDateModified etc on all of your forms, it becomes very easy to handle
and there's no duplicate code. You won't see the controls in intellisense
from a public function, but as long as they exist on the form you pass to it
you'll be good to go.

This particular programming concept was one of those things that opened tons
of doors for me... the ability to use an object variable like that cut the
code in my app near to half! Even using a control object in a function to
verify a phonenumber or email address...

(aircode)
Private Sub ctlPhoneNum_BeforeUpdate(Cancel As Integer)
If Not fVerifyPhone(Me.ctlPhoneNum) Then
Cancel = True
End If
End Sub

Public Function fVerifyPhone(ctl As Control) As Boolean
Dim sText As String
sText = ctl.Value
'test your format
If yourformat = True Then fVerifyPhone = True
End Function


anyway, you get the idea. It really is a golden concept of programming.


Glad to help!

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top