VBA programmers aren’t that stupid, it the study material



  • Ok here is the deal, I’m a par time teacher for a medium consulting en education company.<o:p></o:p>

    And sorry to say, the only one in the company who will do VBA courses.<o:p></o:p>
    One of the courses I give is VBA in access, which can be really fun ;)

    <st1:GivenName w:st="on">Bud</st1:GivenName> recently we have gotten new material for access 2003 <o:p></o:p>

    At one part de teacher manually states that the variant type should be avoided. On which I must agree, sins I’m a programmer I can only see why.<o:p></o:p>
    So at the topic of the MsgBox in VBA it states the return type of the MsgBox is integer.<o:p></o:p>

    And that this should be stored in a variable of that type.<o:p></o:p>

    Now after explaining this to my student they go about making the cases the new material provided them.<o:p></o:p>
    And it has the following example of how the use the MsgBox:

    Private Sub btnClose_Click()

        Dim strResult, strTitle, strBody As String

        strTitle = "Question"
        strBody = "Are you sure you would like quite?"

        strResult = MsgBox(strBody, vbYesNo & vbQuestion, strTitle)  

        If strResult = vbYes Then

            Application.CloseCurrentDatabase

        Else

            DoCmd.CancelEvent

        End If


    End Sub

    What is this I see a string, aarrrrggggg.<o:p></o:p>

    Will just have to make my own cases, so I can bring the number of post down on this forum ;)<o:p></o:p>


    <o:p></o:p>



  • It's worse than you think.  strResult and strTitle are actually both variants.  Set a breakpoint and look at the locals window to verify.  The proper bad code is:

    Dim strResult As String, strTitle As String, strBody As String

     

     



  • This means that the code is that I thought.:'( <o:p></o:p>



  • And what is that?
    vbYesNo & vbQuestion

    ??
    Its not C++ !
    Please use
    vbYesNo Or vbQuestion



  • @Doker said:

    And what is that?
    vbYesNo & vbQuestion

    ??
    Its not C++ !
    Please use
    vbYesNo Or vbQuestion


    The usual syntax is:

    vbYesNo + vbQuestion

    Or will work just fine and dandy, of course, since there is no bitwise ovelap between non-exclusive options. If one uses addition with mutually exclusive options, one will get an invalid bit pattern; with a Boolean op, one will usually get a valid but sometimes unexpected result.

    The ampersand in the exampple likely comes from a confusion between addition and concatenation, not from confusion between bitwise Boolean operators in different languages.



  • @TUX2K said:

    VBA in access, which can be really fun ;)
    <o:p></o:p>


    Fun??
    Whatever you say, Torquemada.



  • @Stan Rogers said:

    @Doker said:
    And what is that?
    vbYesNo & vbQuestion

    ??
    Its not C++ !
    Please use
    vbYesNo Or vbQuestion


    The usual syntax is:

    vbYesNo + vbQuestion

    Or will work just fine and dandy, of course, since there is no bitwise ovelap between non-exclusive options. If one uses addition with mutually exclusive options, one will get an invalid bit pattern; with a Boolean op, one will usually get a valid but sometimes unexpected result.

    The ampersand in the exampple likely comes from a confusion between addition and concatenation, not from confusion between bitwise Boolean operators in different languages.

    I think Or is generally a better idea - it may give an unexpected result if you're combining exclusive options, but so would +. Or has the advantage that, if you're using a constant that sets several options (and they do sometimes exist, for convenience reasons), and you accidently also set one of them yourself, you get the expected behaviour. (Also, if you've got a function that takes a flags value as an argument, and forces certain flags on before using them for something, it should use Or to do this).

    Other than those situations, though, it's really a matter of style (I think C programmers normally use |, whereas VB ones quite often use +, not sure why).



  • @makomk said:

    @Stan Rogers said:
    @Doker said:
    And what is that?
    vbYesNo & vbQuestion

    ??
    Its not C++ !
    Please use
    vbYesNo Or vbQuestion


    The usual syntax is:

    vbYesNo + vbQuestion

    Or will work just fine and dandy, of course, since there is no bitwise ovelap between non-exclusive options. If one uses addition with mutually exclusive options, one will get an invalid bit pattern; with a Boolean op, one will usually get a valid but sometimes unexpected result.

    The ampersand in the exampple likely comes from a confusion between addition and concatenation, not from confusion between bitwise Boolean operators in different languages.

    I think Or is generally a better idea - it may give an unexpected result if you're combining exclusive options, but so would +. Or has the advantage that, if you're using a constant that sets several options (and they do sometimes exist, for convenience reasons), and you accidently also set one of them yourself, you get the expected behaviour. (Also, if you've got a function that takes a flags value as an argument, and forces certain flags on before using them for something, it should use Or to do this).

    Other than those situations, though, it's really a matter of style (I think C programmers normally use |, whereas VB ones quite often use +, not sure why).



    It's likely a hangover from a time when one didn't have the constants defined in the compiler -- if you wanted to use the options represented by 3 and 32 you simply used 35 in the code. Otherwise you'd have to write a bunch of Const declarations. And it's never been proven to my satisfaction that most VB coders are aware that And and Or are bitwise  operators -- I see a lot of energy put into reinventing that particular wheel.


Log in to reply