Welcome to SPN

Register and Join the most happening forum of Sikh community & intellectuals from around the world.

Sign Up Now!

Check filed for input, if none return cursor

Discussion in 'Information Technology' started by AndyG, Jul 28, 2006.

  1. AndyG

    AndyG
    Expand Collapse
    Guest

    Someone please help me. I have 4 fields that I want to check as the user
    leaves the field if they have filled them in. Example form comes up, cursor
    on txtField1, user tabs (or else) off of txtField1, msgbox to alert user must
    enter something, put cursor back in txtField1 box.

    I have tried using the Private Sub txtField1_LostFocus() event to check the
    field. However to check you have to setfocus (txtField1.setfocus) then check
    if txtField1.text = "", and if so give message, and return cursor.

    It looks like it iterrates many times through this, and I don't understand
    why (perhaps once for each record ... or perhaps because each time I setfocus
    it is loosing focus and tripping the event? Also the txtField1.setfocus
    doesn't set the cursor back in the field, it remans in the next field.
    Please help me with some code so that I can understand the behavior, and
    principals.
    Thank you in advance.
     
  2. Loading...


  3. schasteen

    schasteen
    Expand Collapse
    Guest

    Why not in the after update event of txtField1:

    If me![txtField1] = "" or isnull(me![txtField1]) then
    Mgsbox "your message"
    Me![txtField1].setfocus
    end if


    "AndyG" wrote:

    > Someone please help me. I have 4 fields that I want to check as the user
    > leaves the field if they have filled them in. Example form comes up, cursor
    > on txtField1, user tabs (or else) off of txtField1, msgbox to alert user must
    > enter something, put cursor back in txtField1 box.
    >
    > I have tried using the Private Sub txtField1_LostFocus() event to check the
    > field. However to check you have to setfocus (txtField1.setfocus) then check
    > if txtField1.text = "", and if so give message, and return cursor.
    >
    > It looks like it iterrates many times through this, and I don't understand
    > why (perhaps once for each record ... or perhaps because each time I setfocus
    > it is loosing focus and tripping the event? Also the txtField1.setfocus
    > doesn't set the cursor back in the field, it remans in the next field.
    > Please help me with some code so that I can understand the behavior, and
    > principals.
    > Thank you in advance.
    >
     
  4. Ken Sheridan

    Ken Sheridan
    Expand Collapse
    Guest

    Examine the Value property of the text box, not its Text property. You don't
    need to move focus back to the control to do this. Also test for Null not a
    zero-length string, assuming its default value is not a zero-length string:

    If IsNull(Me.txtField1) Then
    MsgBox "Field 1 must be completed.", vbExclamation, "Invalid Operation"
    End If

    You won't be able to move focus back to the control in its own LostFocus
    event procedure, however. To do that you'd need to put the code in the next
    control's GotFocus event procedure.

    Do this at control level, however, assumes that the user tabs through the
    controls in sequence. If they skip over controls by selecting another one
    with the mouse the event won't occur. You should cover this by also testing
    for each control being Null in the form's BeforeUpdate procedure, and set the
    return value of the event procedure's Cancel argument to True if any of the
    controls are Null.

    Ken Sheridan
    Stafford, England

    "AndyG" wrote:

    > Someone please help me. I have 4 fields that I want to check as the user
    > leaves the field if they have filled them in. Example form comes up, cursor
    > on txtField1, user tabs (or else) off of txtField1, msgbox to alert user must
    > enter something, put cursor back in txtField1 box.
    >
    > I have tried using the Private Sub txtField1_LostFocus() event to check the
    > field. However to check you have to setfocus (txtField1.setfocus) then check
    > if txtField1.text = "", and if so give message, and return cursor.
    >
    > It looks like it iterrates many times through this, and I don't understand
    > why (perhaps once for each record ... or perhaps because each time I setfocus
    > it is loosing focus and tripping the event? Also the txtField1.setfocus
    > doesn't set the cursor back in the field, it remans in the next field.
    > Please help me with some code so that I can understand the behavior, and
    > principals.
    > Thank you in advance.
    >
     

Share This Page