Solved

Message box if combobox is null

Posted on 2013-05-29
10
1,035 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 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

765 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