Datetime is not evaluated on form properly (1 Viewer)

ppierce

Registered User.
Local time
Today, 09:03
Joined
Nov 18, 2009
Messages
21
I have a form with a text box I use to capture a datetime value on a button click.

In the code behind I check to make sure a user has entered a date time value using this code:
Code:
ElseIf Trim(ReceivedDateTime & "") = "" Then
    MsgBox "Call Received Date/Time Is Required"

If the user forgets to enter a date time the msgBox displays as expected. However when the user then enters the dat time in the text box and clicks the button again the error message still is shown.

Why is the button click not seeing the data that has now been entered in the form?
 

plog

Banishment Pending
Local time
Today, 08:03
Joined
May 11, 2011
Messages
11,613
Not enough code. Please show where ReceivedDateTime gets set with a value.
 

ppierce

Registered User.
Local time
Today, 09:03
Joined
Nov 18, 2009
Messages
21
Here is the code that sets the value of the DateTime
Code:
Private Sub ReceivedDateandTime_Click()
ReceivedDateandTime.Text = Now
End Sub
 

plog

Banishment Pending
Local time
Today, 08:03
Joined
May 11, 2011
Messages
11,613
Probably still not enough code. Are you declaring ReceivedDateandTime outside of a function? My guess is your issue is scope--not knowing where and how variables exist.

You've yet to post code that references any form element--a button nor an input.
 

ppierce

Registered User.
Local time
Today, 09:03
Joined
Nov 18, 2009
Messages
21
Hmm not sure what you mean by code that does reference any form element. The onclick button checks the value of 4 form elements. The Call Location text box, the ComboType, Beat textbox and ReceivedDate and time these 4 are evaluated for a null value.

If the location, type or beat are null the message is shown. If the user places a value in those 3 form elements and clicks the button then the code runs as expected.

It is only the textbox for the ReceivedDatetime that is not being evaluated properly on the 2nd click?

I have included the entire code block for that button click.
Code:
Private Sub BtnPrint1_Click()
If IsNull(Location) Then
    MsgBox "Call Location Is Required"
    ElseIf IsNull(ComboType) Then
    MsgBox "Call Type Is Required"
    ElseIf IsNull(Beat) Then
    MsgBox "Call Beat Is Required"
    ElseIf Trim(ReceivedDateTime & "") = "" Then
    MsgBox "Call Received Date/Time Is Required"
Else
Call PrintReport
DoCmd.GoToRecord , , acNewRec
'Me.Requery
txtCodeThree.Visible = True
Forms!CallForm2!txtCodeThree.SetFocus
txtCodeThree.Text = ""
Location.SetFocus
End If

End Sub
 

missinglinq

AWF VIP
Local time
Today, 09:03
Joined
Jun 20, 2003
Messages
6,423
Well, you call the errant Control

ReceivedDateTime

in one piece of code and

ReceivedDateandTime

when you're assigning a value to it.

ReceivedDateandTime.Text = Now

will only work if the control has Focus when you try to assign the Value. Normally you'd only use

ReceivedDateandTime.Value = Now

or simply

ReceivedDateandTime = Now

since Value is its Default property.

If you do that assignment and then click the button to check it...that entered Value hasn't been Saved yet.

Getting these things fixed...anytime you go to print a New Record, you need to first Save that Record ...as simple as

If Me.Dirty Then Me.Dirty = False

Linq ;0)>
 

plog

Banishment Pending
Local time
Today, 08:03
Joined
May 11, 2011
Messages
11,613
Things inside your function don't know about the world outside it, unless you explicitly tell it about the world:

Code:
Private Sub BtnPrint1_Click()
If IsNull(Location) Then

With what you have given us Location will always be null. It is a variable that has not been set inside BtnPrint1_Click. The first time you reference it is when you check it for a null value, so naturally it is null every time.

Now, it is possible Location is a global variable declared inside code area but outside of any function. But you haven't provided that code to let us know. Location is not a form element. Because for it to be a form element you must explicitly tell the computer that by using the correct notation (Forms!CallForm2!Location).

I believe this is also what you've done in the code in your initial post. You haven't referenced data properly.
 

ppierce

Registered User.
Local time
Today, 09:03
Joined
Nov 18, 2009
Messages
21
You are correct the devil is in the details. Fixed the issue with the wrong control name and added the (Forms!CallForm2!Location) to the code. All is working now

Thank you gentlemen for your time
 

Users who are viewing this thread

Top Bottom