By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,027 Members | 1,271 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,027 IT Pros & Developers. It's quick & easy.

Lost Focus Got Focus I cannot Focus

100+
P: 134
I am trying to validate a QTY entry in a form , if user did not enter anything or 0 , I will prompt for a message and the cursor return to the QTY entry column.

I have tried Lost Focus in the Qty -

If qty <=0 then
msgbox("Please Enter Qty")
me.qty.SetFocus
end if

But the cursor alway jump to the next entry box, I even replace with Cancel=true, it still doesnt work, Any Idea ?
Dec 15 '06 #1
Share this Question
Share on Google+
16 Replies


MMcCarthy
Expert Mod 10K+
P: 14,534
I am trying to validate a QTY entry in a form , if user did not enter anything or 0 , I will prompt for a message and the cursor return to the QTY entry column.

I have tried Lost Focus in the Qty -

If qty <=0 then
msgbox("Please Enter Qty")
me.qty.SetFocus
end if

But the cursor alway jump to the next entry box, I even replace with Cancel=true, it still doesnt work, Any Idea ?
Change to After Update event.

Expand|Select|Wrap|Line Numbers
  1. Private Sub qty_AfterUpdate()
  2.  
  3.    If IsNull(Me.qty) Or Me.qty <=0 Then
  4.       Msgbox "Please Enter Qty", vbOKOnly
  5.       Me.qty = Null
  6.       Me.qty.SetFocus
  7.    End If
  8.  
  9. End Sub
  10.  
Mary
Dec 15 '06 #2

100+
P: 134
But what if user press the Tab key or move the mouse away from this text box, then I don't think After Update could detect it, Am I Right ?
Dec 15 '06 #3

MMcCarthy
Expert Mod 10K+
P: 14,534
But what if user press the Tab key or move the mouse away from this text box, then I don't think After Update could detect it, Am I Right ?
Once the control has been entered it should trigger the after update event. However, to be sure, you just have to call the event in the forms OnClose event.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_OnClose()
  2.    qty_AfterUpdate
  3. End Sub
  4.  
Mary
Dec 15 '06 #4

100+
P: 134
The Qty entry column is default display at 0, if you press enter or tab or move your mouse away, the After Update cannot detect, I can only detect using Lost Focus, but it doesn't stay focus, even I issue setfocus, will just skip to next entry column,

Why should I use On Form Close, I want interactive repsonse to the user, don;t you think it right ?
Dec 15 '06 #5

MMcCarthy
Expert Mod 10K+
P: 14,534
The Qty entry column is default display at 0, if you press enter or tab or move your mouse away, the After Update cannot detect, I can only detect using Lost Focus, but it doesn't stay focus, even I issue setfocus, will just skip to next entry column,

Why should I use On Form Close, I want interactive repsonse to the user, don;t you think it right ?
Try the Before Update event.

Form On Close is just a catch all.

Mary
Dec 15 '06 #6

NeoPa
Expert Mod 15k+
P: 31,615
James,

Try it out as Mary suggests.
If you find it doesn't really work as you want it, then come back and explain why the answer isn't right.
I suspect that using the focus to trigger would actually cause you more problems than you expect.
Dec 15 '06 #7

P: 3
Hi there,

I have seen ur code why don't you try validate method of text box it works really nice in these cases.

gaurav
Dec 16 '06 #8

100+
P: 134
Hi there,

I have seen ur code why don't you try validate method of text box it works really nice in these cases.

gaurav
Are you Saying creating a text box such as txtQtyOrder instead of using the actual field name =QtyOrder
Dec 16 '06 #9

MMcCarthy
Expert Mod 10K+
P: 14,534
Are you Saying creating a text box such as txtQtyOrder instead of using the actual field name =QtyOrder
I'm terrible at validation using the property as I do all mine in code but what the previous poster is talking about is using the validation property which you will find under the data tab I think.

At a guess I would say if you put the following in the validation property if should work.

Expand|Select|Wrap|Line Numbers
  1. QtyOrder>0
Mary
Dec 16 '06 #10

NeoPa
Expert Mod 15k+
P: 31,615
Are you Saying creating a text box such as txtQtyOrder instead of using the actual field name =QtyOrder
Yup - that's about the size of it.
Consider Guarav's response though, that may be even easier for you.
Dec 16 '06 #11

100+
P: 134
I'm terrible at validation using the property as I do all mine in code but what the previous poster is talking about is using the validation property which you will find under the data tab I think.

At a guess I would say if you put the following in the validation property if should work.

Expand|Select|Wrap|Line Numbers
  1. QtyOrder>0
Mary
Let forget about using Before and After event, Is there a way the cursor can return to that fieldname-Qty in the Lost Focus validation ? I know using sendkey vbtab can be done, but that not the right way.
Dec 16 '06 #12

NeoPa
Expert Mod 15k+
P: 31,615
Let forget about using Before and After event, Is there a way the cursor can return to that fieldname-Qty in the Lost Focus validation ? I know using sendkey vbtab can be done, but that not the right way.
Lost Focus is not a 'validation' attribute.
LostFocus is an event procedure but Validation properties (Validation Rule & Validation Text) are attributes of the field itself.
Entering values in these can cause Access to insist on any changes to the field being valid according to your specified rules.
Dec 17 '06 #13

hyperpau
Expert 100+
P: 184
Let forget about using Before and After event, Is there a way the cursor can return to that fieldname-Qty in the Lost Focus validation ? I know using sendkey vbtab can be done, but that not the right way.
This may be long overdue but for sake of others looking for the solution:

Use instead the BeforeUpdate event as suggested above already.

Expand|Select|Wrap|Line Numbers
  1. Private Sub qty_BeforeUpdate (Cancel as Integer)
  2. If qty <=0 Then
  3. MsgBox("Please Enter Qty")
  4. Cancel = True
  5. End if
This will not set focus back but would actually prevent losing focus
whether by pressing TAB or by using mouse to move focus to other controls.
Aug 30 '08 #14

NeoPa
Expert Mod 15k+
P: 31,615
This should work of course, but why would you suggest adding code to produce the same results as the standard method (of using Validation Rule & Validation Text) for such a simple example?
Aug 30 '08 #15

P: 1
This may be long overdue but for sake of others looking for the solution:

Use instead the BeforeUpdate event as suggested above already.

Expand|Select|Wrap|Line Numbers
  1. Private Sub qty_BeforeUpdate (Cancel as Integer)
  2. If qty <=0 Then
  3. MsgBox("Please Enter Qty")
  4. Cancel = True
  5. End if
This will not set focus back but would actually prevent losing focus
whether by pressing TAB or by using mouse to move focus to other controls.
I have found a solution to the problem, it is definitely a workaround, but I think this is what is being asked for:

Expand|Select|Wrap|Line Numbers
  1. Private Sub TextBox1_LostFocus()
  2.  
  3.     If Len(Nz(Me.TextBox1, "")) = 0 Then
  4.         Me.TextBox2.SetFocus
  5.         Me.TextBox1.SetFocus
  6.     End If
  7.  
  8. End Sub
  9.  
Notes:
1. This is a test for a non-Null and non Zero length string
2. It uses another textbox as an interim target - this could be a textbox with its Visible property set to False.

If you want to control the onward direction from say Textbox1 to Textbox2, then simply adding the 'Else' construct does this fairly elegantly (although highly bespoked)

Expand|Select|Wrap|Line Numbers
  1. Private Sub TextBox1_LostFocus()
  2.  
  3.     If Len(Nz(Me.TextBox1, "")) = 0 Then
  4.         Me.TextBox2.SetFocus
  5.         Me.TextBox1.SetFocus
  6.     Else
  7.         Me.TextBox2.SetFocus
  8.     End If
  9.  
  10. End Sub
  11.  
Kind regards
Geoff
Sep 14 '08 #16

NeoPa
Expert Mod 15k+
P: 31,615
...
Expand|Select|Wrap|Line Numbers
  1. Private Sub TextBox1_LostFocus()
  2.  
  3.     If Len(Nz(Me.TextBox1, "")) = 0 Then
  4.         Me.TextBox2.SetFocus
  5.         Me.TextBox1.SetFocus
  6.     Else
  7.         Me.TextBox2.SetFocus
  8.     End If
  9.  
  10. End Sub
...
This code is logically equivalent to :
Expand|Select|Wrap|Line Numbers
  1. Private Sub TextBox1_LostFocus()
  2.  
  3.     Me.TextBox2.SetFocus
  4.     If Len(Nz(Me.TextBox1, "")) = 0 Then Me.TextBox1.SetFocus
  5.  
  6. End Sub
Can you explain why this would help the situation. I'm a bit confused as to what this is saying :S
Sep 14 '08 #17

Post your reply

Sign in to post your reply or Sign up for a free account.