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
Solved

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

Posted on 2011-03-16
7
993 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 - 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 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
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…
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…

809 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