Jonathan
Tue Jun 17 04:26:14 PDT 2008
"Gordon Bentley-Mix" <gordon(dot)bentleymix(at)gmail(dot)com> wrote in
message news:64F4D1A3-DA06-4D28-8B79-2B995B7F7410@microsoft.com...
> Ann,
>
> You can use either
>
> Len(txtDeductibleCreditsStartDate.Value) > 0
>
> or
>
> txtDeductibleCreditsStartDate.Value <> ""
>
> The result is the same. In the first instance, you are checking to see if
> the length (Len) of the value is greater than zero - which it would be if
> it
> wasn't blank. In the second instance, you are checking to see if the value
> is
> not equal to nothing (""). Which one to use is more a matter of personal
> preference - although I prefer the latter because it's a direct evaluation
> rather than a 'functional' one.
Compared to the length of time spent executing any command which accesses
the Word object model, the difference in speed is trivial, but in fact the
Len method is faster.
In order to understand why, it is necessary to consider how strings are
stored in VB & VBA. Unlike in C/C++, where is string is a sequence of bytes
terminated with a null character, a VBA HLSTR structure consists of a 4-byte
value which specifies the length of the string, followed by the string's
contents. (For those of you who have tried using the Windows API functions
and been baffled by the convolutions you have to go through in order to get
strings out of API functions, this diifference in structure is the reason
why)
To get the length of a VB string, all that need be done is to read the value
of the 4-byte header. To compare the string with another string, both
strings must be pushed onto the stack, then their headers compared, and
then (if they are equal) a character-by-character comparison made. it is
obvious which will be the quicker process.
--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
>
> However, in the structure of your code you would proably want to use
>
> Len(txtDeductibleCreditsStartDate.Value) = 0
>
> or
>
> txtDeductibleCreditsStartDate.Value = ""
>
> You can also simplify your code as follows:
>
> Private Sub txtDeductibleCreditsStartDate_Exit(ByVal Cancel As
> MSForms.ReturnBoolean)
> If optMMOGroupYes.Value = True And optDeductibleCreditsYes.Value = True
> Then
> If txtDeductibleCreditsStartDate.Value = "" Then
> MsgBox "The Deductible Credits Start and" _
> & vbCrLf & "End Dates must be entered."
> Cancel = True
> End If
> End If
> End Sub
>
> A bit of explanation on the above:
> * The nested 'If' statements are a bit more efficient in that you only
> need
> to check the value of the txtDeductibleCreditsStartDate TextBox if both of
> the option buttons have been selected.
> * You only need one 'If' statement to check the value of the TextBox
> because there are only two possibilities: either it's blank or it's not.
> The
> 'ElseIf' statement is redundant.
> * The default value of the Cancel argument is 'False', so you can get
> away
> with setting it to 'True' only if you need to - which you would do only if
> the value of the TextBox is blank. (However, I often set it explicitly as
> the
> first line of the procedure just as a reminder: Cancel = False. And in
> some
> more complex instances I have been known to use a Function that returns a
> Boolean value to set the Cancel argument - usually when the TextBox is in
> a
> Frame and I want to validate the TextBox on the Frame Exit event as
> well...
> EEK!)
>
> Finally, I don't know why that website would 'Null' in Word VBA examples;
> I've never seen it, and a search of the VBA help only returned 3 topics -
> none of which would be applicable to your situation...
> --
> Cheers!
> Gordon
> The Kiwi Koder
>
> Uninvited email contact will be marked as SPAM and ignored. Please post
> all
> follow-ups to the newsgroup.
>
>
> "Ann" wrote:
>
>> I'm in the correct group. I am creating a User Form. I copied code from
>>
http://www.fontstuff.com/vba/vbatut09pfv.htm and the use of Null was
>> repeatedly used. I don't know why it didn't work and if
>> Len(txtDeductibleCreditsStartDate.Value) > 0 is correct then I don't know
>> why using "" (empty quotation marks) did work, but they did. It makes it
>> extremely difficult when each application does it differently. I do
>> appreciate your help though and will try the
>> Len(txtDeductibleCreditsStartDate.Value) > 0 code if that is what is
>> correct.
>> Thanks.
>