Solved

Message box if combobox is null

Posted on 2013-05-29
10
949 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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.

708 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

13 Experts available now in Live!

Get 1:1 Help Now