Solved

Access 2007 Select Case not trapping Null or Empty values in Textbox

Posted on 2011-03-16
7
991 Views
Last Modified: 2012-06-27
I'm checking the value of a text box on exit with a Select statment. I've tried several different methods but can't seem to find the correct way to trap a null or empty condtion.
I've tried these but they did not work for me:

Case Is = Empty
Case Is = ""
Case Null
Case Is = Null, ""

Code ran fine but everytime my Case Else statement was triggered and not any of the other cases. I thought this might be due to the text box being bound to my primary key field ("new"?)so changed it to unbound with the same result. I found an example of using an If  Isnull statement prior to the Select that works (I left it in my code sample)  but it sure seems like there would be a way for Select Case to natively deal with nulls and zero length strings. TIA
-Ralph
Private Sub tbPOID_Exit(Cancel As Integer)
   Dim strMsg, iResponse As String
   
   If IsNull(Me.tbPOID) Or Me.tbPOID = "" Then
      Me.tbPOID = "Null"
   End If

   Select Case Me.tbPOID
   Case "Null"
      MsgBox ("Null or Empty")
      Me.tbPOID = "P"
      Me.cbCompany.SetFocus
   Case "P"
      Me.cbCompany.SetFocus
   Case Else
      strMsg = "You entered '" & Me.tbPOID & "' as a new Order." & _
      vbCrLf & "Is this correct?"
      iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "New Order?")
      If iResponse = vbNo Then
         Cancel = True         ' Cancel exit.
         Exit Sub
      Else
         Exit Sub              ' Save and continue.
      End If
   End Select
  
End Sub

Open in new window

0
Comment
Question by:Raland9966
7 Comments
 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Access MVP)
DatabaseMX (Joe Anderson - Access MVP) earned 75 total points
ID: 35151222
Try this mod:

   Dim strMsg, iResponse As String
   
   If Nz(Me.tbPOID , "") = "" Then
      MsgBox ("Null or Empty")
      Me.tbPOID = "P"                           ' why do you have this here ?
      Me.cbCompany.SetFocus
      Exit Sub
   End If

   Select Case Me.tbPOID

   Case "P"
      Me.cbCompany.SetFocus
   Case Else
      strMsg = "You entered '" & Me.tbPOID & "' as a new Order." & _
      vbCrLf & "Is this correct?"
      iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "New Order?")
      If iResponse = vbNo Then
         Cancel = True         ' Cancel exit.
         Exit Sub
      Else
         Exit Sub              ' Save and continue.
      End If
   End Select


mx
0
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 150 total points
ID: 35151230
Change this:

   Select Case Me.tbPOID

  To

   Select Case NZ(Me.tbPOID,"")

   Case ""
 
   Case "P"

   Case Else

  End Select

JimD.
0
 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 25 total points
ID: 35151289
try this:

If IsNull(Me.tbPOID) Or Me.tbPOID = "" Then
      Me.tbPOID = "Null"
   End If

   Select Case Me.tbPOID
   Case "Null"
   ...
-->

   Select Case Me.tbPOID
   Case ""
   ...
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:Raland9966
ID: 35152203
Thanks everyone. Quick and helpful answers all around.

@DatabaseMX & JDettman
Both your solutions work equally well in different ways, I like DatabaseMX's way of doing the actual work in the IF statement rather than just using it to modify the text box value.  JDettman's is cleaner since it eliminates the If statement completely but in a way I like the idea of dealing with "non values" outside the Select case decision making.  

@HainKurt.
I had it working that way after I found an example on the internet using the If statement to modify the textbox value, I just changed the Case statement back to one that didnt't work when I posted it here.

I'm going to award everyone points with JDettman being the hghest since his solution best fit my question of how to make the Select Case work without any additional code.
Does it seem strange  the Select statement works when you manually set it to Empty aka "" verses when it actually is Empty?

@DatabaseMX  
tbPOID does several different fuctions that aren't all in the code example. If the user is leavling tbPOID blank they must be creating a new record and the "P" is a placeholder for a new record number. My own autonumber function more or less.

0
 
LVL 75
ID: 35152247
"but in a way I like the idea of dealing with "non values" outside the Select case decision making.  "

Which have found is always cleaner and easier to troubleshoot ... which is why I posted the slightly longer initial   IF / End IF

mx
0
 
LVL 75
ID: 35152252
btw ... I don't think you would want to do this:

Me.tbPOID = "Null"

because that is *not* setting the value to Null .... instead to a String that is called Null >> "Null"

Surely not what you intended ...

mx
0
 

Author Comment

by:Raland9966
ID: 35156531
>>btw ... I don't think you would want to do this:
>>Me.tbPOID = "Null"

Yeah I agree. That is from the code I found elsewhere. It used the IF to changed the texbox value to "Null" and then the Select statement had a Case "Null". It worked and you couldn't see "Null" on the screen but it was a little to much of a hack for my taste.

Very true aboiut the troubleshooting. I may well end up using your solution even though the case statement only way is more compact. Wish EE would allow you to assign more than the set amount of points if you are accepting more than one solutions. thanks again!

0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can you open the FORM2 2 32
Dcount using a date in a table compared to today's date 3 31
Access 2003 query lost it's only join 7 27
Criteria for Date for DCount 4 26
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
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…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

831 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