Argument not optional error

J

Jon

Greeting,

I have the following module that it groups controls in form :
**********************
Public Sub HideIt(frm As Form)
On Error Resume Next
Dim ctl As Control
For Each ctl In frm.Controls
With ctl
If ctl.Tag = "GroupA" Then
ctl.Visible = False
End If
End With
Next ctl
Set ctl = Nothing
Set frm = Nothing
End Sub
********************
I used it in the form with the following condition:
**********************
If Me.Check17 = True Then
HideIt
End If
**********************
But the following error occurs:
Compile errors: Argument not optional

How to fix it please??
 
R

RoyVidar

Jon said:
Greeting,

I have the following module that it groups controls in form :
**********************
Public Sub HideIt(frm As Form)
On Error Resume Next
Dim ctl As Control
For Each ctl In frm.Controls
With ctl
If ctl.Tag = "GroupA" Then
ctl.Visible = False
End If
End With
Next ctl
Set ctl = Nothing
Set frm = Nothing
End Sub
********************
I used it in the form with the following condition:
**********************
If Me.Check17 = True Then
HideIt
End If
**********************
But the following error occurs:
Compile errors: Argument not optional

How to fix it please??

The Sub header specify that you are to pass the form as parameter,
so, try;

If Me.Check17 = True Then
HideIt Me
End If
 
T

Tom van Stiphout

On Sat, 21 Mar 2009 03:36:01 -0700, Jon

That is because HideIt is declared with an argument:
Public Sub HideIt(frm As Form)
So if you want to call it, you need to pass in a form argument, like
this:
HideIt Me

-Tom.
Microsoft Access MVP
 
J

Jon

Thank you both , but if I want to disable the module if condition is false!!
How to do it???
 
J

Jon

Hi again,

is there any way to inform user if there is a data in the controls that they
can not be invisible and cancel the module??
 
R

RoyVidar

Jon said:
Thank you both , but if I want to disable the module if condition is
false!! How to do it???

I don't understand your question. To disable the module, I'd think
you didn't want the sub to run if the condition was false, and that
should be the excact way your sub is working.

If you mean to toggle the status depending on the check box, you
could also pass the status, here's some air code, hopefully there
aren't too many typos


HideIt Me, Me.Check17

Public Sub HideIt(frm As Form, fHide as Boolean)

On Error Resume Next

Dim ctl As Control

For Each ctl In frm.Controls
With ctl
If ctl.Tag = "GroupA" Then
ctl.Visible = Not fHide
End If
End With
Next ctl

Set ctl = Nothing
Set frm = Nothing
End Sub
 
R

RoyVidar

Jon said:
Hi again,

is there any way to inform user if there is a data in the controls
that they can not be invisible and cancel the module??

That means you probably have to loop twice. The first time, check
all the controls that have a value property, then, if all is good,
run your hiding thing
 
J

Jon

hi RoyVidar and many thanks for help
would you please reprogram the code to be looping twice????
 
R

RoyVidar

Jon said:
hi RoyVidar and many thanks for help
would you please reprogram the code to be looping twice????

Here is one little attempt

Sub HideIt(frm As Access.Form, fHide As Boolean)

Dim ctl As Access.Control
Dim fNoValue As Boolean

On Error GoTo MyErr

' in case the control having focus, is also part
' of this "GroupA", then you will need to set
' focus to another control, as Access can't
' toggle visible state of controls having the
' focus, for instance
' frm.Controls("txtNameOfControl").SetFocus

For Each ctl In frm.Controls
If ctl.Tag = "GroupA" Then
' only selecting types of control having a
' .value property
Select Case ctl.ControlType
Case acTextBox, acCheckBox, acListBox, acComboBox
fNoValue = (Len(ctl.Value & vbNullString) = 0)
If fNoValue Then
MsgBox "There is no value in '" & _
ctl.Name & "'," & vbNewLine & _
"please enter a value and try again.",
_
vbExclamation, "Missing value!"
Set ctl = Nothing
Exit Sub
End If
Case Else
' do nothing
End Select
End If
Next ctl

For Each ctl In frm.Controls
If ctl.Tag = "GroupA" Then
ctl.Visible = Not fHide
End If
Next ctl

MyExit:
Set ctl = Nothing
Exit Sub
MyErr:
MsgBox Err.Description
Resume MyExit
End Sub
 

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