changing to a function or sub

J

John

Hello. I have the following code that I would like to put into a
function or a sub and I am not skilled in that area. Should be, but
never really had a reason. I've taken over this db and the prior dev
used functions and subs alot. I have 4 forms (opened by buttons) that
are getting restricted to employees only, no contractor access. The
following code works but is in the click event of one of the buttons.
I would prefer not to have this in 4 places incase a change is
needed. The line that starts with MyOpenForm was originally the only
line in the button click event. In all 4 cases, this line would
change.

Can someone take a look at this and see if a call to a function or sub
can be made out of this?

Thanks.

Dim user_allowed_so_access As Boolean
user_allowed_so_access = DLookup("Contractor", "Employees", "ID=" &
gUserID)

If user_allowed_so_access = True Then
MsgBox "You are not allowed access to the Sales Order
information.", vbOKOnly, "No Access"
Exit Sub

Else
MyOpenForm "SalesOrders", , , , acFormAdd

End If
 
J

John W. Vinson

Hello. I have the following code that I would like to put into a
function or a sub and I am not skilled in that area. Should be, but
never really had a reason. I've taken over this db and the prior dev
used functions and subs alot. I have 4 forms (opened by buttons) that
are getting restricted to employees only, no contractor access. The
following code works but is in the click event of one of the buttons.
I would prefer not to have this in 4 places incase a change is
needed. The line that starts with MyOpenForm was originally the only
line in the button click event. In all 4 cases, this line would
change.

Can someone take a look at this and see if a call to a function or sub
can be made out of this?

Thanks.

Dim user_allowed_so_access As Boolean
user_allowed_so_access = DLookup("Contractor", "Employees", "ID=" &
gUserID)

If user_allowed_so_access = True Then
MsgBox "You are not allowed access to the Sales Order
information.", vbOKOnly, "No Access"
Exit Sub

Else
MyOpenForm "SalesOrders", , , , acFormAdd

End If

Just create a new Module on the Modules tab, and create a new public function
(*not* a Sub) containing your desired code. It's not obvious what gUserID is -
a form reference? a global variable? or what? You might need to pass it as a
parameter to the function. In any case you'll want to pass the name of the
desired form as a parameter.

For example:

Public Function OpenSesame(strForm As String)
Dim user_allowed_so_access As Boolean
user_allowed_so_access = DLookup("Contractor", "Employees", "ID=" _
& gUserID)
If user_allowed_so_access = True Then
MsgBox "You are not allowed access to the Sales Order information.", _
vbOKOnly, "No Access"
Exit Sub
Else
MyOpenForm strForm, , , , acFormAdd
End If
End Function

In the Click event property of the button which opens SalesOrders you'ld put

=OpenSesame("SalesOrders")

--

John W. Vinson [MVP]
Microsoft's replacements for these newsgroups:
http://social.msdn.microsoft.com/Forums/en-US/accessdev/
http://social.answers.microsoft.com/Forums/en-US/addbuz/
and see also http://www.utteraccess.com
 
J

John

Just create a new Module on the Modules tab, and create a new public function
(*not* a Sub) containing your desired code. It's not obvious what gUserIDis -
a form reference? a global variable? or what? You might need to pass it as a
parameter to the function. In any case you'll want to pass the name of the
desired form as a parameter.

For example:

Public Function OpenSesame(strForm As String)
Dim user_allowed_so_access As Boolean
user_allowed_so_access = DLookup("Contractor", "Employees", "ID=" _
          & gUserID)
If user_allowed_so_access = True Then
    MsgBox "You are not allowed access to the Sales Order information..", _
vbOKOnly, "No Access"
    Exit Sub
Else
    MyOpenForm strForm, , , , acFormAdd
End If
End Function

In the Click event property of the button which opens SalesOrders you'ld put

=OpenSesame("SalesOrders")

--

             John W. Vinson [MVP]
Microsoft's replacements for these newsgroups:http://social.msdn.microsoft.com/Fo...al.answers.microsoft.com/Forums/en-US/addbuz/
and see alsohttp://www.utteraccess.com- Hide quoted text -

- Show quoted text -


Thanks for the reply and feedback. Looks like I was close but missed
several key factors, ones that I will remember for the next time.
....John
 
J

John W. Vinson

Why a Sub if there's no return value from your function?

Because you can call a function using the syntax

=Functionname()

directly in the Event property, without the need to use a Macro with RunCode
or an extra Event Procedure that does nothing but call a sub. Sorry, should
have explained that!
--

John W. Vinson [MVP]
Microsoft's replacements for these newsgroups:
http://social.msdn.microsoft.com/Forums/en-US/accessdev/
http://social.answers.microsoft.com/Forums/en-US/addbuz/
and see also http://www.utteraccess.com
 
M

mbyerley

Makes sense.. Thanks.

John W. Vinson said:
Because you can call a function using the syntax

=Functionname()

directly in the Event property, without the need to use a Macro with
RunCode
or an extra Event Procedure that does nothing but call a sub. Sorry,
should
have explained that!
--

John W. Vinson [MVP]
Microsoft's replacements for these newsgroups:
http://social.msdn.microsoft.com/Forums/en-US/accessdev/
http://social.answers.microsoft.com/Forums/en-US/addbuz/
and see also http://www.utteraccess.com
 
D

David W. Fenton

Because you can call a function using the syntax

=Functionname()

directly in the Event property, without the need to use a Macro
with RunCode or an extra Event Procedure that does nothing but
call a sub. Sorry, should have explained that!

For what it's worth, even in functions whose purpose is only to be
called as event properties as above, I always define as return
value, in this type of case, a Boolean, and I return True.

A function without a defined return value is nonsense, and bad
coding, even if there is no actual penalty for that when using it
this way.
 
R

Ron2006

There is always an alternative.

In the oncurrent event of the main form with the buttons,

add the code:

Dim user_allowed_so_access As Boolean
user_allowed_so_access = DLookup("Contractor", "Employees", "ID=" _
& gUserID)
If user_allowed_so_access = True Then
me.btn1.enabled = true
me.btn2.enabled = true
Else
me.btn1.enabled = false
me.btn2.enabled = false

End If

or even more straightforward:

Dim user_allowed_so_access As Boolean
user_allowed_so_access = DLookup("Contractor", "Employees", "ID=" _
& gUserID)
me.btn1.enabled = user_allowed_so_access
me.btn2.enabled = user_allowed_so_access


Or maybe me.btn1.visible = user_allowed_so_access


Ron
 

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