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

Read only forms from a table of users

Status
Not open for further replies.

wkdstef

Technical User
Oct 26, 2005
8
GB
can anybody help me.
I'm trying to read from a table of users to find out who has read only access to a form. I have some code but i think that it is wrong somewhere.

First is a module with:-
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Next is the code on the on open part of the form:-
Private Sub Form_Open(Cancel As Integer)

Dim User1 As String ' variable to hold name

Dim rst As Recordset 'variable to hold User names

Dim i As Long 'variable to hold rst record loop

Dim chkAllowed As Boolean 'variable to hold whether read only or not (read only = -1, Allowed = 0)

User1 = Space(255) 'create a

GetUserName User1, 255

Set rst = CurrentDb.OpenRecordset("Users", dbOpenDynaset)

chkAllowed = 0 'allocate variable to allowed initially

If rst.RecordCount > 0 Then

'populate Recordset

rst.MoveLast

rst.MoveFirst

For i = 1 To rst.RecordCount

If rst!Uname = User1 Then ' there is a match therefore set to read only

chkAllowed = -1

Exit For 'exit loop if match found ie. Read only

End If
rst.MoveNext
Next i


End If

If chkAllowed = -1 Then

Me.AllowAdditions = -1
Me.AllowDeletions = -1
Me.AllowEdits = -1

Me.DelDateSubform.Form.AllowAdditions = -1
Me.DelDateSubform.Form.AllowDeletions = -1
Me.DelDateSubform.Form.AllowEdits = -1

Me.VisChangeSubform.Form.AllowAdditions = -1
Me.VisChangeSubform.Form.AllowDeletions = -1
Me.VisChangeSubform.Form.AllowEdits = -1

Me.[Delivery SubForm].Form.AllowAdditions = -1
Me.[Delivery SubForm].Form.AllowDeletions = -1
Me.[Delivery SubForm].Form.AllowEdits = -1
Else

Me.AllowAdditions = 0
Me.AllowDeletions = 0
Me.AllowEdits = 0

Me.DelDateSubform.Form.AllowAdditions = 0
Me.DelDateSubform.Form.AllowDeletions = 0
Me.DelDateSubform.Form.AllowEdits = 0

Me.VisChangeSubform.Form.AllowAdditions = 0
Me.VisChangeSubform.Form.AllowDeletions = 0
Me.VisChangeSubform.Form.AllowEdits = 0

Me.[Delivery SubForm].Form.AllowAdditions = 0
Me.[Delivery SubForm].Form.AllowDeletions = 0
Me.[Delivery SubForm].Form.AllowEdits = 0
End If

Exit Sub

End Sub
 
If you don't have windows 9x box, you may simply do this:
Code:
chkAllowed = Not IsNull(DLookUp("Uname", "Users", "Uname='" & Environ("USERNAME") & "'"))

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PH. But i think the problem lies in the bit of code that is (chkAllowed =0 or -1 or where the rst.MoveNext is):-

chkAllowed = 0 'allocate variable to allowed initially

If rst.RecordCount > 0 Then 'populate Recordset

rst.MoveLast

rst.MoveFirst

For i = 1 To rst.RecordCount

If rst!Uname = User1 Then ' there is a match therefore set to read only

chkAllowed = -1

Exit For 'exit loop if match found ie. Read only

End If
rst.MoveNext
Next i

End If

If chkAllowed = -1 Then
 
So, you want to stay with the long route ...
Replace this:
If rst!Uname = User1 Then
with this:
If Trim(rst!Uname) = Trim(User1) Then

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top