Results 1 to 18 of 18

Thread: difference between ! and . character in the syntax of the VBA identifieer

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2020
    Posts
    7

    Exclamation difference between ! and . character in the syntax of the VBA identifieer

    Hi there,
    Greetings from Shank. Am new to the forum.

    I am in the process of developing a VBA app. The thing is I don't know the difference between the following vb statements. They all behave the same way (i.e copy the contents the Left Text Box to the Right one. Can you tell me what the "!" exclamation mark does when compared to "." dot character. Can I use them Interchangeably.
    'Userform1.bas
    ---------------
    Private Sub CommandButton1_Click()

    Me.TextBox2 = Me.TextBox1
    Me!TextBox4 = Me!TextBox3
    form.TextBox6 = form.TextBox5
    form!TextBox8 = form!TextBox7


    End Sub

    Module1.bas
    --------------`
    Sub displayform()
    Dim frm As New UserForm1
    frm.Show

    Unload frm
    Set frm = Nothing

    End Sub

    Name:  New Bitmap Image.jpg
Views: 116
Size:  50.7 KB

  2. #2

    Thread Starter
    New Member
    Join Date
    Sep 2020
    Posts
    7

    Re: difference between ! and . character in the syntax of the VBA identifieer

    I have inserted a updated picture of the Form.Attachment 178879

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2020
    Posts
    7

    Re: difference between ! and . character in the syntax of the VBA identifieer


  4. #4
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    3,928

    Re: difference between ! and . character in the syntax of the VBA identifieer

    I never use the ! (the bang operator), but there is a nice blog about it:
    https://bytecomb.com/the-bang-exclam...erator-in-vba/

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2020
    Posts
    7

    Re: difference between ! and . character in the syntax of the VBA identifieer

    thanks for the repy man. I appreciate it. I searched all over the net but couldn't find it, until now

  6. #6

    Thread Starter
    New Member
    Join Date
    Sep 2020
    Posts
    7

    Re: difference between ! and . character in the syntax of the VBA identifieer

    The below article also deal about it.
    https://docs.microsoft.com/en-us/pre...mation-clients

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2020
    Posts
    7

    Re: difference between ! and . character in the syntax of the VBA identifieer

    Quote Originally Posted by Arnoutdv View Post
    I never use the ! (the bang operator), but there is a nice blog about it:
    https://bytecomb.com/the-bang-exclam...erator-in-vba/
    Hi Arnoutdv,
    I went through the post you provided. It was difficult to comprehend it. Not for the layman. ...I too, am going to avoid operator.

  8. #8
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    2,309

    Re: difference between ! and . character in the syntax of the VBA identifieer

    Why form.TextBox5 works when form is dimmed both As Object and As UserForm1 is even more mysterious.

    Once they learned how to hack IDispatch with dynamic properties the bang operator became obsolete. Obviously bang operator *predates* IDispatch (and even COM!) as a technology.

    cheers,
    </wqw>

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,010

    Re: difference between ! and . character in the syntax of the VBA identifieer

    This belongs in the VBA section of the forums... the use of the bang operator is a VBA "trick" not a VB one...
    I'll ask the mods to move it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    2,309

    Re: difference between ! and . character in the syntax of the VBA identifieer

    Quote Originally Posted by techgnome View Post
    This belongs in the VBA section of the forums... the use of the bang operator is a VBA "trick" not a VB one...
    I'll ask the mods to move it.

    -tg
    The code in this thread and the explanation in the linked article work in VB6 too. (And the VBA section is undermonitored by the regulars here.)

    cheers,
    </wqw>

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,010

    Re: difference between ! and . character in the syntax of the VBA identifieer

    Quote Originally Posted by wqweto View Post
    The code in this thread and the explanation in the linked article work in VB6 too. (And the VBA section is undermonitored by the regulars here.)

    cheers,
    </wqw>
    Say wha?
    Code:
    Me!TextBox4 = Me!TextBox3
    Works in VB6?
    I know it does when dealing with ADO and recordsets...
    RS!FieldName...
    But this is the first I'm hearing that form!control works too...

    What form of trickery and black magic is this?

    SCANDALOUS!

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,135

    Re: difference between ! and . character in the syntax of the VBA identifieer

    The VisualBasic Core language was merged/replaced with the VBA Core as part of the transition from VB3 to VB4 (remember that backlash, i.e. VB4 is so slow compared to VB3...!?), so as of VB4 Visual Basic had the !bang operator because of the common set of language elements.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  13. #13
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    540

    Re: difference between ! and . character in the syntax of the VBA identifieer

    I read both articles but still a little confused.
    RE: https://bytecomb.com/the-bang-exclam...erator-in-vba/

    The bang operator provides late-bound access to the default member of an object, by passing the literal name following the bang operator as a string argument to that default member.
    If I am reading the above and his examples correctly:

    Late Bound (any assignment to the default member with ! forces Late Bound ?)
    Dim strTest As String
    strTest = Recordset!Name
    Early Bound
    ' This would still work:
    strTest = Recordset("Name")

    ' ... because it is really shorthand for
    strTest = Recorset.Fields.Item("Name")
    I guess in some ways this makes sense. The compiler has already identified anything with quotes around it as a string, where it does NOT know the string exists until the bang operator is encountered.
    Last edited by vb6forever; Sep 28th, 2020 at 05:03 PM.

  14. #14
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    2,309

    Re: difference between ! and . character in the syntax of the VBA identifieer

    First, you have to understand what early-bound call vs late-bound call is. For an early-bound call the compiler emits code that uses the VTable of an interface to locate the address of a function and calls it. For a late-bound call the compiler *always* works with IDispatch (the variable is declare As Object in VB) and emits code that first calls IDispatch::GetIDsOfNames to convert a method's name (as string) to a number (the so called DISPID) and then uses this number in a call to IDispatch::Invoke and this is *1000x* more expensive than early-bound calls.

    Lets have an inocuous looking Result = obj.Method(1, 2, 3) line in VB6.

    So for early-bound call the compiler needs to have an interface to work with. In VB6 terms this means obj is strongly typed/dimensioned with Dim obj As MyType. The MyType type in VB6 determines the interface the compiler is going to use for early-bound calls. Having obj strongly typed we can call the default method on its interface with Result = obj(1, 2, 3) i.e. omitting the name of the method. This is still early-bound call because there is an interface the compiler can work with. Having obj strongly typed we can use the bang operator to call the default method with a string parameter Result = obj!Item1. This is a short-hand for Result = obj("Item1") i.e. this is an early-bound call on the default method, passing a single *string* parameter "Item1".

    So far *everything* is early-bound calls. The explicit method call with dot, the default method call with no dot, the default method call + string parameter with the bang.

    Now, lets consider what happens if we have Dim obj As Object i.e. the compiler does *not* have an interface to call methods on its VTable early-bound. We enter the late-bound territory but surprisingly obj.Method(1, 2, 3), obj(4, 5, 6) and obj!Item1 continue to work without any compilation error but the machine code produced is very different and orders of magnitude slower and bigger.

    Not let's consider ADODB classes default methods:
    Recordset class -> Fields property
    Fields collection -> Item property
    Field class -> Value Property

    Using this info one can translate this line strTest = Recordset!Name to strTest = Recordset.Fields.Item("Name").Value and whether all the calls are early-bound or late-bound depends only on if the Recordset variables is dimensioned strongly typed with Dim Recordset As ADODB.Recordset or not.

    cheers,
    </wqw>

  15. #15

    Thread Starter
    New Member
    Join Date
    Sep 2020
    Posts
    7

    Re: difference between ! and . character in the syntax of the VBA identifieer

    Quote Originally Posted by techgnome View Post
    Say wha?
    Code:
    Me!TextBox4 = Me!TextBox3
    Works in VB6?
    I know it does when dealing with ADO and recordsets...
    RS!FieldName...
    But this is the first I'm hearing that form!control works too...

    What form of trickery and black magic is this?

    SCANDALOUS!

    -tg
    Hi there,
    I am sorry. I am made a typo error in the original Post.

    form.TextBox6 = form.TextBox5
    form!TextBox8 = form!TextBox7


    The compiler flags the above 2 statements as errors.
    Therefore the below code is correct one:

    Private Sub CommandButton1_Click()

    Me.TextBox2 = Me.TextBox1
    Me!TextBox4 = Me!TextBox3

    'Note:the form! notation statement is deleted
    End Sub

    Module1.bas
    --------------`
    Sub displayform()
    Dim frm As New UserForm1
    frm.Show

    Unload frm
    Set frm = Nothing

    End Sub

  16. #16
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    2,309

    Re: difference between ! and . character in the syntax of the VBA identifieer

    Yes, the most intresting case in OP is missing. This should fix it

    Dim form As Object
    Set form = Me

    Now it should compile. Tested it works in VB6.

    cheers,
    </wqw>

  17. #17
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    540

    Re: difference between ! and . character in the syntax of the VBA identifieer

    wqweto:

    Thanks for that detailed explanation and clearing up my confusion with the article.
    So to paraphrase:

    Bang is just a shortcut notation to the default method and as always (how I understood it in the past),
    how the object is declared determines whether we are late bound (as Object) or early bound (e.g. As Picturebox).

  18. #18
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    2,309

    Re: difference between ! and . character in the syntax of the VBA identifieer

    Quote Originally Posted by vb6forever View Post
    how the object is declared determines whether we are late bound (as Object) or early bound (e.g. As Picturebox).
    This is very precise. Few people understand that this is early-bound

    Dim obj As Class1
    Set obj = CreateObject("MyProject.Class1")

    while this is late-bound

    Dim obj As Object
    Set obj = New Class1

    It doesn't matter how an object is instantiated, all it matters is how the variable is dimensioned.

    For instance this is early-bound again

    Dim obj As Class1
    Set obj = MyCreateObjectWithInit(1, 2, 3)

    Private Function MyCreateObjectWithInit(...) As Object
    ...
    End Function

    cheers,
    </wqw>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width