PC Review


Reply
Thread Tools Rate Thread

Command button - check listbox contents before activating?

 
 
Roger on Excel
Guest
Posts: n/a
 
      3rd Dec 2009
[Excel 2003]

I have command buttons on the userform which access other userforms (which
drive the spreadsheet data entry)

I have a listbox on the userform displaying a number.

I would like to enter code into the command button activations such that if
the number in the listbox does not equal the number stored within the code
of the command button then a mesage box is displayed and the command button
does not activate its sub routines.

Can anyone help?

Thanks,

Roger
 
Reply With Quote
 
 
 
 
AB
Guest
Posts: n/a
 
      3rd Dec 2009
Something like this:

Option Explicit

Public Property Get MayProceed() As Boolean

Const MySecretNumber As Long = 7

With Me.ListBox1
If Not .Value = MySecretNumber Or .ListIndex = -1 Then
MayProceed = False
Else
MayProceed = True
End If
End With

End Property


Private Sub CommandButton1_Click()

If Not MayProceed Then
MsgBox "Uups - numbers don't match!!", vbExclamation
Exit Sub
End If

End Sub


On 3 Dec, 17:00, Roger on Excel
<RogeronEx...@discussions.microsoft.com> wrote:
> [Excel 2003]
>
> I have command buttons on the userform which access other userforms (which
> drive the spreadsheet data entry)
>
> I have a listbox on the userform displaying a number.
>
> I would like to enter code into the command button activations such that if
> the number in the listbox does not equal the *number stored within the code
> of the command button then a mesage box is displayed and the command button
> does not activate its sub routines.
>
> Can anyone help?
>
> Thanks,
>
> Roger


 
Reply With Quote
 
Roger on Excel
Guest
Posts: n/a
 
      4th Dec 2009
Hi,

Thanks for the code. Would you know how to make it work if the listbox
contents are a text string?

Roger

"AB" wrote:

> Something like this:
>
> Option Explicit
>
> Public Property Get MayProceed() As Boolean
>
> Const MySecretNumber As Long = 7
>
> With Me.ListBox1
> If Not .Value = MySecretNumber Or .ListIndex = -1 Then
> MayProceed = False
> Else
> MayProceed = True
> End If
> End With
>
> End Property
>
>
> Private Sub CommandButton1_Click()
>
> If Not MayProceed Then
> MsgBox "Uups - numbers don't match!!", vbExclamation
> Exit Sub
> End If
>
> End Sub
>
>
> On 3 Dec, 17:00, Roger on Excel
> <RogeronEx...@discussions.microsoft.com> wrote:
> > [Excel 2003]
> >
> > I have command buttons on the userform which access other userforms (which
> > drive the spreadsheet data entry)
> >
> > I have a listbox on the userform displaying a number.
> >
> > I would like to enter code into the command button activations such that if
> > the number in the listbox does not equal the number stored within the code
> > of the command button then a mesage box is displayed and the command button
> > does not activate its sub routines.
> >
> > Can anyone help?
> >
> > Thanks,
> >
> > Roger

>
> .
>

 
Reply With Quote
 
AB
Guest
Posts: n/a
 
      4th Dec 2009
Hi,

if the only difference is comparing strings instead of comparing
numbers then technically the only change in the code necessary would
be to dim the constant with a different type (string instead of Long)
like this:

replace this:
Const MySecretNumber As Long = 7

with this
Const MySecretNumber As String = "7"

Obviously the "7" can be anything you need (like "Seven" or "Apple"
or ...)

Post back if i misunderstood the question or didn't answer it.



On Dec 4, 12:40*am, Roger on Excel
<RogeronEx...@discussions.microsoft.com> wrote:
> Hi,
>
> Thanks for the code. *Would you know how to make it work if the listbox
> contents are a text string?
>
> Roger
>
>
>
> "AB" wrote:
> > Something like this:

>
> > Option Explicit

>
> > Public Property Get MayProceed() As Boolean

>
> > * * Const MySecretNumber As Long = 7

>
> > * * With Me.ListBox1
> > * * * * If Not .Value = MySecretNumber Or .ListIndex = -1 Then
> > * * * * * * MayProceed = False
> > * * * * Else
> > * * * * * * MayProceed = True
> > * * * * End If
> > * * End With

>
> > End Property

>
> > Private Sub CommandButton1_Click()

>
> > * * If Not MayProceed Then
> > * * * * MsgBox "Uups - numbers don't match!!", vbExclamation
> > * * * * Exit Sub
> > * * End If

>
> > End Sub

>
> > On 3 Dec, 17:00, Roger on Excel
> > <RogeronEx...@discussions.microsoft.com> wrote:
> > > [Excel 2003]

>
> > > I have command buttons on the userform which access other userforms (which
> > > drive the spreadsheet data entry)

>
> > > I have a listbox on the userform displaying a number.

>
> > > I would like to enter code into the command button activations such that if
> > > the number in the listbox does not equal the *number stored within the code
> > > of the command button then a mesage box is displayed and the command button
> > > does not activate its sub routines.

>
> > > Can anyone help?

>
> > > Thanks,

>
> > > Roger

>
> > .- Hide quoted text -

>
> - Show quoted text -


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Activating a Command Button on a Form Slej Microsoft Access Forms 2 15th Jan 2010 02:32 PM
activating a command button chuck Microsoft Excel Misc 1 10th Sep 2009 03:12 AM
Activating a command button with a single key stroke Bishop Microsoft Excel Programming 5 3rd Jun 2009 07:21 PM
command button wizard not activating Debbie S. Microsoft Access 3 28th Dec 2007 11:31 PM
Activating a command button after a certain date passes. =?Utf-8?B?Q2hyaXMgRS4=?= Microsoft Access Form Coding 3 31st Oct 2006 02:46 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:45 PM.