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

Force user to select a option form Combobox

P: 3
I'm a newby . Created a from via wizard with two combo boxes. In the first user must select a Customer. Based on first selection, second combo box displayed Material List of selected Customer again for selection. This is all working 100%

Problem : I want to force user to select a Customer in first combo box. If nothing is selected, the Access must "force" a selection.

Below is not working ?
If user does not select anything I do get the TEST message displayed, but the "Me.Mat_PSlipIN_NewCustomer.SetFocus" does not force the focus back to the first combobox for selection.

Hope I've explained myself....

Expand|Select|Wrap|Line Numbers
  1. Private Sub Mat_PSlipIN_NewCustomer_Exit(Cancel As Integer)
  2.     If IsNull(Mat_PSlipIN_NewCustomer.Value) Or Mat_PSlipIN_NewCustomer.Value = "" Then
  3.         msgbox "TEST"
  4.         Me.Mat_PSlipIN_NewCustomer.SetFocus
  5.     Else
  6.     End If
  7. End Sub

Please assist !
Sep 14 '18 #1
Share this Question
Share on Google+
5 Replies

Expert Mod 2.5K+
P: 3,284

Welcome to Bytes!

Have you done a direct copy and paste from your project? Because the MsgBox function is not in “CamelCase” it appears you have not. We always recommend a direct copy and paste, so we know that what is in your project is what you have given us.

Based upon what you have given us, there is no indication as to why the focus does not stay on that particular control, other than the fact that you have exited the control. You may try using the AfterUpdate Event instead.

Along different lines, whenever I want to force a user to enter data into a specific control (as you have described), I disable all other controls until the control in question has received acceptable data. I also use that Control’s AfterUpdate event to drive the activity. Hence:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Mat_PSlipIN_NewCustomer_AfterUpdate()
  2.     Me.TheNextControl.Enabled = _
  3.         Not (IsNull(Mat_PSlipIN_NewCustomer.Value) Or _
  4.             (Mat_PSlipIN_NewCustomer.Value = ""))
  5. End Sub
There is also much more that can be done with such a set up.

On a side note, it may ease the strain on your fingers to have shorter names for your controls, along with a standardized naming convention for the type of control. For example, if Mat_PSlipIN_NewCustomer is a combo box, then, an appropriate prefix for that control and a shortened name should be sufficient, such as cboNewCustomer. This all goes to preference and clarity—not just for your sake, but for anyone else who might come after you and try to decipher your code.
Sep 14 '18 #2

P: 3
Hi thanks for speedy response.
1. Yes I have just copied and paste the code . Excuse my ignorance but what is Camelcase ??
2. I've used your code in the AfterUpdate routine, but still not working...
3. Agree with the naming - make good sense. I'm busy renaming all. Thx for advise

Anything else I should check ? ( I really do appreciate your valued time to assist this newby :)
Sep 14 '18 #3

Expert Mod 2.5K+
P: 3,284
1. If you had truly copied and pasted your code from your VBA project, then “msgbox” (which is all lower case) would have been posted as “MsgBox” (which is CamelCase—there are upper and lower case letters).

2. You don’t say what is not working about the new code that you have tried. Have you inserted breaks at different points along the code to see if it is even going through the code?

We need a little more detail before we can help with troubleshooting.
Sep 15 '18 #4

P: 3

Sorry you're right - not a proper copy and paste. I concentrated so on code itself I must have changed the "Camelcase". This is way iim a newby and you are the Expert !
I took your advise regarding naming of controls.

See below revised code.
If I select a Customer from cboCustomerList I do get the TEST message as proof that code is working and then the cboMaterialList is updated with the correct material list.
If however I do not select a Customer in cboCustomerList and either press Enter or Tab, focus moves to cboMaterialList without giving me the TEST message and this resulted in the cboMaterialList that is empty (which is correct --> no customer - no material list)

Expand|Select|Wrap|Line Numbers
  1. Private Sub cboCustomerList_AfterUpdate()
  2.     Me.cboMaterialList.Enabled = _
  3.         Not (IsNull(cboCustomerList.Value) Or _
  4.             (cboCustomerList.Value = ""))
  5.             MsgBox "Test"
  6.             Me.cboMaterialList.Requery
  7. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboMaterialList_GotFocus()
  2.         cboMaterialList = ""
  3. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboMaterialList_LostFocus()
  2.     Dim nuweMaterialNo As String
  3.     Customer_ID = cboCustomerList
  5.     If Len(cboMaterialList) < 1 Then
  6.         Cancel = True
  7.     Else
  8.         Product_ID = cboMaterialList
  9.     End If
  10. End Sub
Sep 15 '18 #5

Expert Mod 2.5K+
P: 3,284
I think you essentially have the answer already. Instead of using the AfterUpdate event on cboCustomerList, use the LostFocus event. If your cboMaterialList combo box was disabled by default, then it would be impossible to move to it in the first place.

In the cboCustomerList combo box’s LostFocus event, check to see if that control has a value. If not, then do nothing (or inform the user that they need to select a value). If there is a value, then request the cboMaterialList.

I hope this makes sense. I would remove any code associated with cboMaterialList for now, unless there is an additional data check for what the user selects from it.

Like I said, I think you are very close to the final solution.
Sep 15 '18 #6

Post your reply

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