Solved

VB input box query

Posted on 2002-07-11
8
312 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

747 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