Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

combobox required 2

Status
Not open for further replies.

installer69

Programmer
Sep 18, 2004
35
GB
I have a combo box that has two choices, good or faulty which then appear in a text field on the next form.

How do I prevent a user pressing the enter button without making a choice from the combo?
 
In the properties of the combo box field, select the data tab and set the Limit to List option to "Yes".
 
Set the field to required=yes on the underlying table. You can also include a default value so that the combo always displays something. Of course, your user might put in "good" when it really is "Faulty". Another method I have used in the past is to perform an IsNull check on the field before a save, close or move record event, and on before update. Such as

If IsNull (me.cboGoodFaulty) Then
MsgBox "You must choose either Good or Faulty", vbOkCancel, "My App Name"
Cancel = True
Else:
Let the user save, move add, close etc
End If

Once the user enters the field, they cannot do anything else until valid data has been entered. Even in this case they may still lie about it being good or faulty.


I haven't failed, I have just found 10,000 ways that it won't work!
 
Thanks txacces. Got the error box up on null entry but when I press ok it still goes to the next form, 'issuestock'. How do I stop it passing the to this?

Transition is triggered by an enter buton with the following onclick:


stDocName = "issuestock"
DoCmd.OpenForm stDocName
 
you need to put this:

If IsNull (me.cboGoodFaulty) Then
MsgBox "You must choose either Good or Faulty", vbOkCancel, "My App Name"
Cancel = True
Else:
Let the user save, move add, close etc
End If

in the OnClick event of the button that is opening the other form and check it before you open the other form:

Code:
If IsNull (me.cboGoodFaulty) Then
  MsgBox "You must choose either Good or Faulty", vbOkCancel, "My App Name"
  Cancel = True
Else:
  stDocName = "issuestock"
  DoCmd.OpenForm stDocName
End If

Leslie
 
Change the vbOKCancel to vbInformation or something similar with only one option and then use setfocus to take the cursor to the field that failed validation. Each time the validation fails, the MsgBox will appear & the SetFocus runs. There is no way out until the field passes the validation rule.

I haven't failed, I have just found 10,000 ways that it won't work!
 
Ok guys, you've patiently got me this far. txaccess, I have trouble understanding the process of changing vbOKcancel to vbInformation. I understand that I need to set focus back to the 'engineer' combo box but having tried a few variations I have to ask your help please. I have now got on the onclick function this:

Code:
On Error GoTo Err_Command2_Click

    If IsNull(Me.engineer) Then
  MsgBox "You must choose either Good or Faulty", vbInformation, "setFocus(Me.engineer)"
 
  Cancel = True
Else:
  stDocName = "issuestock"
  DoCmd.OpenForm stDocName
End If
 
try this:

Code:
On Error GoTo Err_Command2_Click

    If IsNull(Me.engineer) Then
  MsgBox "You must choose either Good or Faulty", vbInformation, "Title of Message Box" 
  setFocus(Me.engineer)
 
  Cancel = True
Else:
  stDocName = "issuestock"
  DoCmd.OpenForm stDocName
End If

Leslie
 
Sorry that I was not clearer - the above solutions describe perfectly what I was attempting to explain. Good luck.

I haven't failed, I have just found 10,000 ways that it won't work!
 
I got a compile error:

Wrong number of arguments or invalid property assignment
 
try me.engineer.setfocus in stead of setfocus( me.engineer)

I haven't failed, I have just found 10,000 ways that it won't work!
 
I'm sure you wrote that before I posted my error!!

You and Leslie make a superb double act.

Thanks, it worked. To celebrate I'm not going to stay up 'till 3am working on the thing tonight. It's only 10.30pm and I might even talk to the wife for a while!
 
Here's the final code if it helps someone else:

Code:
Private Sub Command2_Click()
On Error GoTo Err_Command2_Click

    If IsNull(Me.engineer) Then
  MsgBox "You Must Choose an Engineer", vbInformation, "HEY FOOL!"
Me.engineer.SetFocus
  Cancel = True
  
Else:
  stDocName = "issuestock"
  DoCmd.OpenForm stDocName
End If
 
Good luck, and keep the wife happy.

I haven't failed, I have just found 10,000 ways that it won't work!
 
1 last thing.

Ive tried this on a text box on a form displayed as a data sheet.

Although the message pops up etc it doesnt set the focus back to the text box but moves on to the next field as usual

Code:
    If IsNull(Me.job) Then
  MsgBox "You Must Input a Job Number!", vbInformation, "HEY FOOL!"
Me.job.SetFocus
  Cancel = True
  
Else:
  
End If

I've tried it in lostfocus, beforeupdate, afterupdate and I've even tried changing the text box to a combo but nowt works
 
Sorted that one.

I put the following in the nest field along after 'job'

Code:
Private Sub serial_GotFocus()
If IsNull(Me.job) Then
  MsgBox "You Must Input a Job Number!", vbInformation, "HEY FOOL!"
Me.job.SetFocus
  Cancel = True
  
Else:
  
End If

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top