Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 18th, 2005, 04:01 PM
Authorized User
 
Join Date: Apr 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to lguzman
Default Keeping the focus on a field

I have a form on which one field is required only if another field on the same form has a specific value. How can I force the user to enter a value on that field before allowing him to continue to fill up the rest of the form?

Thanks.

[LGuzman]
__________________
[LGuzman]
 
Old April 18th, 2005, 07:50 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post
Default

Use either the Exit event or the LostFocus event of the field. See if it contains a value. If not, SetFocus back to the field.

Be careful, the field will lose focus if changing to another form within your application or even to another application. Be sure to test this carefully.

Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
http://www.pnwadg.org
 
Old April 19th, 2005, 12:27 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

Be also careful you don't trap the user on the field. Scenario: the user enters the data in Field1. Field2 is now required because Field1 = X. You set focus to Field2. Your lost focus code says that it's required and won't let the user out. What if the user then decides that Field1 is NOT X? The user is now "stuck" in Field2 unless he/she puts a dummy value on Field2, changes Field1, and erases Field2 again.


Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old April 19th, 2005, 03:14 PM
Authorized User
 
Join Date: Apr 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to lguzman
Default

First I would like to thank you both for replying.

Second, I now have tried on the LostFocus event of the field in which I want to force the user to enter a value, to set the focus back on it if it's value is Null.

I have tested with a Msgbox to make sure I am getting within the condition, which I am, but the Fieldname.SetFocus doesn't seem to work. Meaning when I tab out of the said field, it goes to the next field, leaving the required one empty!

[LGuzman]
 
Old April 19th, 2005, 03:18 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

Can you please post your code for the field's Lost Focus event? That'll help us figure out what's wrong. Thanks.


Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old April 20th, 2005, 07:46 AM
Authorized User
 
Join Date: Apr 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to lguzman
Default

Private Sub Cmb_SecondarySTI_LostFocus()
If IsNull([Cmb_SecondarySTI]) Then
   [Cmb_SecondarySTI].SetFocus
End If
End Sub

I have also tried the following:
Private Sub Cmb_SecondarySTI_LostFocus()
If IsNull([Cmb_SecondarySTI]) Then
   Cmb_SecondarySTI.SetFocus
End If
End Sub

---

Private Sub Cmb_SecondarySTI_LostFocus()
If IsNull([Cmb_SecondarySTI]) Then
   Forms!Frm_Application!Cmb_SecondarySTI.SetFocus
End If
End Sub




[LGuzman]
 
Old April 20th, 2005, 09:18 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post
Default

My profound apologies. Not sure why it's not working. It doesn't work for me either. But this will work...

Code:
Private Sub Cmb_SecondarySTI_Exit(Cancel as Integer)
If IsNull(Me.Cmb_SecondarySTI) Then Cancel = True
End Sub
Note the change to the Exit event instead of LostFocus
 
Old April 20th, 2005, 10:18 AM
Authorized User
 
Join Date: Apr 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to lguzman
Default

No apologies necessary. I appreciate the time you took to help me. It seems to me that what you first sugested should have worked. Anyway, I tried your code and it works.

Thanks.


[LGuzman]





Similar Threads
Thread Thread Starter Forum Replies Last Post
Determining what field has focus? firefighter2045 Access VBA 4 November 6th, 2007 12:58 PM
Set Focus to Field in User Control Ron Howerton ASP.NET 1.0 and 1.1 Professional 3 March 14th, 2006 05:53 PM
Focus on a text input field fizzerchris Classic ASP Basics 0 December 16th, 2005 09:20 PM
.showmodaldialog and field focus DarrenMelling Classic ASP Basics 0 February 14th, 2005 06:45 AM
calculate value on Lost Focus of text field creative_eye General .NET 1 April 12th, 2004 08:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.