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 / May 2008

Tip: Looking for answers? Try searching our database.

setfocus to textbox

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jeff - 21 May 2008 19:03 GMT
I am having trouble getting the setfocus command to work. I have a textbox
that is linked to a database field. I want to make sure they have entered
something in the textbox before the move to the next field. I put vb code in
the loseFocus event. I looks like this.

If IsNull(Me.Survey_ID.Value) Then
       Me.Survey_ID_Status.Value = "BAD"
       Me.Survey_ID.SetFocus
Else
       Me.Survey_ID_Status.Value = "GOOD"
End If

I can see the value change in the Survey_ID_Status field when I run the
form, but it never sets the focus back to the Survey_ID textbox. It just
keeps moving forward.

It's like it just ignores the Setfocus command.
Klatuu - 21 May 2008 19:54 GMT
That will not work.  Here is how it is done:
Use the Survey_ID control's Before Update event.  It can be canceled and
when it is, the focus does not move.

Private Sub Survery_ID_BeforeUpdate(Cancel As Integer)

   If Trim(Nz(Me.Survery_ID, vbNullString)) & vbNullString = vbNullString
Then
       MsgBox "A Value is required"
       Cancel = True
   End If

End Sub

this part:
Trim(Nz(Me.Survery_ID, vbNullString)) & vbNullString

Will catch not only a Null, but cases where a user has accidentily put a
space in the control.
Signature

Dave Hargis, Microsoft Access MVP

> I am having trouble getting the setfocus command to work. I have a textbox
> that is linked to a database field. I want to make sure they have entered
[quoted text clipped - 13 lines]
>
> It's like it just ignores the Setfocus command.
strive4peace - 21 May 2008 19:54 GMT
Hi Jeff,

rather than the LostFocus event, use the control BeforeUpdate event and
CANCEL the change ... better yet, use the FORM BeforeUpdate event to
ensure that all required data is filled before a record (new or changed)
is saved

what is Survey_ID?  something that the user is supposed to fill out
before a record is saved?

to validate a record and prevent it from being saved, put code in the
form BeforeUpdate event

'~~~~~~~~~~~~~~~~~~~~~~~~~~
   '----------------- make sure all required data is filled out

   'make sure Survey_ID is filled out
   If IsNull(me.Survey_ID) then

      'if it is not filled out, then move the focus to that control
      me.Survey_ID.setFocus

      'give the user a message
      msgbox "You must enter the Survey ID",,"Missing Data"

      'don't save the record yet
      Cancel = true

      'quit checking and give them a chance to fill it out
      exit sub
   end if
'~~~~~~~~~~~~~~~~~~~~~~~~~~

Warm Regards,
Crystal

Access Basics
8-part free tutorial that covers essentials in Access
http://www.AccessMVP.com/strive4peace

 *
   (: have an awesome day :)
 *

> I am having trouble getting the setfocus command to work. I have a textbox
> that is linked to a database field. I want to make sure they have entered
[quoted text clipped - 13 lines]
>
> It's like it just ignores the Setfocus command.
 
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.