PC Review


Reply
Thread Tools Rate Thread

Cancel = True

 
 
Sam Kuo
Guest
Posts: n/a
 
      9th Jun 2008
I tried to use Cancel = Ture (as shown below) to ensure the user inputs a
value in 10 textboxes (namely txtCatchment1, txtCatchment2, ...,
txtCatchment10).
But my attempt returns an error at the line "Cancel=True". How should I fix
this?

' Below is in UserForm1
' (duplicate 10 times with the number changed to match the textbox name)
Private Sub txtCatchment1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
CheckInput(1)
End Sub

' Below is in Module1
Sub CheckInput(CatchmentNo As Long)
Dim strCatchmentNo As String
With UserForm1
strCatchmentNo = .Controls("txtCatchment" & CatchmentNo).Value
If Len(strCatchmentNo) > 0 Then
Else
Cancel = True
MsgBox "You must enter a value",
vbOKOnly + vbExclamation, "Entry Required"
End If
End With
End Sub
 
Reply With Quote
 
 
 
 
Tim Williams
Guest
Posts: n/a
 
      9th Jun 2008
You're setting Cancel in a sub which has no reference to it: it's not a
global variable.

Tim

"Sam Kuo" <(E-Mail Removed)> wrote in message
news:1593F33F-CF7A-4F43-B378-(E-Mail Removed)...
>I tried to use Cancel = Ture (as shown below) to ensure the user inputs a
> value in 10 textboxes (namely txtCatchment1, txtCatchment2, ...,
> txtCatchment10).
> But my attempt returns an error at the line "Cancel=True". How should I
> fix
> this?
>
> ' Below is in UserForm1
> ' (duplicate 10 times with the number changed to match the textbox name)
> Private Sub txtCatchment1_BeforeUpdate(ByVal Cancel As
> MSForms.ReturnBoolean)
> CheckInput(1)
> End Sub
>
> ' Below is in Module1
> Sub CheckInput(CatchmentNo As Long)
> Dim strCatchmentNo As String
> With UserForm1
> strCatchmentNo = .Controls("txtCatchment" & CatchmentNo).Value
> If Len(strCatchmentNo) > 0 Then
> Else
> Cancel = True
> MsgBox "You must enter a value",
> vbOKOnly + vbExclamation, "Entry Required"
> End If
> End With
> End Sub



 
Reply With Quote
 
Sam Kuo
Guest
Posts: n/a
 
      9th Jun 2008
Hi Tim
Thanks for your explantion.
I've now have the Cancel in the private sub and it works fine. But I just
wonder if there is a shorter way to achieve what I want to do, without having
to manually duplicate the code 10 times? Thanks in advance.

' Below is in UserForm1
' (duplicate 10 times with the number changed to match the textbox name)
Private Sub txtCatchment1_BeforeUpdate(ByVal Cancel As
MSForms.ReturnBoolean)
Dim strCatchmentNo As String
Const CatchmentNo As Long = 1
With UserForm1
strCatchmentNo = .Controls("txtCatchment" & CatchmentNo).Value
If Len(strCatchmentNo) > 0 Then
Else
Cancel = True
MsgBox "You must enter a value",
vbOKOnly + vbExclamation, "Entry Required"
End If
End With
End Sub


"Tim Williams" wrote:

> You're setting Cancel in a sub which has no reference to it: it's not a
> global variable.
>
> Tim
>
> "Sam Kuo" <(E-Mail Removed)> wrote in message
> news:1593F33F-CF7A-4F43-B378-(E-Mail Removed)...
> >I tried to use Cancel = Ture (as shown below) to ensure the user inputs a
> > value in 10 textboxes (namely txtCatchment1, txtCatchment2, ...,
> > txtCatchment10).
> > But my attempt returns an error at the line "Cancel=True". How should I
> > fix
> > this?
> >
> > ' Below is in UserForm1
> > ' (duplicate 10 times with the number changed to match the textbox name)
> > Private Sub txtCatchment1_BeforeUpdate(ByVal Cancel As
> > MSForms.ReturnBoolean)
> > CheckInput(1)
> > End Sub
> >
> > ' Below is in Module1
> > Sub CheckInput(CatchmentNo As Long)
> > Dim strCatchmentNo As String
> > With UserForm1
> > strCatchmentNo = .Controls("txtCatchment" & CatchmentNo).Value
> > If Len(strCatchmentNo) > 0 Then
> > Else
> > Cancel = True
> > MsgBox "You must enter a value",
> > vbOKOnly + vbExclamation, "Entry Required"
> > End If
> > End With
> > End Sub

>
>
>

 
Reply With Quote
 
Tim Williams
Guest
Posts: n/a
 
      10th Jun 2008
Something like this ?

'********************************************
Private Sub txt1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Checkit 1, Cancel
End Sub

Private Sub Checkit(num As Integer, ByRef Cancel As MSForms.ReturnBoolean)

Dim strCatchmentNo As String
With UserForm1
strCatchmentNo = .Controls("txt" & num).Value
If Len(strCatchmentNo) > 0 Then
Else
Cancel = True
MsgBox "You must enter a value"
End If
End With

End Sub
'*********************************************
However, from a usability perpective this can be frustrating for users: it
may be better to have a single validation process which runs (eg) when your
user clicks "OK". Eg. what should they enter if they don't know what to
enter and have to go off and look it up ? With this model they're stuck in
that particular textbox unless they enter a value.

Tim



"Sam Kuo" <(E-Mail Removed)> wrote in message
news:EF2AA225-B0CF-484F-B77A-(E-Mail Removed)...
> Hi Tim
> Thanks for your explantion.
> I've now have the Cancel in the private sub and it works fine. But I just
> wonder if there is a shorter way to achieve what I want to do, without
> having
> to manually duplicate the code 10 times? Thanks in advance.
>
> ' Below is in UserForm1
> ' (duplicate 10 times with the number changed to match the textbox name)
> Private Sub txtCatchment1_BeforeUpdate(ByVal Cancel As
> MSForms.ReturnBoolean)
> Dim strCatchmentNo As String
> Const CatchmentNo As Long = 1
> With UserForm1
> strCatchmentNo = .Controls("txtCatchment" & CatchmentNo).Value
> If Len(strCatchmentNo) > 0 Then
> Else
> Cancel = True
> MsgBox "You must enter a value",
> vbOKOnly + vbExclamation, "Entry Required"
> End If
> End With
> End Sub
>
>
> "Tim Williams" wrote:
>
>> You're setting Cancel in a sub which has no reference to it: it's not a
>> global variable.
>>
>> Tim
>>
>> "Sam Kuo" <(E-Mail Removed)> wrote in message
>> news:1593F33F-CF7A-4F43-B378-(E-Mail Removed)...
>> >I tried to use Cancel = Ture (as shown below) to ensure the user inputs
>> >a
>> > value in 10 textboxes (namely txtCatchment1, txtCatchment2, ...,
>> > txtCatchment10).
>> > But my attempt returns an error at the line "Cancel=True". How should I
>> > fix
>> > this?
>> >
>> > ' Below is in UserForm1
>> > ' (duplicate 10 times with the number changed to match the textbox
>> > name)
>> > Private Sub txtCatchment1_BeforeUpdate(ByVal Cancel As
>> > MSForms.ReturnBoolean)
>> > CheckInput(1)
>> > End Sub
>> >
>> > ' Below is in Module1
>> > Sub CheckInput(CatchmentNo As Long)
>> > Dim strCatchmentNo As String
>> > With UserForm1
>> > strCatchmentNo = .Controls("txtCatchment" & CatchmentNo).Value
>> > If Len(strCatchmentNo) > 0 Then
>> > Else
>> > Cancel = True
>> > MsgBox "You must enter a value",
>> > vbOKOnly + vbExclamation, "Entry Required"
>> > End If
>> > End With
>> > End Sub

>>
>>
>>



 
Reply With Quote
 
Sam Kuo
Guest
Posts: n/a
 
      12th Jun 2008
Thanks Tim.
Your comment on the usability issue is also worth thinking. Thanks :-)
Sam

"Tim Williams" wrote:

> Something like this ?
>
> '********************************************
> Private Sub txt1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
> Checkit 1, Cancel
> End Sub
>
> Private Sub Checkit(num As Integer, ByRef Cancel As MSForms.ReturnBoolean)
>
> Dim strCatchmentNo As String
> With UserForm1
> strCatchmentNo = .Controls("txt" & num).Value
> If Len(strCatchmentNo) > 0 Then
> Else
> Cancel = True
> MsgBox "You must enter a value"
> End If
> End With
>
> End Sub
> '*********************************************
> However, from a usability perpective this can be frustrating for users: it
> may be better to have a single validation process which runs (eg) when your
> user clicks "OK". Eg. what should they enter if they don't know what to
> enter and have to go off and look it up ? With this model they're stuck in
> that particular textbox unless they enter a value.
>
> Tim
>
>
>
> "Sam Kuo" <(E-Mail Removed)> wrote in message
> news:EF2AA225-B0CF-484F-B77A-(E-Mail Removed)...
> > Hi Tim
> > Thanks for your explantion.
> > I've now have the Cancel in the private sub and it works fine. But I just
> > wonder if there is a shorter way to achieve what I want to do, without
> > having
> > to manually duplicate the code 10 times? Thanks in advance.
> >
> > ' Below is in UserForm1
> > ' (duplicate 10 times with the number changed to match the textbox name)
> > Private Sub txtCatchment1_BeforeUpdate(ByVal Cancel As
> > MSForms.ReturnBoolean)
> > Dim strCatchmentNo As String
> > Const CatchmentNo As Long = 1
> > With UserForm1
> > strCatchmentNo = .Controls("txtCatchment" & CatchmentNo).Value
> > If Len(strCatchmentNo) > 0 Then
> > Else
> > Cancel = True
> > MsgBox "You must enter a value",
> > vbOKOnly + vbExclamation, "Entry Required"
> > End If
> > End With
> > End Sub
> >
> >
> > "Tim Williams" wrote:
> >
> >> You're setting Cancel in a sub which has no reference to it: it's not a
> >> global variable.
> >>
> >> Tim
> >>
> >> "Sam Kuo" <(E-Mail Removed)> wrote in message
> >> news:1593F33F-CF7A-4F43-B378-(E-Mail Removed)...
> >> >I tried to use Cancel = Ture (as shown below) to ensure the user inputs
> >> >a
> >> > value in 10 textboxes (namely txtCatchment1, txtCatchment2, ...,
> >> > txtCatchment10).
> >> > But my attempt returns an error at the line "Cancel=True". How should I
> >> > fix
> >> > this?
> >> >
> >> > ' Below is in UserForm1
> >> > ' (duplicate 10 times with the number changed to match the textbox
> >> > name)
> >> > Private Sub txtCatchment1_BeforeUpdate(ByVal Cancel As
> >> > MSForms.ReturnBoolean)
> >> > CheckInput(1)
> >> > End Sub
> >> >
> >> > ' Below is in Module1
> >> > Sub CheckInput(CatchmentNo As Long)
> >> > Dim strCatchmentNo As String
> >> > With UserForm1
> >> > strCatchmentNo = .Controls("txtCatchment" & CatchmentNo).Value
> >> > If Len(strCatchmentNo) > 0 Then
> >> > Else
> >> > Cancel = True
> >> > MsgBox "You must enter a value",
> >> > vbOKOnly + vbExclamation, "Entry Required"
> >> > End If
> >> > End With
> >> > End Sub
> >>
> >>
> >>

>
>
>

 
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
CancelEventArgs.Cancel - How Event Raising Code Know if Client Set .Cancel=true Smithers Microsoft C# .NET 3 24th Sep 2007 02:30 AM
Cancel = true Stapes Microsoft Access 1 18th Jul 2007 04:48 PM
Cancel = True Stapes Microsoft Access 1 18th Jul 2007 04:45 PM
Cancel = true Stapes Microsoft Access 0 18th Jul 2007 04:30 PM
Cancel = True =?Utf-8?B?SnVzdEJyZWF0aGU=?= Microsoft Excel Programming 4 9th Apr 2007 04:02 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:46 AM.