469,300 Members | 2,355 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,300 developers. It's quick & easy.

Required field / error with null value / can't move focus

Hi all

I have a combo-box control [Insertion] which I've set as a required field. It's a date and uses a calendar as coded below. All works fine unless the user deletes a pre-existing value (leaving a 0-length string I guess) and then clicks in the Insertion control.

This results in Runtime error '2110' with the message that "can't move the focus to the control Calendar0"

I think this is because I've made Insertion a required field - certainly the problem is "resolved" if I make Insertion not required.

But I do want it to be a required field...any ideas? I did try loading a default value (today's date) into the field after MouseDown if the value was null or zero length string but that didn't work and anyway I don't like the idea of deliberately putting a false value into the field.
Access 2003, Windows XP

Expand|Select|Wrap|Line Numbers
  1. Private Sub Insertion_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  2. Calendar0.Visible = True
  3. Calendar0.SetFocus
  4. If Not IsNull(Insertion) Then
  5.     Calendar0.Value = Insertion.Value
  6. Else
  7.     Calendar0.Value = Date
  8.  
  9. End If
  10. End Sub
  11. Private Sub Calendar0_Click()
  12. If Calendar0.Value > Date Then
  13. MsgBox "Insertion Date cannot be in the future."
  14.             Calendar0.SetFocus
  15.             Calendar0.Value = Date
  16. ElseIf Calendar0.Value < Me.Parent.DOB Then
  17.     Call MsgBox("Insertion Date cannot Precede Date of Birth", , "NICU Line Infection Database")
  18.             Calendar0.SetFocus
  19.             Calendar0.Value = Me.Parent.DOB
  20.   Else
  21.   Insertion.Value = Calendar0.Value
  22.   Insertion.SetFocus
  23. Calendar0.Visible = False
  24. End If
  25. End Sub
  26.  
Thanks - Michael
May 29 '07 #1
7 7527
MMcCarthy
14,534 Expert Mod 8TB
Hi Michael,

Don't set the field to required, just make it so they can't save the record unless the date if entered.

Mary
Jun 1 '07 #2
Hi Michael,

Don't set the field to required, just make it so they can't save the record unless the date if entered.

Mary
You are messing with my mind!

Michael.
Jun 1 '07 #3
MMcCarthy
14,534 Expert Mod 8TB
You are messing with my mind!

Michael.

... unless the date is entered. sorry.

Just check for the date in the save/close event or maybe in the current event whichever is more suitable. In other words if they try to add a record without entering a date you can popup a message box saying please enter date to proceed.
Jun 1 '07 #4
maxamis4
295 Expert 100+
I don't see why you don't set the code in the after update event of the combo box. What this means is that after the combo box is updated, it will return the value you want.


I can't follow your code because you don't use typical programming conventions. I don't mean to be rude just trying to help you out. Could repost the variable with labels so I can assist you with this.

example:

variable1 = my combo box
variable2 = my text box
variable3 = my button

thanks
Jun 2 '07 #5
... unless the date is entered. sorry.

Just check for the date in the save/close event or maybe in the current event whichever is more suitable. In other words if they try to add a record without entering a date you can popup a message box saying please enter date to proceed.

Thanks - I tried that idea using a few different events - in the end the unload event of the Parent form seemed to be best:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Unload(Cancel As Integer)
  2.     If IsNull(Forms!frmentry.subfrmlines![Insertion].Value) Then
  3.     MsgBox ("Please Enter the Line Insertion Date")
  4.     Cancel = True
  5.     Me!subfrmlines.SetFocus
  6. Me!subfrmlines.Form!Calendar0.Visible = True
  7. Me!subfrmlines.Form!Calendar0.SetFocus
  8. Me!subfrmlines.Form!Calendar0.Value = Date
  9.     End If
  10. End Sub
  11.  
Cheers - Michael.
Jun 5 '07 #6
MMcCarthy
14,534 Expert Mod 8TB
Glad you got it working Michael.
Jun 5 '07 #7
jordimarsa
2 2Bits
First of all, I prefer always using before or after update events in order to tirgger them always. The mouse events can be avoided by using the keyboard instead.

Secondly, I think that there is a missunderstanding on how to change values in a form. You do not really need to set focus on another control you can just change the value property.

Finally, about the error, you can do several things taking into account that you are using a required field. If the field is required and it has been already filled you cannot change it by a null value. The only way to have a null value in a required field is before inserting a record and before attempting to change it.

A good way to avoid the error is by using the BeforeUpdate event and if the value is null use the method "Me.[Insertion].Undo" to retake the last value introduced. But a required field means exactly that you cannot save the record without a value. It's in your hand which value you prefer to leave if the user decide to do something forbiden. In datasheet mode I tend to show the error and set the focus to the field that has been missed, but you can undo the change or even set another value more suitable like the current date. If you want to do something particular, please explain me how is your form and what exactly you expect it to behave for the user and I will try to help you.
3 Weeks Ago #8

Post your reply

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

Similar topics

2 posts views Thread by fish | last post: by
2 posts views Thread by SAN CAZIANO | last post: by
16 posts views Thread by Georges Heinesch | last post: by
2 posts views Thread by bufbec1 | last post: by
7 posts views Thread by Mike P | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.