PC Review


Reply
Thread Tools Rate Thread

changing to a function or sub

 
 
John
Guest
Posts: n/a
 
      27th Jul 2010
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
 
Reply With Quote
 
 
 
 
John W. Vinson
Guest
Posts: n/a
 
      27th Jul 2010
On Tue, 27 Jul 2010 11:56:12 -0700 (PDT), John <(E-Mail Removed)> wrote:

>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/For...-US/accessdev/
http://social.answers.microsoft.com/.../en-US/addbuz/
and see also http://www.utteraccess.com
 
Reply With Quote
 
John
Guest
Posts: n/a
 
      28th Jul 2010
On Jul 27, 4:24*pm, John W. Vinson
<jvinson@STOP_SPAM.WysardOfInfo.com> wrote:
> On Tue, 27 Jul 2010 11:56:12 -0700 (PDT), John <jevans10...@yahoo.com> wrote:
> >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 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/For.../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
 
Reply With Quote
 
John W. Vinson
Guest
Posts: n/a
 
      28th Jul 2010
On Tue, 27 Jul 2010 22:40:40 -0400, "mbyerley"
<mDotByerley@VerizonDottieNettie> wrote:

>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/For...-US/accessdev/
http://social.answers.microsoft.com/.../en-US/addbuz/
and see also http://www.utteraccess.com
 
Reply With Quote
 
David W. Fenton
Guest
Posts: n/a
 
      28th Jul 2010
John W. Vinson <jvinson@STOP_SPAM.WysardOfInfo.com> wrote in
news:(E-Mail Removed):

> On Tue, 27 Jul 2010 22:40:40 -0400, "mbyerley"
><mDotByerley@VerizonDottieNettie> wrote:
>
>>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!


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.

--
David W. Fenton http://www.dfenton.com/
contact via website only http://www.dfenton.com/DFA/
 
Reply With Quote
 
Ron2006
Guest
Posts: n/a
 
      29th Jul 2010
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



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA function changing its name John B. Smotherman Microsoft Access VBA Modules 0 24th May 2010 03:56 PM
VBA function changing name John B. Smotherman Microsoft Access VBA Modules 0 24th May 2010 02:51 PM
Changing Function Elizabeth- MDC Microsoft Access VBA Modules 1 8th Oct 2008 04:38 AM
NOW() function that is un-changing =?Utf-8?B?Q2hlZXNl?= Microsoft Excel Misc 7 19th Mar 2007 12:51 AM
Changing a delete function to an export function =?Utf-8?B?bm1hcmFubw==?= Microsoft Access VBA Modules 4 11th Mar 2005 01:05 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:35 PM.