UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Make Forms Read Only, Access 2010    
 
   
winterolympic
post Jul 17 2017, 10:26 AM
Post#1



Posts: 400
Joined: 26-March 13



Hi,
I'd like to make a form Read-only depending on user level.
I want to make a sub to do this in module.
CODE
Sub faccess(fname as string)

If Not IsNull(TempVars!LevelID) Then
  If TempVars!LevelID = 4 Then
    Forms(fname).AllowAdditions = False
    Forms(fname).AllowEdits = False
    Forms(fname).AllowDeletions = False
  End If
End If
End sub


And then, in a form, I used below code
CODE
Private Sub form_load
   faccess(me.name)
end sub


I got an error message, it says "Runtime error 2450, can't find form name"
How can I fix this?
Thanks.
This post has been edited by winterolympic: Jul 17 2017, 10:35 AM
Go to the top of the page
 
theDBguy
post Jul 17 2017, 10:30 AM
Post#2


Access Wiki and Forums Moderator
Posts: 70,233
Joined: 19-June 07
From: SunnySandyEggo


Hi,

Which line is getting highlighted?

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Microsoft Access MVP | Access Website | Access Blog | Email
Go to the top of the page
 
winterolympic
post Jul 17 2017, 10:38 AM
Post#3



Posts: 400
Joined: 26-March 13



Sorry, I finally solved my problem.
I changed like below.
CODE
Sub faccess(frm as form)

If Not IsNull(TempVars!LevelID) Then
  If TempVars!LevelID = 4 Then
    frm.AllowAdditions = False
   frm.AllowEdits = False
    frm.AllowDeletions = False
  End If
End If
End sub

and then Call faccess(me)

Thanks.
Go to the top of the page
 
Doug Steele
post Jul 17 2017, 10:39 AM
Post#4


UtterAccess VIP
Posts: 21,200
Joined: 8-January 07
From: St. Catharines, ON (Canada)


I don't have an answer for you, but why bother passing the name? Why not pass a reference to the form itself?

CODE
Sub faccess(WhichForm As Form)
  
  If Not IsNull(TempVars!LevelID) Then
    If TempVars!LevelID = 4 Then
      With WhichForm
        .AllowAdditions = False
        .AllowEdits = False
        .AllowDeletions = False
      End With
    End If
  End If
  
End Sub

then call it like

CODE
Private Sub Form_Load
   Call faccess([Form])
End Sub

In fact, if you're not going to have any other actions associated with the form's Load event, create it as a function, then simply call the function rather than an Event Procedure:

CODE
Function fAccess(WhichForm As Form)
  
  If Not IsNull(TempVars!LevelID) Then
    If TempVars!LevelID = 4 Then
      With WhichForm
        .AllowAdditions = False
        .AllowEdits = False
        .AllowDeletions = False
      End With
    End If
  End If
  
End Sub


Attached File  fAccess.jpg ( 22.34K )Number of downloads: 0



--------------------
Go to the top of the page
 
winterolympic
post Jul 17 2017, 11:22 AM
Post#5



Posts: 400
Joined: 26-March 13



Wow, thank you so much.
I learned!
Go to the top of the page
 
BruceM
post Jul 17 2017, 11:35 AM
Post#6


UtterAccess VIP
Posts: 6,654
Joined: 24-May 10
From: Downeast Maine


What happens if TempVars!LevelID is null?
Go to the top of the page
 
winterolympic
post Jul 17 2017, 12:08 PM
Post#7



Posts: 400
Joined: 26-March 13



I guess, users can edit forms. smile.gif

When a user login correctly, I can get TempVars!LevelID.
If a user fails login, then the user can't go further, so I think I am fine.

Any thought?

Thanks.
Go to the top of the page
 
tina t
post Jul 17 2017, 12:15 PM
Post#8



Posts: 5,049
Joined: 11-November 10
From: SoCal, USA


another option, rather than setting the three properties individiually, is to change the RecordsetType property to Snapshot, which prevents additions/edits/deletes, as

CODE
Function fAccess(WhichForm As Form)
  
  If Not IsNull(TempVars!LevelID) Then
    If TempVars!LevelID = 4 Then
      Whichform.RecordsetType = 2  ' Snapshot
    End If
  End If
  
End Sub

hth
tina

--------------------
"you can't take the sky from me"
Go to the top of the page
 
winterolympic
post Jul 17 2017, 12:22 PM
Post#9



Posts: 400
Joined: 26-March 13



Thanks for the tip!!
I will use this one too.
Go to the top of the page
 
BruceM
post Jul 17 2017, 01:10 PM
Post#10


UtterAccess VIP
Posts: 6,654
Joined: 24-May 10
From: Downeast Maine


You could do this:

If Nz(TempVars!LevelID,4) = 4 Then...

See Help for more about the Nz function.

The reason I asked, though, is I wondered if there was a third condition (the TempVar being null) that needed attention. As I understand, it shouldn't happen at all, but I didn't know that when I asked. Still, not a bad idea to allow for unexpected contingencies.
Go to the top of the page
 
winterolympic
post Jul 17 2017, 01:18 PM
Post#11



Posts: 400
Joined: 26-March 13



Thanks!!
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    23rd July 2017 - 09:43 AM