Making Form Read only based on Windows User - Code correction requ


Irshad Alam

Dear Sir/Madam,

I am trying to make for certain user read only form. I tried the below code.
It failed.
Below are the codes

These below are from module:====

Option Compare Database
Option Explicit

Declare Function apiGetUserName Lib "advapi32" Alias "GetUserNameA" (ByVal
buffer As String, BufferSize As Long) As Long
Declare Function apiGetComputerName Lib "kernel32" Alias "GetComputerNameA"
(ByVal buffer As String, BufferSize As Long) As Long

Function GetUserName() As String
'Wrapper for GetUserName API call.
'Procedure Syntax
'Dim strUserName As String
'strUserName = GetUserName
'Procedure Syntax
Dim strName As String
Dim lngSize As Long
Dim lngRetVal As Long
strName = Space(15)
lngSize = 15
lngRetVal = apiGetUserName(strName, lngSize)
GetUserName = left$(strName, lngSize - 1)
End Function

Function UserAccessInfo()
Dim frm As Form
If GetUserName = "mia" Then
With frm
..AllowAdditions = False
..AllowDeletions = False
..AllowEdits = False
End With
With frm
..AllowAdditions = True
..AllowDeletions = True
..AllowEdits = True
End With
End If
End Function

*** Trying to call on the all the form opening :

Private Sub Form_Open(Cancel As Integer)
DoCmd.RunCommand acCmdRecordsGoToNew
Call UserAccessInfo
End Sub

Producing error
Run Time error No. 91 - object variable or with block variable not set

Please correct my code and advice.



John Spencer

Your code does not set the variable frm to any specific form. So it is going
to error. You can either pass a reference to a form or get the current form.

Function UserAccessInfo(frm as Form)
'Dim frm As Form - Get rid of this
<<< Your code here>>>

End Function

call it from a form or subform with
UserAccessInfo Me

Or you can use a specific reference to a specific form
UserAccessInfo Forms![NameOfSpecificForm]
UserAccessInfo Forms("Name of Specific Form")

You might want to add some error handling.

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

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