Hide cmdButtons based on Variable?

CharlesWh

Registered User.
Local time
Today, 20:34
Joined
Jan 19, 2006
Messages
36
In my Db a user belongs to a company, a company can belong to various PrimaryDataTypes.

TblPrimaryData
TblPrimaryDataTypes
TblLinkPrimaryData2Types

The last one is basically a many to many relationship link table which stores the PrimaryDataID along with the associated Type

On my splashscreen I have various command buttons but I only want certain types to be able to see certain buttons.

I'm thinking I could have a module where the types are stored when a user logs on but am not sure how to go about it?

Any advice welcomed.:confused:
 
This sounds that you need a conditional statement on the Open/Load event of your Splash Screen.

Given you probably have several types; use the Select Case method

e.g.

Select Case myVar


Case = "type 1"

cmd.button1.visible - true
cmdotherbutton.visible = false

Case = "type 2"

cmd.button1.visible - true
cmdotherbutton.visible = false

'etc

'Finish with

Case Else

msgbox "Programme Error",vbcritical,"Contact Administrator"

end select
 
Charles,

Start with a query, that I think will include your TblPrimaryData and TblLinkPrimaryData2Types tables, with a criteria based on the current user, which will then give you a list of the applicable Types for that user's company.

You will then be able to refer to that query in your code, in order to toggle the Enabled or Visible properties of the buttons on your form.

There are a number of options as to how to go about building the code. If you need more explicit help with this, please post back with some more details, with data-based examples, of what you really want to happen.
 
First, whatever you do should be in the OnLoad event because the form isn't fully available in the OnOpen event. Second, you can split the work if you want.

The way I do it is I look at the "Username" Environment variable to see who this user is. Because we use domain-based, high-security logins, that is pretty reliable. So I do that in the OnOpen event where I have Cancel available if my DLookup or recordset operation tells me the person shouldn't be there. I put the results in the form's general declaration area. The OnLoad event follows OnOpen if you didn't cancel the open. That is where I put code that plays with button visibility and enablement. (Gotta remember to disable a button when you are going to hide it.)

It is pretty easy, really. The trick is to decide what buttons user X can see. Once you know the user's role, the rest is actually pretty easy.
 
I've Created A Module Called "Globals" I've put the following code in the module

Code:
Option Compare Database

Public Function Init_Globals()
GBl_student_ID = 0
End Function
Public Function Get_Global()
Get_Global = GBl_student_ID
End Function

On the Close Event of my login form I have the following code

Code:
Private Sub Form_Close()
GBl_student_ID = Me.txtDbUserID
End Sub

And on the OnLoad event of my splashscreen I have the following code

Code:
Private Sub Form_Load()
Init_Globals
End Sub

Then in my OnOpen Event (To test my code) I have the following which just comes back with DOH!!! So I know it doesnt work -
I just dont know why? :confused:

Code:
Private Sub Form_Open(Cancel As Integer)
If GBl_student_ID = 3 Then
MsgBox "SUCCESS"
Else
MsgBox "DOH!!!"
End If
 
To get the user ID, I call Environ("Username"), which requires that you have a secure login system that makes that name available as an environment variable. Domain-based logins USUALLY have this. (Not guaranteed.)

In your example, your "get" function gets something that APPEARS to be TEXT. Your comparison that leads to "DOH!" on the other hand is clearly based on an INTEGER user ID. Mixed-mode for sure.
 
I have found a solution using Tempvars. This apparantly remembers the value whilst the database is open and seems to be useful too by saving have to create modules and functions. This is a solution new in Access 2007.
 
Maybe this would enlighten CharlesWh and CharlesWhiteman (both from England). Hmmm...

http://www.blueclaw-db.com/access_database_global_variable.htm

TempVar has its limitations though. If you don't want to be restricted to numeric or string return types then use a global variable and declare it as any of the supported data types (or your own custom type).
 

Users who are viewing this thread

Back
Top Bottom