Solved

Message box if combobox is null

Posted on 2013-05-29
10
985 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
  • 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 119

Expert Comment

by:Rey Obrero
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
 
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

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

Expert Comment

by:Rey Obrero
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now