Solved

VB input box query

Posted on 2002-07-11
8
315 Views
Last Modified: 2010-07-27
how to determine whether a user has clicked the OK or CANCEL button in an input box in VB?
0
Comment
Question by:ganpat
8 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7145962
if the user clicks cancel, the function returns an empty string. Unfortunately, if the user clicks ok with empty field the same...
cheers
0
 
LVL 3

Expert Comment

by:sanjaykattimani
ID: 7145987
There is no 100% precise way to find whether Ok or cancel button was clicked. Following code works but fails if user removes the contents of the input box and click Ok.

If InputBox("Checking inputbox", "Sample Title", "DefalultValue") = "" Then
    MsgBox "Cancel clicked", vbInformation
Else
    MsgBox "Value entered", vbInformation
End If

Alternate is to use your own form which resembles input box and can works 100% as you wish.

Good luck
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7146692
Following previous comments, you could do this: (You ALWAYS need to check what value is returned)

Private Sub Form_Click()
Dim s As String

s = InputBox("Checking inputbox", "Sample Title", "DefalultValue")
If s <> "" And s <> "DefalultValue" Then
   ' what you need with string returned
   Debug.Print s
End If
End Sub
0
 
LVL 4

Accepted Solution

by:
Monchanger earned 50 total points
ID: 7147388
I found this snippet which was written by Bill McCarthy of Barongarook, Victoria, Australia :

Ascertain OK or Cancel From InputBox
When the user presses Cancel on a VB InputBox, the string
returned is a vbNullString. If the user inputs a zero-length string and presses OK, the return string is empty (""). Unfortunately, in VB, you can’t compare an empty string to vbNullString because VB equates "" to be equal to vbNullString even though the two are quite different.
However, you can use the undocumented StrPtr function to
determine whether the return string is indeed a vbNullString. A vbNullString’s pointer is, by definition, zero:

[I added to the code a little:]
Dim strReturn as String
strReturn = InputBox("Enter in a value")
If StrPtr(strReturn) = 0 Then
   msgbox "User pressed Cancel"
Elseif strReturn="" then
   msgbox "User entered blank string"
Else
   msgbox "User entered: " & strReturn
End If
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ganpat
ID: 7148291
Thanks a tonne!
0
 

Expert Comment

by:phanikishore
ID: 8604905
HI this is from kishore

this is my vb code

Dim a As Date
Dim b As Date

a = InputBox("Enter the Start Date", "Start Date")
b = InputBox("Enter the End Date", "End Date")

actually i want to generate a data report in vb based on date format i am giving two input boxes for taking starting date

and ending date my problem is the input box taking string and any date format so i want to restrict the input box with only

date format.

Regards
kishore
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8605715
You could check the return value with Isdate function and act accordingly.
0
 

Expert Comment

by:phanikishore
ID: 8611815
HI its fine for cheaking date

i am able to connect from vb to Oracle using ADODB1 Control but according present requirement

i want to connect to MsAccess and i the data should store in .mdb file ofter clicking add and

can any body help in this

connect VB with  Ms-Acess using ADODB1 Control

and how to add records

regards

kishroe.k
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to skip loop 6 55
Prevent checkbox click event occur while editing it in vb6 8 34
Hide vba in gp 7 83
Set email body to html using vbscript 6 28
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

896 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

11 Experts available now in Live!

Get 1:1 Help Now