SetValue and Now() failing (1 Viewer)

jdraw

Super Moderator
Staff member
Local time
Today, 11:20
Joined
Jan 23, 2006
Messages
14,319
??? I see an End With without a With in one piece of code.
And a With CodeContextObject without a matching End With.

Within functions and subs, if you use a With you must use a matching End With, all within the function or sub.
 

Minty

AWF VIP
Local time
Today, 16:20
Joined
Jul 26, 2013
Messages
9,288
By definition a function normally returns a result. You would normally declare what that result should be, so I would have thought your function would be more like;

Code:
Public Function DateTimeIn() as Date

    DateTimeOut = Now()

End Function

I would also check that you haven't inadvertently saved another function or sub somewhere with the same name, as that would probably cause the failure with the more normal usage method.

Also you need to assign the functions value to something at the other end - so
Code:
Public Sub btnLoginPaperwork_Click()
   [COLOR="Red"] Me.YourDateTimeInControl = DateTimeIn()[/COLOR]
    'Timestamps DateTimeIn Field
    Beep
End With
    MsgBox "Repack/Relabel Paperwork Sucessfully Logged In", vbOKOnly, "Thank You!"
    'Popup to confirm data entry to Operator
    DoCmd.Close
Change the control name to match your form.
 

GLese

Registered User.
Local time
Today, 11:20
Joined
Feb 13, 2018
Messages
52
??? I see an End With without a With in one piece of code.
And a With CodeContextObject without a matching End With.

Within functions and subs, if you use a With you must use a matching End With, all within the function or sub.

Right after I posted that code, I corrected it in VB and now I am getting a compile error. So allow me to post the code correctly below.

modDateTimeIn:
Code:
Public Function DateTimeIn() As String
DateTimeIn = Now()
End Function

Code from the Form I am using which runs (according to the popup message box) but does not add a new record to the bound table:
Code:
Public Sub btnLoginPaperwork_Click()
    modDateTimeIn.DateTimeIn
    'Timestamps DateTimeIn Field
    Beep
    MsgBox "Repack/Relabel Paperwork Sucessfully Logged In", vbOKOnly, "Thank You!"
    'Popup to confirm data entry to Operator
    DoCmd.Close
    
End Sub
 

Minty

AWF VIP
Local time
Today, 16:20
Joined
Jul 26, 2013
Messages
9,288
As I mentioned in post #22 you aren't doing anything with your function result, which is probably why you are getting a compile error.

DateTimeIn returns a date. You aren't using it to do anything with that date.
In order to set a value somewhere on your form you need to assign that value to something.

In your forms code try this;

Code:
Dim sFlibble as String

sFlibble = modDateTimeIn.DateTimeIn [COLOR="Green"] 'This assigns the result of your function to a string variable[/COLOR]

Msgbox sFlibble
 

GLese

Registered User.
Local time
Today, 11:20
Joined
Feb 13, 2018
Messages
52
As I mentioned in post #22 you aren't doing anything with your function result, which is probably why you are getting a compile error.

DateTimeIn returns a date. You aren't using it to do anything with that date.
QUOTE]

Minty, After seeing some advice in another thread about avoiding the Now() expression in favor of Date() and Time(), I switched my tables/forms around to have a DateIn and TimeIn column, (As well as out columns). With that change, I had to change my code around. And since the code is simple enough, I have ditched the module for timestamping, and have put in two straightforward lines of code in there as shown below.

Code:
Private Sub btnLoginPaperwork_Click()
    Me.DateIn = Date
    Me.TimeIn = Time()
    'Timestamps DateIn mm/dd/yyyy, and TimeIn column hhmm (24hr)
    MsgBox "Paperwork Sucessfully Logged In", vbOKOnly, "Thank You!"
    Beep
    CloseWind
End Sub

This code works fantastic for my login form where a single record is being saved. Also works well for logging a single record out.

I experimented with using it for logging multiple records out at the same time in a continuous form, but found that it will only log out the last record in the continuous form which gets edited. Nothing else. So I'm back to the drawing board there. I'm working on developing a set of forms which will work together as subforms on a navigation pane.

All that being said I'm going to consider this thread closed out since we've deviated so far from the original issue and question of mine. But if you have any advice or direction on my multi-record timestamp I'd be glad to hear it! (And any advice on the code above if you see any glaring red flags)
 

Minty

AWF VIP
Local time
Today, 16:20
Joined
Jul 26, 2013
Messages
9,288
As you will find with many things in access - there are many ways to skin a cat.

To update multiple records you can
a) Use an Update query based on some criteria found on your form.
b) Use code to loop through the current recordset and set the value per record.
c) Use code to loop through the forms records then use the current form control and set the value on the form.

a) Would be the correct and most efficient method (IMHO).
b) Would work but is more clunky.
c) Is not normally very practical, but I've seen it done.

I would open a new thread with your requirement if you get stuck for advice.
 

GLese

Registered User.
Local time
Today, 11:20
Joined
Feb 13, 2018
Messages
52
As you will find with many things in access - there are many ways to skin a cat.

To update multiple records you can
a) Use an Update query based on some criteria found on your form.
b) Use code to loop through the current recordset and set the value per record.
c) Use code to loop through the forms records then use the current form control and set the value on the form.

a) Would be the correct and most efficient method (IMHO).
b) Would work but is more clunky.
c) Is not normally very practical, but I've seen it done.

I would open a new thread with your requirement if you get stuck for advice.


Thanks Minty, I plan on doing that once I get to my next stuck point :D
 

Users who are viewing this thread

Top Bottom