Solved

VB input box query

Posted on 2002-07-11
8
323 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 143

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
Industry Leaders: 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!

 
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
 

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

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!

Question has a verified solution.

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

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

740 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