Label Update After Button Clicked

G

Guest

I would like to know if there is any problem with the below syntax. it seem
not able to update the Label that I created. Basically, I want the forms to
update the Label(UpdatedDate) field to the current date when I click on "Back
To Main" Button.

Private Sub BMain_Click()
On Error GoTo Err_BMain_Click

Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "Switchboard"
DoCmd.SetWarnings False
Me!UpdateDate.Caption = "Last Updated On " & Format(Now, "ddd, mmm d
yyyy, hh:mm:ss AMPM")
DoCmd.Save acForm, "Import TimeCard Forms"
DoCmd.Close
DoCmd.OpenForm stDocName, , , stLinkCriteria


Exit_BMain_Click:
Exit Sub

Err_BMain_Click:
MsgBox Err.Description
Resume Exit_BMain_Click

End Sub
 
B

Brendan Reynolds

The code will change the caption of the label, but it then closes the form,
so you'll never see the changed caption.

If you were expecting the change to the caption to be saved, that's not
going to work. The form has to be open in design view for design changes to
be saved. If you want to do something like this without opening the form in
design view, you need to store the new value somewhere, then apply it in the
Open event. You could save the value in a table, but I like to use the
AccessObjectProperties collection for this kind of thing. Something like ...

Private Sub Form_Open(Cancel As Integer)

Dim aop As AccessObjectProperty
For Each aop In CurrentProject.AllForms(Me.Name).Properties
If aop.Name = "LastClosed" Then
Me.Label0.Caption = CStr(aop.Value)
Exit For
End If
Next aop

End Sub

Private Sub Form_Unload(Cancel As Integer)

CurrentProject.AllForms(Me.Name).Properties.Add "LastClosed", Now()

End Sub
 
B

Brendan Reynolds

BTW: The documentation says that adding an AccessObjectProperty with the
same name as an existing member of the collection, as in my Form_Unload
example below, will raise an error. The documentation is wrong - the
property is silently over-written with the new value. Strictly speaking,
perhaps we should be checking to see whether the object already exists, in
case Microsoft decide that this is a bug and should be fixed in some future
version. However, as it has been this way now since Access 2000, I think it
is very unlikely that the behaviour will be changed. Hopefully, one day the
documentation may be changed to reflect the actual behaviour. In fact, I
think I'll go remind someone about that right now ...
 
G

Guest

This works. Thanks.
--
Seikyo


Brendan Reynolds said:
The code will change the caption of the label, but it then closes the form,
so you'll never see the changed caption.

If you were expecting the change to the caption to be saved, that's not
going to work. The form has to be open in design view for design changes to
be saved. If you want to do something like this without opening the form in
design view, you need to store the new value somewhere, then apply it in the
Open event. You could save the value in a table, but I like to use the
AccessObjectProperties collection for this kind of thing. Something like ...

Private Sub Form_Open(Cancel As Integer)

Dim aop As AccessObjectProperty
For Each aop In CurrentProject.AllForms(Me.Name).Properties
If aop.Name = "LastClosed" Then
Me.Label0.Caption = CStr(aop.Value)
Exit For
End If
Next aop

End Sub

Private Sub Form_Unload(Cancel As Integer)

CurrentProject.AllForms(Me.Name).Properties.Add "LastClosed", Now()

End Sub
 

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