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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting form attributes in a global Module

Status
Not open for further replies.

jedel

Programmer
Jan 11, 2003
430
AU
I am creating a simple relational dbase using Access 97 (yes I know, But work won't upgrade it). We are using Windows NT and I have some code that I think may have come from this forum, that allows the database to select the user login ID from Windows NT. This is really handy because now I can determine what LEVEL of security each user has by deciding on what he can and cannot see by setting the attributes on each form eg if someone opens the database and is not in the user table, then he can only have veiw access because the forms are set so that there no Additions, deletions or edits.

My dilema, I am duplicating the same code for each form for the "on Load" event. I would like to write the code only once in a global module and refer to this on each form. The code that I am using is:


Private Sub Form_Load()
Dim Access1 As String
Access1 = [Forms]![frm_access]![Access]
Select Case Access1
Case "Developer"
Me.AllowAdditions = True
Me.AllowDeletions = True
Me.AllowEdits = True

Case "administrator"
Me.AllowAdditions = True
Me.AllowDeletions = True
Me.AllowEdits = True

Case "User"
Me.AllowAdditions = True

Case "nil"
DoCmd.OpenForm ("frm_application")
End Select

End Sub

How can I convert this code so that I can refer to it using any form in the database

Cheers

JEDEL
 
You will need to pass the form object to your function so you can reference it. I would also change it to be a boolean so that you can call frm_application from the program. Try something like this:

Public Function CheckRights(frm As Form) As Boolean
Dim Access1 As String
Access1 = Forms(frm.name).[Access]
Select Case Access1
Case "Developer", "administrator"
frm.AllowAdditions = True
frm.AllowDeletions = True
frm.AllowEdits = True
CheckRights = True
Case "User"
frm.AllowAdditions = True
CheckRights = True
Case "nil"
CheckRights = False
Case Else
MsgBox "Unexpected Program Result " & Access1
CheckRights = False
End Select
End Sub

Then from your form load code do this:

If Not CheckRights(Me.Form) Then
DoCmd.OpenForm ("frm_application")
End If

This way everything is done in the function except calling the other form if you encounter an error. Hope this helps.

Good LucK!
 
SBendBuckeye,
Cheers! The code worked flawlessly. Take a pinky coloured star. For your info the code used to check the NT User ID is this:

In the Module heading goes this code:

Private Declare Function GetComputerNameA Lib "kernel32" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetUserName Lib "ADVAPI32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

lower down the Module is this:

Public Function GetComputerName() As String
On Error GoTo Err_GetComputerName
Dim Username As String * 255
Call GetComputerNameA(Username, 255)
GetComputerName = Left$(Username, InStr(Username, Chr$(0)) - 1)

Exit_GetComputerName:
Exit Function

Err_GetComputerName:
MsgBox Err.Description
Resume Exit_GetComputerName

End Function
Public Function GetCurrentUserName() As String
On Error GoTo Err_GetCurrentUserName
Dim lpBuff As String * 25
Dim ret As Long, Username As String
ret = GetUserName(lpBuff, 25)
Username = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
GetCurrentUserName = Username & ""

Exit_GetCurrentUserName:
Exit Function

Err_GetCurrentUserName:
MsgBox Err.Description
Resume Exit_GetCurrentUserName
End Function

Now I have two forms made. One that accesses this code and places the NT Username in the field. The second opens directly with a query in the datasourse that accesses the first form to determine which user to logon. The "Access" form is then hidden and referred to to check what level of access the user has (Developer, Admionistrator, User etc)

If the user is not listed in the table, your code comes into it and opens an application form so the administrator can reveiw who wants access. He can then transfer the relevant forms over simply by clicking a button. Once the applicant has filled in the form he is kicked out of the database. So I only needed to put the second bit of the code on one form.

Your help was invaluable, Thanks again

JEDEL
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top