Go Back   Access World Forums > Microsoft Access Discussion > Modules & VBA

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 04-15-2019, 06:16 AM   #1
ppierce
Newly Registered User
 
Join Date: Nov 2009
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
ppierce is on a distinguished road
Datetime is not evaluated on form properly

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?

ppierce is offline   Reply With Quote
Old 04-15-2019, 06:27 AM   #2
plog
AWF VIP
 
Join Date: May 2011
Posts: 9,347
Thanks: 10
Thanked 2,270 Times in 2,222 Posts
plog is a jewel in the rough plog is a jewel in the rough plog is a jewel in the rough
Re: Datetime is not evaluated on form properly

Not enough code. Please show where ReceivedDateTime gets set with a value.
plog is offline   Reply With Quote
Old 04-15-2019, 06:29 AM   #3
ppierce
Newly Registered User
 
Join Date: Nov 2009
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
ppierce is on a distinguished road
Re: Datetime is not evaluated on form properly

Here is the code that sets the value of the DateTime
Code:
Private Sub ReceivedDateandTime_Click()
ReceivedDateandTime.Text = Now
End Sub

ppierce is offline   Reply With Quote
Old 04-15-2019, 06:35 AM   #4
plog
AWF VIP
 
Join Date: May 2011
Posts: 9,347
Thanks: 10
Thanked 2,270 Times in 2,222 Posts
plog is a jewel in the rough plog is a jewel in the rough plog is a jewel in the rough
Re: Datetime is not evaluated on form properly

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.
plog is offline   Reply With Quote
Old 04-15-2019, 06:45 AM   #5
ppierce
Newly Registered User
 
Join Date: Nov 2009
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
ppierce is on a distinguished road
Re: Datetime is not evaluated on form properly

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
ppierce is offline   Reply With Quote
Old 04-15-2019, 07:03 AM   #6
missinglinq
AWF VIP
 
missinglinq's Avatar
 
Join Date: Jun 2003
Location: Richmond (Virginia that is!)
Posts: 6,297
Thanks: 11
Thanked 716 Times in 666 Posts
missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light
Re: Datetime is not evaluated on form properly

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)>
__________________
The Devil's in the Details!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


(All code solutions tested in Access 2003/2007, before posting, unless otherwise noted.)
missinglinq is offline   Reply With Quote
Old 04-15-2019, 07:14 AM   #7
plog
AWF VIP
 
Join Date: May 2011
Posts: 9,347
Thanks: 10
Thanked 2,270 Times in 2,222 Posts
plog is a jewel in the rough plog is a jewel in the rough plog is a jewel in the rough
Re: Datetime is not evaluated on form properly

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.

plog is offline   Reply With Quote
Old 04-15-2019, 08:53 AM   #8
ppierce
Newly Registered User
 
Join Date: Nov 2009
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
ppierce is on a distinguished road
Re: Datetime is not evaluated on form properly

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
ppierce is offline   Reply With Quote
Old 04-15-2019, 04:30 PM   #9
missinglinq
AWF VIP
 
missinglinq's Avatar
 
Join Date: Jun 2003
Location: Richmond (Virginia that is!)
Posts: 6,297
Thanks: 11
Thanked 716 Times in 666 Posts
missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light
Re: Datetime is not evaluated on form properly

Glad we could help!

Linq ;0)>

__________________
The Devil's in the Details!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


(All code solutions tested in Access 2003/2007, before posting, unless otherwise noted.)
missinglinq is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Too Complex to be evaluated vbbbmom Queries 1 12-02-2014 11:43 AM
datetime - data mismatch using datetime.now bbxrider General 3 09-29-2009 11:38 PM
VBA constant in sql is it only evaluated once? petebob796 Queries 4 08-14-2007 12:25 AM
expression is too complex to be evaluated, I need help! authorsami Queries 3 11-03-2003 06:59 AM
Events - What is evaluated first. Mark Liddle Forms 5 06-23-2001 01:36 AM




All times are GMT -8. The time now is 06:18 AM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World