Solved

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

Posted on 2011-03-16
7
972 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
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 functions 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 Microsoft Ac…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

743 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

12 Experts available now in Live!

Get 1:1 Help Now