Conditional Text on a Command Button

  • Thread starter Thread starter Amy E. Baggott
  • Start date Start date
A

Amy E. Baggott

Is it possible to have conditional text on a command button? I want to put a
button on my order form that says "Cancel Order" if the order is not
cancelled, but says "Uncancel Order" if the order is cancelled (we have
exhibitors who drop out of the show and then come back).
 
Amy E. Baggott said:
Is it possible to have conditional text on a command button? I want to
put a
button on my order form that says "Cancel Order" if the order is not
cancelled, but says "Uncancel Order" if the order is cancelled (we have
exhibitors who drop out of the show and then come back).


Use the form's Current event to check the order's status and change the
Caption property of the command button accordingly. Also do the same thing
when/wherever you change the status of the order. So if, for example, your
command button is called cmdCancel, you might have code along these lines:

Private Sub cmdCancel_Click()

If Me.OrderStatus = "Cancelled" Then
Me.OrderStatus = "Active"
Me.cmdCancel.Caption = "Cancel Order"
Else
Me.OrderStatus = "Cancelled"
Me.cmdCancel.Caption = "Uncancel Order"
End If

End Sub
 
I knew there had to be a simpler way than putting two buttons on top of each
other! Thanks.

You wouldn't by any chance know if there's a way to limit who can actually
click that button, would you? The only way I can think of is to use an input
box with a password, but I'd much rather set it up on a user-level basis.
 
Amy E. Baggott said:
I knew there had to be a simpler way than putting two buttons on top of
each
other! Thanks.

You wouldn't by any chance know if there's a way to limit who can actually
click that button, would you? The only way I can think of is to use an
input
box with a password, but I'd much rather set it up on a user-level basis.



Depends; how are you defining your users? If your database is in the
..mdb/.mde format, with user-level security applied, you can use the
CurrentUser() function to find out who the user is or what user-group they
belong to, and restrict access based on that. Or you could get the Windows
user name (see http://www.mvps.org/access/api/api0008.htm) and restrict
access based on that. Or you could have your own home-grown user-login
function.
 
My database is an .mdb and I am using an Access Workgroup with the user
security and permissions set up through the wizard in Access.
 
Amy E. Baggott said:
My database is an .mdb and I am using an Access Workgroup with the user
security and permissions set up through the wizard in Access.


Then you can either check the value of CurrentUser() and enable/disable (or
show/hide) the button based on the user, like this:

'----- start of example code -----
Private Sub Form_Open(Cancel As Integer)

Dim blnCancellingAllowed As Boolean

Select Case CurrentUser()
Case "Amy Baggott"
blnCancellingAllowed = True
Case "Amy's Close Friend"
blnCancellingAllowed = True
Case Else
blnCancellingAllowed = False
End Select

Me.cmdCancel.Enabled = blnCancellingAllowed
' ** or ** Me.cmdCancel.Visible = blnCancellingAllowed

End Sub
'----- end of example code -----

Of course, you may want to store the authorized users in a table and use a
DLookup to find out if CurrentUser() is authorized, instead of hard-coding
the values. You'd need to user ULS to protect that table against
unauthorized changes.

*** OR ***

You may have your users classified into groups, in which case you can check
whether the current user is in a group that is authorized to cancel. For
that you might use this function:

'----- start of function code -----
Function fncUserIsInGroup(GroupName As String) As Boolean

' Returns True if the current user is a member of the specified
' security group; False if not, or if the group doesn't exist, or
' if an error occurs reading the groups.

Dim ws As Workspace

Set ws = DBEngine.Workspaces(0)

On Error Resume Next
fncUserIsInGroup = _
(ws.Users(CurrentUser).Groups(GroupName).Name = GroupName)

Set ws = Nothing

End Function

'----- end of function code -----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top