Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

InputBox

Status
Not open for further replies.

ChewDoggie

Programmer
Mar 14, 2005
604
US
Hello All !

Does anyone know how to capture when the user presses the "Cancel" button on an InputBox ?

Thanks !


AMACycle

American Motorcyclist Association
 
According to the help files...

If the user clicks Cancel, the function returns a zero-length string ("").[/qoute]

This is why I never use it. Instead, I prefer to create my own input box.



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
If the user clicks Cancel, the function returns a zero-length string ("").


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
This is what I use. To use it yourself, you should copy/paste the code to Notepad and then save it to a file named frmSingleEdit.frm

Then, include this form in to your project.

Code:
VERSION 5.00
Begin VB.Form frmSingleEdit 
   BorderStyle     =   3  'Fixed Dialog
   Caption         =   "Form1"
   ClientHeight    =   1440
   ClientLeft      =   45
   ClientTop       =   435
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   1440
   ScaleWidth      =   4680
   ShowInTaskbar   =   0   'False
   StartUpPosition =   2  'CenterScreen
   Begin VB.CommandButton btnCancel 
      Cancel          =   -1  'True
      Caption         =   "&Cancel"
      Height          =   495
      Left            =   2400
      TabIndex        =   2
      Top             =   720
      Width           =   1215
   End
   Begin VB.CommandButton btnOK 
      Caption         =   "&OK"
      Default         =   -1  'True
      Height          =   495
      Left            =   960
      TabIndex        =   1
      Top             =   720
      Width           =   1215
   End
   Begin VB.TextBox txtData 
      Height          =   285
      Left            =   240
      TabIndex        =   0
      Top             =   240
      Width           =   4215
   End
End
Attribute VB_Name = "frmSingleEdit"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private bCancel As Boolean
Private cData As String

Public Sub GetSingleEditWithMaxLength(ByVal Caption As String, ByVal MaxLength As Long, ByRef Data As String, ByRef Cancel As Boolean)

    Me.Caption = Caption
    bCancel = True
    txtData.Text = Data
    txtData.SelStart = 0
    txtData.SelLength = Len(txtData.Text)
    txtData.MaxLength = MaxLength
    
    Call Show(vbModal)
    Data = cData
    Cancel = bCancel
    
End Sub

Public Sub GetSingleEdit(ByVal Caption As String, ByRef Data As String, ByRef Cancel As Boolean)
    
    Me.Caption = Caption
    bCancel = True
    txtData.Text = Data
    txtData.SelStart = 0
    txtData.SelLength = Len(txtData.Text)
    
    Call Show(vbModal)
    Data = cData
    Cancel = bCancel
    
End Sub

Private Sub btnCancel_Click()
    
    Unload Me
    
End Sub

Private Sub btnOK_Click()
    
    cData = txtData.Text
    bCancel = False
    
    Unload Me
    
End Sub

To use it...

Code:
Private Sub Command1_Click()
    
    Dim Data As String
    Dim Cancel As Boolean
    
    Call frmSingleEdit.GetSingleEdit("This is the caption", Data, Cancel)
    
    If Cancel Then
        MsgBox "User clicked cancel"
    Else
        MsgBox "User Typed" & vbCrLf & vbCrLf & Data
    End If
    
End Sub

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I get so embarassed when replies begin with, "According to the help files..." :)

You're right, returning an empty string doesn't tell you much of anything. But in my case, it's useful b/c the user is entering the Sheet Name in their Excel file. So, if SheetName = "", then I just cancel the request.

But I'm STILL going to use your code, George. I can find several other instances in my project where an empty string isn't useful.

Thanks much !




AMACycle

American Motorcyclist Association
 
Try this:

Code:
Private Sub Command1_Click()
    Dim SheetName As String
    SheetName = InputBox("Enter the Excel sheet name")
    If StrPtr(SheetName) = 0 Then
        MsgBox "User pressed cancel!", vbInformation + vbOKOnly
    ElseIf Len(Trim$(SheetName)) = 0 Then
        MsgBox "You must enter an Excel sheet name!", vbInformation + vbOKOnly
    Else
        MsgBox "Your Excel sheet name is: " & SheetName, vbInformation, vbOKOnly
    End If
End Sub

Swi
 
Code:
Dim s As String

s = InputBox("foo")
If StrPtr(s) = 0 Then
    MsgBox "cancel"
Else
    MsgBox "ok: [" & s & "]"
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top