Check to see if an item from a combobox has been selected

dcjones

Dereck
Local time
Today, 04:05
Joined
Mar 10, 2004
Messages
108
Hi all.

I have a Combobox on a form, What I am trying to do is before the record is saved check that a selection has been made. What I have so far is:-

Private Sub Command17_Click()
On Error GoTo Err_Command17_Click



DoCmd.Save
If IsNull(Combo2) Then
MsgBox "You must select a supplier, If the job is in house, select in house"
End If
DoCmd.Close
Forms!frmMainClient![frmJob].Form.Requery


Exit_Command17_Click:
Exit Sub

Err_Command17_Click:
MsgBox Err.Description
Resume Exit_Command17_Click

End Sub

Many thanks for any help.

Kind regards
 
Last edited:
Code:
If IsNull(Combo2) Then
   MsgBox "You must select a supplier. If the job is In House, select In House."
   Exit Sub
Else
   DoCmd.RunCommand.acCommandSaveRecord
   Forms!frmMainClient![frmJob].Form.Requery
End If
 
Hi.

I have tried to use your reply (thanks you for that).

I did forget to say that the combobox is on a subform.
This is what I have so far:-

Private Sub Command17_Click()
On Error GoTo Err_Command17_Click

DoCmd.GoToControl Forms![frmSupplierCombo]![Combo2]
If IsNull(Combo2) Then
MsgBox "You must select a supplier. If the job is In House, select In House."
Exit Sub
Else
DoCmd.Save
DoCmd.Close
Forms!frmMainClient![frmJob].Form.Requery
End If

Exit_Command17_Click:
Exit Sub

Err_Command17_Click:
MsgBox Err.Description
Resume Exit_Command17_Click

End Sub

When I run this code I get an error message saying that the "frmSupplierCombo" form cannot be found.

Any ideas.

Many thanks
 
Code:
If IsNull(Forms![frmsuppliercombo]![Combo2]) Then
   MsgBox "You must select a supplier. If the job is In House, select In House."
   Exit Sub
Else
   DoCmd.RunCommand.acCommandSaveRecord
   Forms!frmMainClient![frmJob].Form.Requery
End If

Instead DoCmd.GoToControl, try just referencing the control through it's subform directly from the test for null.
 
Hi

POST RESOLVED

I used the following code:-

Private Sub Command17_Click()
On Error GoTo Err_Command17_Click




If IsNull(frmSupplierCombo.Form.Combo2) Then
MsgBox "You must select a supplier. If the job is In House, select In House."
Exit Sub
Else
DoCmd.Save
DoCmd.Close
Forms!frmMainClient![frmJob].Form.Requery
End If

Forms!frmMainClient.Form.Requery

Exit_Command17_Click:
Exit Sub

Err_Command17_Click:
MsgBox Err.Description
Resume Exit_Command17_Click

End Sub

Many thansk for your help.
 
Hi

POST RESOLVED

I used the following code:-

Private Sub Command17_Click()
On Error GoTo Err_Command17_Click




If IsNull(frmSupplierCombo.Form.Combo2) Then
MsgBox "You must select a supplier. If the job is In House, select In House."
Exit Sub
Else
DoCmd.Save
DoCmd.Close
Forms!frmMainClient![frmJob].Form.Requery
End If

Forms!frmMainClient.Form.Requery

Exit_Command17_Click:
Exit Sub

Err_Command17_Click:
MsgBox Err.Description
Resume Exit_Command17_Click

End Sub

Many thanks for your help.
 
I would have thought that you need to do the checking in the "BeforeUpdate" event of the subform rather than behind a Save button. Once a record has lost focus on a subform then surely it is saved anyway.
 
Fear Naught said:
I would have thought that you need to do the checking in the "BeforeUpdate" event of the subform rather than behind a Save button. Once a record has lost focus on a subform then surely it is saved anyway.
Not only that but DoCmd.Save is also incorrect
 
Hi All.

O.K. maybe not RESOLVED.

I have now used "DoCmd.RunCommand.acCommandSaveRecord" but when the script is run it gives an error "Argument not optional". The complete code looks like this:-

Private Sub Command12_Click()
On Error GoTo Err_Command17_Click
If IsNull(frmSupplierCombo.Form.Combo2) Then
MsgBox "You must select a supplier. If the job is In House, select In House."
Exit Sub
Else
DoCmd.RunCommand.acCommandSaveRecord

DoCmd.Close
Forms!frmMainClient![frmJob].Form.Requery
End If

Forms!frmMainClient.Form.Requery

Exit_Command17_Click:
Exit Sub

Err_Command17_Click:
MsgBox Err.Description
Resume Exit_Command17_Click

End Sub.

Can anyone see what is causing the error.

Many thanks
 

Users who are viewing this thread

Back
Top Bottom