Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms Programming / January 2005

Tip: Looking for answers? Try searching our database.

Option Group and setfocus problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Wylie C - 28 Jan 2005 23:39 GMT
I have a text box on a form as well as an option group within a frame
containing two option buttons. I have written code to display a message box
if the user does not enter any data in the text box. I have a setfocus
statement following the closing of the msgbox but the focus goes to the the
option box. Why and how can I fix that?

Dim intMsg as Integer
  IF txtDistance.text = "" Then
      intMsg = MsgBox("Distance value is blank", vbOKOnly,"Invalid Entry")
      txtDistance.setfocus
  End If

Thank you.
Allen Browne - 29 Jan 2005 03:17 GMT
Two issues: timing, and nulls.

Use the BeforeUpdate event of the control to validate it. If the entry is no
good, just cancel the event and Access won't let you out of the text box.

If nothing has been entered, the value will be Null. That's not the same as
a zero-length string: you need to test for it with IsNull().

Try something like this:
   Private Sub txtDistance_BeforeUpdate(Cancel As Integer)
       If IsNull(Me.txtDistance) Then
           Cancel = True
           MsgBox "Distance is blank", vbOKOnly,"Invalid Entry"
       End If
   End Sub

Notes:
1. It might be easier just to open your table in design view, select the
field, and set its Required property to Yes in the lower pane of table
design.

2. If txtDistance is a Text field (not a Number field), set the
AllowZeroLength property to No (also in the lower pane in table design).
That way you won't have to test for both IsNull() and also = "".

For more information on working with nulls, see:
   Common errors with Null
at:
   http://members.iinet.net.au/~allenbrowne/casu-12.html

Signature

Allen Browne - Microsoft MVP.  Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

>I have a text box on a form as well as an option group within a frame
> containing two option buttons. I have written code to display a message
[quoted text clipped - 11 lines]
>
> Thank you.
Wylie C - 29 Jan 2005 16:39 GMT
Thank you Allen. In conjunction with this post, the calculator is not
associated with a table, just a way the user can calculate the distance. The
code module works but now when I try to close the form, the invalid msg
appears and I end up in a loop. How would I avoid this?

> Two issues: timing, and nulls.
>
[quoted text clipped - 41 lines]
> >
> > Thank you.
Allen Browne - 30 Jan 2005 03:12 GMT
So you do want to let the user out of the text box if it's blank, but you
don't want to run the calculation if it's blank?

Then move the IsNull() text out of that event, and put it in the event where
you actually run the calculation (e.g. the Click  of a button?)

Signature

Allen Browne - Microsoft MVP.  Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

> Thank you Allen. In conjunction with this post, the calculator is not
> associated with a table, just a way the user can calculate the distance.
[quoted text clipped - 50 lines]
>> >
>> > Thank you.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.