Exit event to change event (1 Viewer)

aman

Registered User.
Local time
Today, 06:10
Joined
Oct 16, 2008
Messages
1,250
Hi All

I have written the following code which works fine but I need to press Tab after type end date in the textbox to make it work.

Can I change it into so that it automatically captures if the end date has been typed in textbox named txtEndDate and then runs the code ? I think it needs to be in change event but how to check if the date has been typed in or not.
Code:
Private Sub txtEndDate_Exit(Cancel As Integer)
If IsNull(Me.txtEndDate) Then
MsgBox "Please make sure you have entered StartDate and End Date", vbCritical + vbOKOnly, "Can't leave Dates blank"
Cancel = True
Else
Me.FraOption.Visible = True
Me.OptPrintAll.Visible = True
Me.optPrintSel.Visible = True
'Me.lblPrintAll.Visible = True
'Me.lblPrintSel.Visible = True
Me.lblPrintFilter.Visible = True
End If
End Sub

Thanks
 

CJ_London

Super Moderator
Staff member
Local time
Today, 14:10
Joined
Feb 19, 2013
Messages
16,610
you reference the .text property in the change event. In your code, you will need validation after each change that a complete date has been entered - see the isDate function. Note that it might return a partial date as a valid date so you may need additional validation
 

aman

Registered User.
Local time
Today, 06:10
Joined
Oct 16, 2008
Messages
1,250
Thanks CJ_London, Can you please help with the code? I tried few things but doesn't seem to be working :(
 

BeeJayEff

Registered User.
Local time
Today, 06:10
Joined
Sep 10, 2013
Messages
198
So you want the system to check after each key press whether it is yet a valid date, rather than when the user leaves the control - is that right ?
 

aman

Registered User.
Local time
Today, 06:10
Joined
Oct 16, 2008
Messages
1,250
after the date has been typed in and validated , automatically make the following controls visible without pressing any enter or tab key.
Code:
Me.FraOption.Visible = True
Me.OptPrintAll.Visible = True
Me.optPrintSel.Visible = True
'Me.lblPrintAll.Visible = True
'Me.lblPrintSel.Visible = True
Me.lblPrintFilter.Visible = True
 

BeeJayEff

Registered User.
Local time
Today, 06:10
Joined
Sep 10, 2013
Messages
198
That doesn't answer my question. If the user types the following keystrokes : 0,6,1,4,2,0,1,7 - do you want the system to check whether a valid date has been entered after each keystroke, or only after the user presses another key such as tab or enter (or moves the mouse/cursor to another field) ?

By the way, I try to avoid having users enter dates explicitly as on my side of the pond, much confusion can be caused by the American way of expressing dates - which is what Access uses. I always provide the user with a date picker to minimise such issues.
 

Cronk

Registered User.
Local time
Today, 23:10
Joined
Jul 4, 2013
Messages
2,771
aman,

CJ gave you the answer ie use the change event, something like
Code:
private sub txtEndDate_change()
   if len(me.txtEndDate.text & "") = 10 then
      If isdate(me.txtEndDate.yext) then
         <put your code in here>
         me.<next control>.setfocus
      endif
  endif
end sub
 

aman

Registered User.
Local time
Today, 06:10
Joined
Oct 16, 2008
Messages
1,250
Cronk, it didn't work. :(

To enter the dates in textboxes , I click on the calendar button next to it. and if the code is written in change event and I click calendar to enter date in txtEnddate then nothing happens.

But if I start typing the date in then the code runs fine.

Can anyone please help me in this?

Thanks
 

CJ_London

Super Moderator
Staff member
Local time
Today, 14:10
Joined
Feb 19, 2013
Messages
16,610
what code are you using and on what event?
 

aman

Registered User.
Local time
Today, 06:10
Joined
Oct 16, 2008
Messages
1,250
CJ_London, I tried all the events on txtEndDate but nothing works as required. After clicking calendar on the textbox I want the code to run:

Code:
Private Sub txtEndDate_Change()
'If Len(Me.txtEndDate.Text & "") = 10 Then
      If IsDate(Me.txtEndDate) Then
         Me.FraOption.Visible = True
         Me.OptPrintAll.Visible = True
         Me.optPrintSel.Visible = True
         Me.lblPrintFilter.Visible = True
         'me.<next control>.setfocus
      End If
'  End If
End Sub
 

CJ_London

Super Moderator
Staff member
Local time
Today, 14:10
Joined
Feb 19, 2013
Messages
16,610
compare the line suggested by Cronk

If isdate(me.txtEndDate.text) then

with your equivalent

If IsDate(Me.txtEndDate) Then
 

Cronk

Registered User.
Local time
Today, 23:10
Joined
Jul 4, 2013
Messages
2,771
If you use this code
Code:
'If Len(Me.txtEndDate.Text & "") = 10 Then
      If IsDate(Me.txtEndDate) Then
you need to have
me.txtEndDate.text (as CJ suggested in #2)

By commenting out the line testing that the whole full date has been entered, the first digit entered will trigger the invalid date code.

In thinking about it further, what I would do is set the text field to a short date. That way, a valid date has to be entered. I'd also use the after_date event of both start and end date text boxes to check that both the start/end dates have been entered. (Your original post indicated that 2 valid dates need to be entered
Code:
MsgBox "Please make sure you have entered StartDate and End Date"
and if so, execute your visible code
 

Users who are viewing this thread

Top Bottom