Solved

Message box if combobox is null

Posted on 2013-05-29
10
1,073 Views
Last Modified: 2013-05-29
Trying to make a msgbox appear if a combobox is null.  Here's my code...  I've tried it in beforeupdate and afterupdate but it doesn't work.

    If IsNull(Me![cboProjectName]) Then
        MsgBox "There are no new projects."
    End If
0
Comment
Question by:SteveL13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 29

Expert Comment

by:IrogSinta
ID: 39204663
Try putting it in the OnCurrent event.  
This may work better too:
If Me![cboProjectName] & "" = "" Then

Open in new window

0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39204674
the beforeupdate and afterupdate event of the control will only happen if;
- you update the data by entering  or deleting a character then moving on to another control.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39204679
After Update and Before Update don't run unless the user actually edits the field.  If you want to trap "no data entry", you can place your code in the Before Exit Event of the control and set that control as the first tab stop (not my favorite UI behavior, but this will keep the focus locked on that control until data is entered):

Private Sub cboProjectName_BeforeExit(Cancel as Integer)
   If IsNull(Me![cboProjectName]) Then
        MsgBox "There are no new projects."
        Cancel = true
    End If
End Sub

Open in new window

0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39204823
I generally don't like to use the BeforeExit event of controls, because that forces users to perform their data entry in a specific order, which is somewhat contrary to the whole "windows UI concept".

I generally put these types of tests in the Click event of an button designed to perform some action, either the Save the record or some other action.  I also like to use the Form_BeforeUpdate event, which can be cancelled if a required field is missing a value.

It really depends on how your form is organized, is it a data entry form (if so, a "Save" button or the Form_BeforeUpdate event are probably best)?  If is is a form that allows you to generate reports or something like that, then it probably is unbound and a command button which performs some action would probably be best.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39204848
Steve,

<< MsgBox "There are no new projects." >>

The wording of your message is a little unusual.  Are you referring to the textbox portion of your combo - indicating that the user has not entered anything, or are you trying to indicate that there are no selections available in the drop-down list?

If Me.cboProjectName.ListCount = 0 Then
    MsgBox "There are no new projects." 
End If

Open in new window

0
 

Author Comment

by:SteveL13
ID: 39205796
That there are no selections available in the drop-down list.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39205814
then you can't use any of the event you mentioned.. perhaps the form's current event

private sub form_current()

if me.cboProjectName.listcount= 0 then
   MsgBox "No Project"
end if


end sub
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 39205860
Steve,

See my comment at http:#a39204848...

The On Current event that cap mentioned is a good place if you want this to be checked every time the user changes records or opens the form.

However, if you want it to be in response to some user action... place it in whatever event is actually changing the rowsource of the combo.

For example if you have code filtering that combo in some other control's After Update event, place that code AFTER filtering the combo in the same After Update event.   This may be VBA that sets the rowsource property... or it may simply be a requery statement...

So you may need something like this:

Me.cboProjectName.rowsource = ... etc
If Me.cboProjectName.ListCount = 0 Then
    MsgBox "There are no new projects." 
End If

Open in new window


Or this:

Me.cboProjectName.Requery
If Me.cboProjectName.ListCount = 0 Then
    MsgBox "There are no new projects." 
End If

Open in new window

0
 

Author Comment

by:SteveL13
ID: 39206170
I put your code in the ongotfocus event and it worked.... (THANKS!)

Me.cboProjectName.Requery
If Me.cboProjectName.ListCount = 0 Then
    MsgBox "There are no new projects."
End If
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39206193
Steve,

Glad to help out.

Just a note -  if you are using the GotFocus event to trigger this, that Me.requery is probably not needed.

I'd assume you already have the requery or something similar elsewhere in your existing code if your combo box is being filtered.

If Me.cboProjectName.ListCount = 0 Then
    MsgBox "There are no new projects." 
End If

Open in new window


These three lines are meant to be a supplement to whatever is filtering your combo box.  The location of the code is up to you -- based on what actions should trigger it (and the gotfocus or enter events are of course good choices if it responds the way you want :)  )
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question