Calling a Sub

  • Thread starter Thread starter James
  • Start date Start date
J

James

Hello I have the following Code and I would like to call
it to a number for forms:
-----------------------------------------------------------
Dim ctrl As Control

For Each ctrl In Me.Controls
If usrLevel = 3 Then
If ctrl.ControlType = acTextBox Then
ctrl.Locked = True
Else
If ctrl.ControlType = acTextBox Then
ctrl.Locked = True
End If
End If
End If

Next ctrl
-----------------------------------------------------------
How could I achieve this? Is there an easy way of just
getting a module to run on startup or something and it
does what I am asking above?

So could I get the module to run on startup that looks at
all the open forms and the uerlevel of the person logged
in and apply the above security?

Many Thanks

James
 
James said:
Hello I have the following Code and I would like to call
it to a number for forms:
-----------------------------------------------------------
Dim ctrl As Control

For Each ctrl In Me.Controls
If usrLevel = 3 Then
If ctrl.ControlType = acTextBox Then
ctrl.Locked = True
Else
If ctrl.ControlType = acTextBox Then
ctrl.Locked = True
End If
End If
End If

Next ctrl

Place the sub in a standard code module (not one associated with a Report
or Form) and then just call it in the Open event of any form you want to
apply it to. All you have to do is add an input argument that tells the
routine which form to run against and include that when calling the
routine.

Sub YourSubName(FormName As String)

Dim ctrl As Control

For Each ctrl In Forms(FormName).Controls
If usrLevel = 3 Then
If ctrl.ControlType = acTextBox Then
ctrl.Locked = True
Else
If ctrl.ControlType = acTextBox Then
ctrl.Locked = True
End If
End If
Next ctrl

End Sub

In the form's Open event...

Call YourSubName(Me.Name)
 
Put these two lines in a standard module (at Modules tab) and insert your code
between the lines:

Public Sub MySub()

End Sub

You can then run your code anywhere in your application with the code:
Call MySub
 
Thanks for that.

When I use my code in a standard module I get an error
telling me that its an invalid use of Me argument which I
can understand but I am calling my module (below) and it
still falls over.

So if I were to use your code provided in your reply would
I have to go through all the forms?

Its just i have about 20 forms that I need to call this
from and I was wondering if there is some code that says
something along the lines of:

Current Form *then looks at the usrlevel and if = 3 then
follow lockdown procedure* if not then leave it alone but
to do that when ever a form is opened or would I have to
call the module on each forms on open command?

Many Thanks

James
 
I have tried this and unfortunatly it does not work as it
is falling over on the invalid use of Me command.

Is there any way I can achieve my goels in a smiple and
time time effective mannor?

Many Thanks

James
 
james said:
Thanks for that.

When I use my code in a standard module I get an error
telling me that its an invalid use of Me argument which I
can understand but I am calling my module (below) and it
still falls over.

"Me" only works in a code module attached to a Report or Form in which case
"Me" is a shorthand reference to the particular Report or Form. That is
why when you move the code to a standard module you have to replace "Me"
with the name of the form that the code should run against. That is what
my example does.
So if I were to use your code provided in your reply would
I have to go through all the forms?

The code is generic in that it would work against any form you want it to,
but each form will require that a Call statement be added to its Open event
in order for the routine to run.
 
Sorry! Missed that point.

Try the following:

Put the following code in a standard Module:

Public Sub MySub(MyForm As Form)
Dim ctrl As Control

For Each ctrl In MyForm.Controls
If usrLevel = 3 Then
If ctrl.ControlType = acTextBox Then
ctrl.Locked = True
Else
If ctrl.ControlType = acTextBox Then
ctrl.Locked = True
End If
End If
End If

Next ctrl

End Sub

You can then run your code anywhere in your application with the code:

Dim frmCurrentForm As Form
Set frmCurrentForm = Screen.ActiveForm
Call MySub(frmCurrentForm)

Steve
PC Datasheet
 
Back
Top