Can't update records with CurrentUser() control

C

charles.kendricks

I have a form which is used to input customer data. The same form
also has a button whose OnClick event causes the form to be
repopulated using a query that prompts for the desired customer last
name...to look up specific customer records. So far, so good. I
recently was asked to monitor who (what users) input or update which
records. So I created two additional fields in the customer records
UpdatedBy, and UpdateTime. I use the BeforeUpdate event of the form
to assign the user and time to those two controls on the form

Private Sub Form_BeforeUpdate(Cancel As Integer)

UpdatedBy = CurrentUser()
UpdateTime = Now()

End Sub

This seems to work fine, also....However when I click the button to
look up the same record that was just created with the form, first of
all the controls on the form don't display the user or time
information that was saved in the record,...and worst of all when I
edit, or change the customer data, such as updat his phone number, and
try to save the record I get a Run-time error that says "You can't
assign a value to this object" when I hit the 'debug' button the
UpdatedBy = CurrentUser() is highlighted.
 
C

Cheese_whiz

Hi Charles,

What are you doing to 'save' the record before you hit that button? If you
just input a record, or make some changes, those changes aren't 'saved' until
you do something to save them (like navigate to a different record).

You could add this code to the top of the on_click procedure for your button:

Sub YourButton_Click()

If Me.Dirty Then
Me.Dirty = False
End If

'Rest of your on_click code here
End Sub

That also may be the reason you aren't getting the user/time info added.
The 'before_update' event won't fire until you do something to 'update'
(read: save) your added/changed data.

Hope that helps,
CW
 
C

charles.kendricks

I follow the logic of both contributers to my post, and I guess I
should have mentioned that I do close the form after inputting the
data, and I also opened the customer table in table mode to verify
that the new data, including the UpdatedBy, and UpdateTime has been
written successfully to the table. I then re-open the form before
clicking the 'search by name' button.
 
C

Cheese_whiz

Hi Charles,

If the data is in your table, then your problem is related to the display
controls where you should see the information (updatedby and time). Make
sure the control source of the controls that are suppose to display the data
are properly set to the fields in your table or query. If the controls that
are suppose to display that information (updatedby and time) are NOT in the
main form, make sure that the form they're in has IT'S record source properly
set.

Other than that, I'm not sure what to tell you.

Good Luck,
CW
 

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