-
Sep 28th, 2020, 03:57 AM
#1
Thread Starter
New Member
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
-
Sep 28th, 2020, 04:07 AM
#2
Thread Starter
New Member
Re: difference between ! and . character in the syntax of the VBA identifieer
I have inserted a updated picture of the Form.Attachment 178879
-
Sep 28th, 2020, 04:08 AM
#3
Thread Starter
New Member
Re: difference between ! and . character in the syntax of the VBA identifieer
-
Sep 28th, 2020, 04:17 AM
#4
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/
-
Sep 28th, 2020, 04:38 AM
#5
Thread Starter
New Member
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
-
Sep 28th, 2020, 05:02 AM
#6
Thread Starter
New Member
Re: difference between ! and . character in the syntax of the VBA identifieer
-
Sep 28th, 2020, 05:44 AM
#7
Thread Starter
New Member
Re: difference between ! and . character in the syntax of the VBA identifieer
Originally Posted by Arnoutdv
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.
-
Sep 28th, 2020, 06:45 AM
#8
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>
-
Sep 28th, 2020, 07:18 AM
#9
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
-
Sep 28th, 2020, 07:39 AM
#10
Re: difference between ! and . character in the syntax of the VBA identifieer
Originally Posted by techgnome
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>
-
Sep 28th, 2020, 07:52 AM
#11
Re: difference between ! and . character in the syntax of the VBA identifieer
Originally Posted by wqweto
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
-
Sep 28th, 2020, 11:27 AM
#12
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
-
Sep 28th, 2020, 04:55 PM
#13
Fanatic Member
Re: difference between ! and . character in the syntax of the VBA identifieer
I read both articles but still a little confused.
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.
-
Sep 29th, 2020, 02:07 AM
#14
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>
Last edited by wqweto; Sep 29th, 2020 at 02:10 AM.
-
Sep 29th, 2020, 04:50 AM
#15
Thread Starter
New Member
Re: difference between ! and . character in the syntax of the VBA identifieer
Originally Posted by techgnome
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
-
Sep 29th, 2020, 05:06 AM
#16
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>
-
Sep 29th, 2020, 08:38 AM
#17
Fanatic Member
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).
-
Sep 29th, 2020, 10:21 AM
#18
Re: difference between ! and . character in the syntax of the VBA identifieer
Originally Posted by vb6forever
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|