Solved

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

Posted on 2011-03-16
7
1,008 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 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 58

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 55

Assisted Solution

by:Huseyin KAHRAMAN
Huseyin KAHRAMAN 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

690 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