Enable Buttons Depending on AccessID

Steve_T

Registered User.
Local time
Today, 16:44
Joined
Feb 6, 2008
Messages
96
Hello,
I have created a Database and are assigning Users to it, the easiest way i see to do this is set them a AccessID (Admin = ID1, User =ID2, Temp =ID3, Guest = ID4)

I have 4 buttons and depending on what ID the user has will bepend on whether they can press the buttton as it will not be enabled.
I have tried to write the code but i am either over my head or been working on nights for too long. i have attached my code but in short i will also list the Buttons below and alond side them i will list with ID can access them.

CmdAdmin 1
Command66 1 & 2
Command70 1 & 2 & 3
Command72 1 & 2 & 4

I would be very greatful if someone could point me in the right direction
 

Attachments

For starters, you open 4 If statements but you only have 1 End If, so presumably you're getting a compile error. Personally with that many options I'd use a Select/Case structure. You're not actually using the With statements, which won't cause an error but is, well, sloppy. If that doesn't sort it, what error are you getting, or what behavior?
 
Thanks for your reply i have not used End If because i am using End With. Or have i made an error. I have had no experience with Case statments and wanted to use them as they are neater, i have searched the forum but all i seem to find i comments saying "Search Forum".
 
Yes, you've made an error. Each With must be completed by an End With, each If must be completed by an End If. The Select Case structure, though it is also covered in VBA help:

Code:
  Select Case YourValue
    Case 1
      'what to do for 1
    Case 2
      'what to do for 2
    Case 3
      'what to do for 3
    Case Else
      'what to do if no other condition met
  End Select
 
Just to go along with what Paul said and kind of give you a mini tutorial -

When you use an IF...Then...Else statement you need to start it with IF and then it must have an End IF at the end. If you want to check for more than two conditions you would need to use an ElseIf instead of just Else. You can only have one Else between any IF / End IF statement so for additional conditions you use ElseIf.

However, that is very messy and not easy to read, so Select Case Statements are better. For example you start with
Code:
Select Case [WhateverThatHasAValueYouWantToCheck]
so, if you have a text box named AccessID then you would use

Code:
Select Case Me.AccessID

or if on a different form:
Code:
Select Case Forms!YourFormName.AccessID

Then you add the "cases" to the statement:
Code:
Select Case Me.AccessID
Case 1
   ' Enter the code here if AccessID equals 1
Case 2
   ' Enter the code here if AccessID equals 2
Case 3
   ' Enter the code here if AccessID equals 3
Case Else
   ' Enter the code here if AccessID equals anything other than 1, 2, or 3.
End Select
You always end a select case statement with an End Select line.

Then, if you have multiple values that can get the same result you can combine them:
Code:
Select Case Me.AccessID
Case 1, 3
   ' Enter the code here if AccessID equals 1 or 3
Case 2, 4
   ' Enter the code here if AccessID equals 2 or 4
Case 3 to 20
   ' Enter the code here if AccessID equals 3, or any value up to 20
Case Else
   ' Enter the code here if AccessID equals anything other than the values above.
End Select

I hope that helps.
 

Users who are viewing this thread

Back
Top Bottom