Visual Basic Helper Routines
StrPtr: Differentiate Between Cancel, OK and No Value in an InputBox
     
Posted:   Thursday August 15, 2002
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   None
Author:   Bill McCarthy, Mattias Sjögren, Mark Hurd
       

Related:  

Nothing, thank gawd.
     
 Prerequisites
A propensity for using a truly poor interface.

Battles are fought daily in VB newsgroups - SDI environment vs. MDI environment, classes vs. modules, .net vs. .classic, VB vs. Delphi, and so on.  But mention InputBox - the dreaded first refuge of the newbie and last refuge of the desperate - and the consensus among VB devs is ceremoniously unanimous - it sucks.

The warped and perverted soul responsible for the logic giving digital birth to this "not-user-friendly interface" will eternally remain possibly one of Microsoft's most well-guarded secrets, for obvious personal health reasons. But when the desperate (or newbie) must use this built-in atrocity, there is at least one thing we can do to make its inclusion in our otherwise well-crafted applications somewhat more palatable.

Now, why someone would actually need to differentiate between whether an InputBox Cancel was pressed, or whether OK was pressed with no string entered, frankly escapes me since the end result - no string to work with - is the same and can easily be handled by a few lines of code using Len(). But some apparently do, and as I neither fall into the newbie nor desperate camp I am obviously missing the obvious. I suppose. But I digress.

Here then is a quick snip of code that can differentiate between the Cancel being pressed and simply not entering any data and pressing OK. VB5 and VB6 users can use this out-of-the-box, so to speak, while VB4-32 users will need to add a API declare for StrPtr ( Private Declare Function StrPtr Lib "vb40032.dll" Alias "VarPrt" (pAny As Any) As Long). In VB6 and earlier, StrPtr() is the only way to differentiate "" and vbNullString. (.Net handles strings differently, and we won't even go there).

Let's blame Bill McCarthy for the initial newsgroup post pumping much-needed life into this dialog, and Mattias Sjögren and Mark Hurd for propagating its continued use!

 BAS Module Code
None.

 Form Code
To a form, add a command button (Command1). Be forewarned that since this displays an InputBox, you are advised to ensure no professional developers are in the vicinity when creating this demo. When the coast is clear, add the following code and quickly run:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Command1_Click()

   Dim sInput As String

   sInput = InputBox("Go ahead ... abuse me", _
                     "Dreaded InputBox Demo", _
                     "Some default text goes here")
   
   If StrPtr(sInput) = 0 Then
   
      MsgBox "Cancel was pressed"

   Else
   
      If Len(sInput) = 0 Then
         MsgBox "OK pressed but nothing entered."
      Else
         MsgBox "OK pressed: value= " & sInput
      End If  'Len(sInput)
   
   End If  'StrPtr

End Sub
 Comments

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter