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

Combo Box SetFocus not working...

P: 4
I have two combo boxes and a text box on a single form, and I need to make sure that the user selects (or inputs) a response in each control before saving the record. I already searched this site and found someone with almost the same problem, but the solution (add Cancel = True) didn't work for me... Here's what I've got so far in the OnLostFocus part of my first combo box (Conflict):

Private Sub Conflict_LostFocus()
If Trim(Me.Conflict) = "" Or IsNull(Me.Conflict) Then
MsgBox "The name of the conflict is a required field."
Cancel = True
Me.Conflict.SetFocus
End If
End Sub

Here's what happens: if the user skips the first combo box and clicks in the second combo box, the message displays, but after I close the message, the focus is on the second combo box. How can I make the focus go back to the first combo box even if the user never actually clicked on it?

Thanks,
Rebekah
Jul 24 '06 #1
Share this Question
Share on Google+
7 Replies


100+
P: 179
At first you said that you want the user to select a reponse in each control before saving the record. If this is all you want to do, couldn't you open the underlying table in design view and changed the "Required" property for each of the 3 fields to Yes.

Then, later on it seems like this may not be what you are looking for. It seems like you want the focus to always go back to the first combo box.

Is it possible to get more detail?

comteck
Jul 24 '06 #2

P: 4
I've got the underlying table properties set to "Required" for each of the three fields, so the record isn't actually saved if the user doesn't input something for each of the three controls - but I want a message to pop up that informs the user that it's not being saved, and allows him to correct his mistake and THEN save it properly... maybe there is a really easy way to do this that I'm not aware of? I've tried messing with the validation fields and all sorts of things, but I'm new to Access, so if there is an easy way to do this...?
Jul 24 '06 #3

100+
P: 179
OK, try this. Instead of LostFocus, Use AfterUpdate. Also, the "Required" fields in the underlying table must be set to "No" for this to work. I also noticed that you had the code in the same field as the one you are trying to change. Put it in the next one. For example, if the first combo box is called "conflict" and the second one is called "boodle", then put the code under "boodle".

Also, take out the "Cancel=True" statement, and replace it with "Me.Boodle=Null". You have to reset the field before you can SetFocus.

Anyway, here is the code for the second combobox. Like I said, the "Required" fields must be set to "No". I tried it and it worked. Let me know if you have any more problems.

Private Sub Boodle_AfterUpdate()
If Trim(Me.Conflict) = "" Or IsNull(Me.Conflict) Then
MsgBox "The name of the conflict is a required field."
Me.boodle = Null
Me.Conflict.SetFocus
End If
End Sub

comteck
Jul 24 '06 #4

P: 4
That's fantastic - Thank you! So if I put the code in the control following the one that I am checking, what do I do with the last control? Can I have the "Submit" button check it? I tried that, but I can't figure out the proper order of things - if the entries in the controls are correct, I want the form to save the record and close. If not, then I want the form to stay open and display the error message. The way I've got it now, I get the error message, but then the form still closes. Is there a way to override the form closing if the error message pops up?

Private Sub Save_New_Conflict_Click()

If Trim(Me.Duration) = "" Or IsNull(Me.Duration) Then
MsgBox "The name of the duration is a required field."
Me.Duration = Null
Me.Conflict.SetFocus
End If


DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70


DoCmd.RunMacro "Close Add New Conflict"

Exit_Save_New_Conflict_Click:
Exit Sub



End Sub
Jul 24 '06 #5

P: 4
Okay, just in case anyone else ever has this problem, here's the final (working!) code (my controls are "Conflict," "Grouping," "Cause," and "Number Lost." I finally figured out that the key is that Exit Sub line in each one. I put this in the On Click command of the "submit" button on my form and it works great.


Private Sub Command25_Click()
On Error GoTo Err_Command25_Click

If Trim(Me.Conflict) = "" Or IsNull(Me.Conflict) Then
MsgBox "The conflict is a required field."
Me.Conflict.SetFocus
Exit Sub
End If

If Trim(Me.Grouping) = "" Or IsNull(Me.Grouping) Then
MsgBox "The grouping is a required field."
Me.Grouping.SetFocus
Exit Sub
End If

If Trim(Me.Cause) = "" Or IsNull(Me.Cause) Then
MsgBox "The cause of loss is a required field."
Me.Cause.SetFocus
Exit Sub
End If


If Trim(Me.[Number Lost]) = "" Or IsNull(Me.[Number Lost]) Then
MsgBox "The number lost is a required field."
Me.[Number Lost].SetFocus
Exit Sub
End If
DoCmd.Close


Exit_Command25_Click:
Exit Sub

Err_Command25_Click:
MsgBox Err.Description
Resume Exit_Command25_Click

End Sub
Jul 25 '06 #6

100+
P: 179
On the third control, you should be able to use similar code, but check that the 2 previous controls have an entry, as opposed to just one.

With your button control, I assume that the following code is used to close the form:
DoCmd.RunMacro "Close Add New Conflict"
The reason your form is closing is because that should be part of your conditional statement. Try this:

Private Sub Save_New_Conflict_Click()

If Trim(Me.Duration) = "" Or IsNull(Me.Duration) Then
MsgBox "The name of the duration is a required field."
Me.Duration = Null
Me.Conflict.SetFocus

Else
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
DoCmd.RunMacro "Close Add New Conflict"
End If

Exit_Save_New_Conflict_Click:
Exit Sub

The part under "Else" will only be carried out if the "IF" part isn't true.

comteck


End Sub
Jul 25 '06 #7

P: 5
Hello
I'm trying to accomplish what Rebekah tried. Unfortunately it's not really working for me. I have a cuople of combo boxes but i want to ensure that all of the field have a value ie not null. I've tried long and hard to find the 'required' property in the property window for each but i don't see anything that i can click to either say yes or no. Can someone help me? I've tried before and after update and also tried creating macro's for it to work. Basically before the user clicks on the "Add record" button. I want to make sure that field A B C D E have a value. Then after all these values are in go ahead and add the record. if someone could help me or email me in regards to this I would be greatful.

Eg of Macro:
[a] is null
cancelevent
msgbox
gotocontrol (which positions it in that field)

hoping to hear from someone soon

thanks
Mar 29 '07 #8

Post your reply

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