Go Back   Access World Forums > Microsoft Access Discussion > Forms

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 09-30-2019, 01:15 PM   #1
sxschech
Newly Registered User
 
Join Date: Mar 2010
Posts: 612
Thanks: 28
Thanked 112 Times in 101 Posts
sxschech is on a distinguished road
SetFocus and Select Text

I am using the following to Select the textbox when the date is before another date. I thought it wasn't working. However when I stepped through the code it does select the text until the End Sub and then becomes unselected. I have this in the after update event. Is this the right spot? Or should I use another technique to highlight the textbox for user to decide whether to adjust the date.

Code:
If Me.tb_DateTaken < dtRWP Then
        MsgBox "Training Date of " & Me.tb_DateTaken & " is before the RWP date of " & dtRWP & ".  Please verify this is correct or enter a date after the RWP date.", vbOKOnly + vbQuestion, "Date Issue"
        Me.tb_DateTaken.SetFocus
        Me.tb_DateTaken.SelStart = 0
        Me.tb_DateTaken.SelLength = Len(Me.tb_DateTaken)
    End If
End Sub
DateSelected is how it looks prior to End Sub and DateNotSelected is how it looks when the code has finished.

DateNotSelected.PNG

DateSelected.PNG

sxschech is offline   Reply With Quote
Old 09-30-2019, 01:34 PM   #2
June7
AWF VIP
 
June7's Avatar
 
Join Date: Mar 2014
Location: The Great Land
Posts: 2,428
Thanks: 0
Thanked 568 Times in 564 Posts
June7 will become famous soon enough June7 will become famous soon enough
Re: SetFocus and Select Text

I use Enter or Click event of control that gets focus to set itself.

Which textbox AfterUpdate has this code?
__________________
Attach File Manager is below Advanced editor window, click Go Advanced below Quick Reply window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.
DEBUG! DEBUG! DEBUG!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
June7 is offline   Reply With Quote
Old 09-30-2019, 02:25 PM   #3
sxschech
Newly Registered User
 
Join Date: Mar 2010
Posts: 612
Thanks: 28
Thanked 112 Times in 101 Posts
sxschech is on a distinguished road
Re: SetFocus and Select Text

I have this in the after update of the field in question: Me.tb_DateTaken

sxschech is offline   Reply With Quote
Old 09-30-2019, 02:36 PM   #4
June7
AWF VIP
 
June7's Avatar
 
Join Date: Mar 2014
Location: The Great Land
Posts: 2,428
Thanks: 0
Thanked 568 Times in 564 Posts
June7 will become famous soon enough June7 will become famous soon enough
Re: SetFocus and Select Text

Use BeforeUpdate event to validate data.
__________________
Attach File Manager is below Advanced editor window, click Go Advanced below Quick Reply window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.
DEBUG! DEBUG! DEBUG!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
June7 is offline   Reply With Quote
Old 09-30-2019, 03:02 PM   #5
sxschech
Newly Registered User
 
Join Date: Mar 2010
Posts: 612
Thanks: 28
Thanked 112 Times in 101 Posts
sxschech is on a distinguished road
Re: SetFocus and Select Text

Changed the code as you advised to BeforeUpdate. Same result, after the End Sub the selection goes away.

Code:
Private Sub tb_DateTaken_BeforeUpdate(Cancel As Integer)
Dim dtRWP As Date
    
    dtRWP = DLookup("EmployeeTraining_DateTaken", "tbl_dt_EmployeeTraining", "EmployeeTraining_EmployeeID=" & Me.EmployeeTraining_EmployeeID & " AND EmployeeTraining_TrainingTypeID = 6")
   
    If Me.tb_DateTaken < dtRWP Then
        MsgBox "Training Date of " & Me.tb_DateTaken & " is before the RWP date of " & dtRWP & ".  Please verify this is correct or enter a date after the RWP date.", vbOKOnly + vbQuestion, "Date Issue"
        
        Me.tb_DateTaken.SelStart = 0
        Me.tb_DateTaken.SelLength = Len(Me.tb_DateTaken)
    End If
End Sub
sxschech is offline   Reply With Quote
Old 09-30-2019, 03:27 PM   #6
June7
AWF VIP
 
June7's Avatar
 
Join Date: Mar 2014
Location: The Great Land
Posts: 2,428
Thanks: 0
Thanked 568 Times in 564 Posts
June7 will become famous soon enough June7 will become famous soon enough
Re: SetFocus and Select Text

And does control lose focus?

Need use:

Cancel = True

If you want control to retain focus.
__________________
Attach File Manager is below Advanced editor window, click Go Advanced below Quick Reply window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.
DEBUG! DEBUG! DEBUG!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
June7 is offline   Reply With Quote
The Following User Says Thank You to June7 For This Useful Post:
sxschech (10-01-2019)
Old 09-30-2019, 05:31 PM   #7
sxschech
Newly Registered User
 
Join Date: Mar 2010
Posts: 612
Thanks: 28
Thanked 112 Times in 101 Posts
sxschech is on a distinguished road
Re: SetFocus and Select Text

Would Cancel remove the entry? They don't want to undo the entry since there are exceptions, it is only for information purposes and I thought would be helpful if the textbox would be highlighted so user can decide if the date needs to be revised. Maybe tomorrow I'll try a different approach and see about placing a rectangle around the date and then hide/unhide it?

sxschech is offline   Reply With Quote
Old 09-30-2019, 06:00 PM   #8
June7
AWF VIP
 
June7's Avatar
 
Join Date: Mar 2014
Location: The Great Land
Posts: 2,428
Thanks: 0
Thanked 568 Times in 564 Posts
June7 will become famous soon enough June7 will become famous soon enough
Re: SetFocus and Select Text

Does not remove entry. Try it.

If you want to allow user to keep the entry, then probably need a nested If Then:

If MsgBox("Do you want edit date?", vbYesNo) = vbYes Then
Cancel = True

End If
__________________
Attach File Manager is below Advanced editor window, click Go Advanced below Quick Reply window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.
DEBUG! DEBUG! DEBUG!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by June7; 09-30-2019 at 06:06 PM.
June7 is offline   Reply With Quote
Old 09-30-2019, 06:51 PM   #9
sxschech
Newly Registered User
 
Join Date: Mar 2010
Posts: 612
Thanks: 28
Thanked 112 Times in 101 Posts
sxschech is on a distinguished road
Re: SetFocus and Select Text

I'll try tomorrow when back in the office.
sxschech is offline   Reply With Quote
Old 09-30-2019, 09:38 PM   #10
arnelgp
error reading drive A:
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 8,569
Thanks: 68
Thanked 2,745 Times in 2,630 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: SetFocus and Select Text

Code:
rivate Sub tb_DateTaken_BeforeUpdate(Cancel As Integer)
Dim dtRWP As Date
    
    dtRWP = DLookup("EmployeeTraining_DateTaken", "tbl_dt_EmployeeTraining", "EmployeeTraining_EmployeeID=" & Me.EmployeeTraining_EmployeeID & " AND EmployeeTraining_TrainingTypeID = 6")
   
    If Me.tb_DateTaken < dtRWP Then
        MsgBox "Training Date of " & Me.tb_DateTaken & " is before the RWP date of " & dtRWP & ".  Please verify this is correct or enter a date after the RWP date.", vbOKOnly + vbQuestion, "Date Issue"
        Cancel = True
        Me.tb_DateTaken.SelStart = 0
        Me.tb_DateTaken.SelLength = Len(Me.tb_DateTaken.Text)
    End If
End Sub
__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
The Following User Says Thank You to arnelgp For This Useful Post:
sxschech (10-01-2019)
Old 10-01-2019, 07:57 AM   #11
sxschech
Newly Registered User
 
Join Date: Mar 2010
Posts: 612
Thanks: 28
Thanked 112 Times in 101 Posts
sxschech is on a distinguished road
Re: SetFocus and Select Text

Getting closer, thanks June7 and arnelgp. After adding your code, it does keep the highlight and the date just like you said. The new issue is it puts up a message box "The value violates the validation rule for the field or record". Since the user has already been prompted that there is an issue, this secondary message is not needed. I put in an error handler and stepped through the code and like before the error message pops up after the code has finished (Pressing F8 on End Sub). It does not seem to be generating an error while in the code as I did a debug.print just before the End Sub for err.Number and it was 0.
sxschech is offline   Reply With Quote
Old 10-01-2019, 08:02 AM   #12
arnelgp
error reading drive A:
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 8,569
Thanks: 68
Thanked 2,745 Times in 2,630 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: SetFocus and Select Text

capture the Error number.
add code the the Form's Error Event:
Code:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 'the error number here'
    ' already handled
    Response = acDataErrContinue
End If
End Sub
__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
The Following User Says Thank You to arnelgp For This Useful Post:
sxschech (10-01-2019)
Old 10-01-2019, 08:13 AM   #13
June7
AWF VIP
 
June7's Avatar
 
Join Date: Mar 2014
Location: The Great Land
Posts: 2,428
Thanks: 0
Thanked 568 Times in 564 Posts
June7 will become famous soon enough June7 will become famous soon enough
Re: SetFocus and Select Text

Post your code. Did you consider the nested If Then I suggested?

What Validation Rule did you set?
__________________
Attach File Manager is below Advanced editor window, click Go Advanced below Quick Reply window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.
DEBUG! DEBUG! DEBUG!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
June7 is offline   Reply With Quote
Old 10-01-2019, 08:30 AM   #14
sxschech
Newly Registered User
 
Join Date: Mar 2010
Posts: 612
Thanks: 28
Thanked 112 Times in 101 Posts
sxschech is on a distinguished road
Re: SetFocus and Select Text

Thanks arnelgp. That sorted it out.

Thanks June7 for your suggestions/advice and code. I tried both your version and arnelgp's and since they both returned the same result, decided to go with arnelgp's since his worked without the nested If.
sxschech is offline   Reply With Quote
Old 10-01-2019, 08:33 AM   #15
June7
AWF VIP
 
June7's Avatar
 
Join Date: Mar 2014
Location: The Great Land
Posts: 2,428
Thanks: 0
Thanked 568 Times in 564 Posts
June7 will become famous soon enough June7 will become famous soon enough
Re: SetFocus and Select Text

Interesting, I did not get error in my test. Did not have ValidationRule property set either.

__________________
Attach File Manager is below Advanced editor window, click Go Advanced below Quick Reply window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.
DEBUG! DEBUG! DEBUG!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
June7 is offline   Reply With Quote
Reply

Tags
afterupdate event , form , text box

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
move cursor to end of text in textbox after setfocus fire2ice Modules & VBA 16 10-15-2015 01:20 AM
How to setfocus on a Specific Text box TheSearcher Forms 12 11-11-2013 01:29 PM
SetFocus on a Text Box ...error KingBroil Forms 6 01-16-2013 12:32 PM
SetFocus to Text Box Not Working DavidWE Modules & VBA 3 10-07-2011 02:18 AM
setfocus and accessing a text box swarv Forms 4 03-13-2009 11:16 AM




All times are GMT -8. The time now is 05:05 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