Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-03-16
7
Medium Priority
?
1,031 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 300 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 58

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 600 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 60

Assisted Solution

by:HainKurt
HainKurt earned 100 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
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.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

604 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