New to Modules

G

Guest

Hi, I'm new to writing modules and I'm looking for help with a specific code
I'm trying to run. I'm trying to use this module in numerous forms and based
on the forms caption, it should close the current form, opent the switchboard
and then re-name the caption of a text box which I'll run another module off
that caption. The problem I'm haing is it isn't recognizing the "Caption"
portion in the first line of code. It gives me an "Argument not Optional"
error.

Can anyone look at the following code and tell what's wrong with it? Thanks
in advance.

Public Function CloseTrainingFrm()

If Caption = "MXG Information Assurance Training" Then

DoCmd.Close
' DoCmd.OpenForm "frmSwitchboard"
' Forms!frmSwitchboard.IATraining = "Open MXG Training Form"

End If


End Function
 
D

Douglas J Steele

What is Caption supposed to be?

If this code is running within a form's module and it's supposed to give you
the caption of that form, you need Me.Caption.

If you're trying to determine whether there's an open form with that
caption, you need something like:

Dim intLoop As Integer

For intLoop = Forms.Count - 1 To 0 Step -1
If Forms(intLoop).Caption = "MXG Information Assurance Training" Then
DoCmd.Close acForm, Forms(intLoop).Name
End If
Next intLoop
 
G

Guest

Steve,

You are correct, I'm using the current forms caption as the control source(I
guess) to determine a text fields caption on the switchboard. Big picture, I
have numerous offices who will track training through this database and I
want to control there footprint within the database based on office. I don't
want MXG looking at OG training, I only want MXG to see there training
records. I'm trying to write code to check the caption on the current form
(up to 12 forms) and based on the current forms caption, the text caption on
the switchboard will be written by the code so when MXG closes there training
for to return to the switchboard, the text caption on the switchboard will =
"58 MXG Training."

The code you wrote below is foriegn to me nor did it work. Can you explain
it to me?

Thanks for the help.


Rick
 
D

Douglas J Steele

I was suggesting that you replace the code you have for Function
CloseTrainingFrm with the code I gave:

Public Function CloseTrainingFrm()
Dim intLoop As Integer

For intLoop = Forms.Count - 1 To 0 Step -1
If Forms(intLoop).Caption = "MXG Information Assurance Training" Then
DoCmd.Close acForm, Forms(intLoop).Name
End If
Next intLoop

End Function

To be even more generic, you might pass the caption you want to ignore to
the function:

Public Function CloseTrainingFrm(WhatCaption As String)
Dim intLoop As Integer

For intLoop = Forms.Count - 1 To 0 Step -1
If Forms(intLoop).Caption = WhatCaption Then
DoCmd.Close acForm, Forms(intLoop).Name
End If
Next intLoop

End Function
 

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