open a form with conditions (1 Viewer)

scallebe

Registered User.
Local time
Today, 21:05
Joined
Mar 23, 2018
Messages
51
Good evening (for me it is here in Belgium) developers :)

I would like to open a form in 2 ways : as a User or as a Admin.

I have a table with all the users where the Usertype field has a possible selection User or Admin. (from another table)

When the Usertype is "User" the form should go open and show only the records he/she added. When the Usertype is "Admin" the form should go open and show all the records.

I have some code but I already understand it's not that simple.

Code:
Private Sub Form_Load()

If [tabelCorrespondenten.Usertype] = ("Admin") Then

DoCmd.OpenForm "FormPP", acNormal

Else

DoCmd.OpenForm "FormPP", , , "Tarifeerder = GetWinUser()"
DoCmd.GoToRecord , , acNewRec

End If

End Sub

I already find out I have to do some 'Recordset' but there is where it stops for me. :confused:

Is there someone who can help me with this issue?

Thanks

Greetz

Pascal
 

isladogs

MVP / VIP
Local time
Today, 20:05
Joined
Jan 14, 2017
Messages
18,246
Consider using the OpenArgs property to define how the form is opened
 

scallebe

Registered User.
Local time
Today, 21:05
Joined
Mar 23, 2018
Messages
51
isladogs,

Thanks for your reply.

I have some basic in vba and I tried this but it didn't work :confused:

Code:
Private Sub Form_open()

If Me.OpenArgs() = "Admin" Then
DoCmd.OpenForm "FormPP", acNormal

Else

DoCmd.OpenForm "FormPP", , , "Tarifeerder = GetWinUser()"
DoCmd.GoToRecord , , acNewRec

End If

End Sub

Can you help me a little further please :(

Thanks

Greetz

Pascal
 

deletedT

Guest
Local time
Today, 20:05
Joined
Feb 2, 2019
Messages
1,218
Does this works?

Code:
Private Sub Form_open()

If Me.OpenArgs() = "Admin" Then
DoCmd.OpenForm "FormPP", acNormal

Else

DoCmd.OpenForm "FormPP", , , "Tarifeerder = " & GetWinUser()
DoCmd.GoToRecord , , acNewRec

End If

End Sub
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 12:05
Joined
Aug 30, 2003
Messages
36,127
Since the user is presumably a text value, perhaps:

DoCmd.OpenForm "FormPP", , , "Tarifeerder = '" & GetWinUser() & "'"
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:05
Joined
May 7, 2009
Messages
19,247
where are you opening the FormPP? is it from the Login screen or Welcome screen?
 

scallebe

Registered User.
Local time
Today, 21:05
Joined
Mar 23, 2018
Messages
51
Hi everybody, thank you so much for your reply...

Tera's code doesn't work :(
pbaldy : the way you open the form also doesn't work :(


These codes works seperetly perfect:

Shows all records :

Code:
Private Sub Form_load(Cancel As Integer)

DoCmd.OpenForm "FormPP", acNormal
DoCmd.GoToRecord , , acNewRec

End Sub

Shows only the records created by the user (it can be a 'user' or an 'admin') :

Code:
Private Sub Form_load(Cancel As Integer)

DoCmd.OpenForm "FormPP", , , "Tarifeerder = GetWinUser()"
DoCmd.GoToRecord , , acNewRec

End Sub

I think, to combine the 2 ways, access needs to find out first if the user on his/her local machine is a 'user' or an 'admin' witch can be found out in the table 'TabelCorrespondenten' and then open the form one way or the other way.

My research in google learned me that I have to do that with a OpenRecordSet… or a DLOOKUP... True?? (but how I don't know :(:confused:) and then use the if - then - else argument...

Thanks

Greetz

Pascal
 
Last edited:

scallebe

Registered User.
Local time
Today, 21:05
Joined
Mar 23, 2018
Messages
51
arnelgp,

Thanks for your reply.

The form can be used after a login. it's connected on a table 'TabelPP'. It's one of the many forms in my db and opens by a switchboard.

Greetz

Pascal
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:05
Joined
May 7, 2009
Messages
19,247
form Load or Open is not appropriate to put the code.
you should put it after the login is validated.
Code:
If Me.OpenArgs() = "Admin" Then
DoCmd.OpenForm "FormPP", acNormal

Else

DoCmd.OpenForm "FormPP", , , "Tarifeerder = '" & GetWinUser() & "'"
docmd.GoToRecord acDataForm,"FormPP",acNewRec

End If

add also code to the BeforeInsert Event of "FormPP"
Code:
Private Sub Form_BeforeInsert(Cancel As Integer)
Me!Tarifeerder  = GetWinUser()
End Sub
 

isladogs

MVP / VIP
Local time
Today, 20:05
Joined
Jan 14, 2017
Messages
18,246
As I said originally, if using OpenArgs, you need to define what the OpenArgs means before opening the form.
My suggestion is that it is based on UserType.
So I would create a function GetUserType based on user login details

Code:
Function GetUserType()
GetUserType = DLookup("UserType", "TabelCorrespondten", use filter based on login)
End Function

The filter criteria would check the user login on the login form. Is that the Tarifeeerder and GetWinUser check?

Next in your calling form, use that in the OpenArgs
Code:
If GetUserType="Admin" Then
  DoCmd.OpenForm "FormPP", , , , , ,GetUserType
Else
  DoCmd.OpenForm "FormPP", , , "Tarifeerder = '" & GetWinUser & "'", , ,GetUserType
End If

The above assumes that Tarifeerder is a text field

Finally in FormPP itself, do something like this in Form_Load

Code:
If Not Me.OpenArgs="Admin" Then DoCmd.GoToRecord , , acNewRec
 
Last edited:

scallebe

Registered User.
Local time
Today, 21:05
Joined
Mar 23, 2018
Messages
51
arnelgp,

Thanks for reply.

The form doesn't have to open after Login. After Login, the Switboard forms open. FormPP is just one of many…
I think your solution is not an option here... :eek: forgive me if I'm wrong...

isladogs,

Also thanks for reply.

I got this one so far :

Code:
Function GetUserType()
GetUserType = DLookup("UserType", "TabelCorrespondenten", "UserLogin = GetWinUser()")
End Function

and

Code:
Private Sub Form_Load()
If Not Me.OpenArgs="Admin" Then DoCmd.GoToRecord , , acNewRec
End Sub

Is this correct?

Where do I put this code :eek:

Code:
If GetUserType="Admin" Then
  DoCmd.OpenForm "FormPP", , , , , ,GetUserType
Else
  DoCmd.OpenForm "FormPP", , , "Tarifeerder = '" & GetWinUser & "'", , ,GetUserType
End If

To all the developers :

I'm a VBA student... (beginner) I try to understand your solutions and try to figger out what they do. Sometimes I can follow but sometimes :banghead::banghead:

So thank you all for your help. Thank god there Forums like this…

Greetz

Pascal
 

scallebe

Registered User.
Local time
Today, 21:05
Joined
Mar 23, 2018
Messages
51
arnelgp,
FormPP doesn't have to open after Login. After Login a Switchboard Items Form opens. FormPP is just one of the forms in my DB. Also The Login & Password Form is not connected to a table, so the input is not saved.

I can not put your solution in the Login Form If that's what you mean. I think… :(

isladogs,

This is what I gor so far :

Code:
Function GetUserType()
GetUserType = DLookup("UserType", "TabelCorrespondenten", "UserLogin = GetWinUser()")
End Function
and
Code:
Private Sub Form_Load()
If Not Me.OpenArgs="Admin" Then DoCmd.GoToRecord , , acNewRec
End Sub
Correct so far? :(

Where do I put the "If GetUserType…. code"?

I want to thank all of your for your help here, I'm just a VBA student (beginner :eek:) and I try to learn form this. Thank God there are Forums like this

Greetz

Pascal
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 12:05
Joined
Aug 30, 2003
Messages
36,127
Post 12 was moderated, I'm posting to trigger email notifications.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:05
Joined
Feb 19, 2002
Messages
43,331
Typically, when a user logs in, I store certain security info about him in the login form and just hide the form when I open the switchboard so the form is always available for reference throughout the app. Depending on someone's security settings, I may allow add/change/delete but that check is done at the point where a user tries to take the action. So, in the current event, if this is a new record, I check to see if the user is allowed to create new records. If he isn't, I undo the change, cancel the event and present a message.

In your case, if the only difference is what records a person can see, modify the query.

Select ...
From ..
Where IIf(Forms!frmLogin!UserType = "Admin", True, IIf(CreatedBy = Forms!frmLogin!UserID, True, False) = True
 

isladogs

MVP / VIP
Local time
Today, 20:05
Joined
Jan 14, 2017
Messages
18,246
Assuming user login is a text string you need text delimiters
Place the function code in a standard module

Code:
Function GetUserType()
GetUserType = DLookup("UserType", "TabelCorrespondenten", "UserLogin = '" & GetWinUser & "'")
End Function

The If...Else...End If code to open FormPP needs to go on the appropriate button click event in your login form
 

scallebe

Registered User.
Local time
Today, 21:05
Joined
Mar 23, 2018
Messages
51
Hi everybody,

I don't know what happend with post #12. I clicked submit reply and then something strange happend and brought me back to the loginform. Back to the thread I couldn't find my reply so I started again… :confused:

After Pat Hartman's reply I started thinking and came up with the following solution :

First I set my FormLogin "vissible = False" instead of closing it.
I also find out in de FormLogin that there was a code to lookup the UserType (for later use after login). So I used that code in my FormPP :
Code:
Private Sub Form_Load()
Dim Userlevel As Integer
    Userlevel = DLookup("UserType", "TabelCorrespondenten", "UserLogin = '" & Forms!FormLogin.txtUserName.Value & "'")
        If Userlevel = 1 Then ' for admin
        DoCmd.OpenForm "FormPP", acNormal
        Else
        DoCmd.OpenForm "FormPP", , , "Tarifeerder = GetWinUser()"
        DoCmd.GoToRecord , , acNewRec
        End If
End Sub

So far it works for me.

Later I will look into the "OpenArg" solution. It's a new itme I have to learn.

Thank you all so much for the proposed solutions. I'm sure I will come back later for this Thread or another one. :);)

Greetz

Pascal
 

Users who are viewing this thread

Top Bottom