code to enable/disable buttons on form based on info in table

H

hastit

I am very new to code, and need to make a form uneditable to users who

are not the author of the record, as well as make uneditable to anyone
once a "completed" button is pushed which sets the date into a table.
Any help would be greatly appreciated.
 
G

Guest

If I am understanding correctly that you want the form to prohibit editing
once a date has been put in a table, there are a couple of things you will
need to do. Note that in the example below, I have made up names of controls
and fields. You will have to modify the code to use your own.

Assuming that clicking the Completed button enters the current data in a
field, you will need this in the Click event of the Completed button:

Me.txtDateCompleted = Date
Me.AllowEdits = False

Also, to make sure that existing records with the completed date can't be
edited:

If Me.NewRecord Then
Me.AllowEdits = True
Else
Me.AllowEdits = IsNull(Me.txtDateCompleted)
End If
 
R

Rick Brandt

I am very new to code, and need to make a form uneditable to users
who

are not the author of the record, as well as make uneditable to anyone
once a "completed" button is pushed which sets the date into a table.
Any help would be greatly appreciated.

Do you have a function which identifies the current user? Do you store this
as part of the record when records are created? If so, then you use the
forms Current event which runs as you arrive at each record.

(all field names and functions are EXAMPLES)

If Me!CompletedField = True _
Or Me!CreatedByField <> CurrentUserFunction() Then
'code to lock record
Else
'code to unlock record
End If

Now, what about that locking/unlocking code? The easiest is just to use
AllowEdits...

If Me!CompletedField = True _
Or Me!CreatedByField <> CurrentUserFunction() Then
Me.AllowEdits = False
Else
Me.AllowEdits = True
End If

One downside of this is that it locks everything on the form, not just the
data-bound controls. If you had for example an unbound ComboBox used for
navigating then it would also be locked and not usable. If you have no such
controls then AllowEdits is the way to go.

If AllowEdits is a problem, then I would loop through all the controls and
lock the desired ones. I usually identify these by giving them all a common
Tag property entry...

If Me!CompletedField = True _
Or Me!CreatedByField <> CurrentUserFunction() Then
RecordLock(True)
Else
RecordLock(False)
End If

Function RecordLock(LockUnlock as Boolean)
Dim cnt as Control
For Each cnt in Me
If cnt.Tag = "MyTagString" Then cnt.Locked = LockUnlock
Next cnt
End Function
 
H

hastit

Yes, I have an "Author" field which pulls the users windows user id. I
also have a list of "super users" who are the admin on this DB.
The "mark complete" button pulls date end enters it into the master
table, but does not show it on the form.
This is my first access database as well as my first time playing with
any 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

Top