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