Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a Precedure to Call in other modules / Correct code

Status
Not open for further replies.

Scoob55k

Technical User
Nov 16, 2005
62
0
0
US
Can you check this code as it is erroring out saying:

"Run-time Error '2001':

You cancelled the previous operation


Can you tell me the error of my ways in the code below? Where should I put it (Form_Load, Form_Open, etc.?):

Private Sub Form_Load()
'Checks User Security
Dim UserName As String
UserName = Environ$("USERNAME")

If DLookup("[Administrator]", "tblClaimHandlers", "[Alias]=UserName") = True Then
GoTo 1
ElseIf DLookup("[Administrator]", "tblTeam", "[TMAlias]=UserName") = True Then
GoTo 1
ElseIf DLookup("[Administrator]", "tblSection", "[SMAlias]=UserName") = True Then
GoTo 1
Else
MsgBox "You do not have permission to view this section. Contact your administrator to gain access.", vbExclamation, "Security Warning"
DoCmd.Close
End If

1
End Sub


Besides that, I'd like to create a coded procedure that I can call On Load or On Open instead of having to insert the code below in every form's code module. That for obvious reasons, but most because of any updates I make, I will only have to make them in one spot.

Thanks!!
 
How are Scoob55k . . .

Easiest would be to run the security code before you open the form! If you don't then you'll have to do some error trapping to trap the very reason your here.

As an example, copy/paste the following to a [blue]module[/blue] in the [blue]modules window[/blue]:
Code:
[blue]Public Sub VerifyPermission(("[purple][b][i]frmName[/i][/b][/purple] As String)
   Dim UserName As String, fld As String, flg As Boolean
   Dim Msg As String, Style As Integer, Title As String, DL As String
   
   UserName = Environ$("USERNAME")
   fld = "[Administrator]"
   DL = vbNewLine & vbNewLine
   
   If IsNull(DLookup(fld, "tblClaimHandlers", "[Alias]=UserName")) And _
      IsNull(DLookup(fld, "tblTeam", "[TMAlias]=UserName")) And _
      IsNull(DLookup(fld, "tblSection", "[SMAlias]=UserName")) Then
      MsgBox "You do not have permission to view this section!" & DL & _
             "Contact your administrator to gain access . . ."
      Style = vbCritical + vbOKOnly
      Title = "Security Violation! . . ."
      MsgBox Msg, Style, Title
   Else
       DoCmd.OpenForm [purple][b][i]frmName[/i][/b][/purple]
   End If

End Sub[/blue]
To call the routine:
Code:
[blue]   Call VerifyPermission("[purple][b][i]YourFormName[/i][/b][/purple]")
or
   Call VerifyPermission("[purple][b][i]StringVariable[/i][/b][/purple]")[/blue]
[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .
 
If DLookup("[Administrator]", "tblClaimHandlers", "[Alias]=[tt][!]'" & [/!][/tt]UserName [tt][!]& "'[/!]"[/tt]) = True Then

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Perfect! Thanks to all.

PHV that worked perfect using the code I had. How is one to ever figure that out?

TheAceMan1 I am doing much better now. Thanks for your answer as well. I kind of figured out the creation and calling of a procedure, but your code confirmed it.

Appreciate the education. Take care!
 
One more question.

It's working slick right now except for one form in which I have it doing something OnCurrent. It seems to error out because the form has been closed due to the user (me in this case, testing) not having proper security enabled.

I was thinking if putting code in the Procedure I'm calling to look and see if it's this specific form, but not sure how to do that.

Below is the original code. I've tried to bypass it with an if statement, etc., but no goods.

[Staffing] = DCount("Alias", "tblClaimHandlers", "[Team]=[TMteam]")

FYI - [Staffing] is a field on the form

Thanks a million!
 
Scoob55k . . .

This is a good spot to [blue]start a new thread[/blue] so others can [blue]benefit from its resolution![/blue] [thumbsup2]

Calvin.gif
See Ya! . . . . . .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top