Solved

Using the !

Posted on 2000-05-12
39
167 Views
Last Modified: 2010-05-02
In ado u can use the ! as a shortcut to a field in a recordset.
ie

strName=rs!name

instead of
strName=rs.fields("Name")

what i want to know is how is this done and is there a way to implement this in my own dll.

ie:a paramater is passed and an object is added to a collection with the key being the parameter,how do i let users of my dll access the item in the collection using !

i want

dim obj as new mydll.classname

obj.Go "Test" 'this adds to a collection
              'as well as other stuff

strSomething = obj!test


is this possible?
0
Comment
Question by:crazyman
  • 12
  • 9
  • 6
  • +5
39 Comments
 
LVL 2

Expert Comment

by:Ginger_Ed
Comment Utility
The ! is from SQL and that is why it works in ado, because your just sending a SQL statement.  I don't think it is possible to get it to work with your own objects though. sorry


Ed
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
Ginger_Ed,

the ! has nothing to do with SQL! It is caused by the collection.

I hope someone will have the answer on how to implement this on our own collection!
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
I agree with emoreau that ! has nothing to do with SQL

And this is REALLY easy!

Add a Item property or function to your class, with one Argument (index) and declare it as Default (Procedure Attributes)

Need more details
0
 
LVL 14

Expert Comment

by:mcrider
Comment Utility
A collection using the ! is based on the key.  Here is how you do it:

Dim XX As New Collection
Private Sub Command1_Click()
    XX.Add "Hello", "Key1"
    Debug.Print XX!Key1
End Sub


Cheers!®©
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
mcrider, the question goes on how do DEVELOP this, not to USE it :-)
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
Hey I just made a small test and it is actually working.

Create a class (named CEmployee) containing this code:
Option Explicit

'local variable(s) to hold property value(s)
Private mvarLastName As String 'local copy
Private mvarFirstName As String 'local copy

Public Property Let FirstName(ByVal vData As String)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.FirstName = 5
    mvarFirstName = vData
End Property

Public Property Get FirstName() As String
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.FirstName
    FirstName = mvarFirstName
End Property

Public Property Let LastName(ByVal vData As String)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.LastName = 5
    mvarLastName = vData
End Property

Public Property Get LastName() As String
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.LastName
    LastName = mvarLastName
End Property

Now create a form containing a command button and paste this code:
Option Explicit

Private Sub Command1_Click()
Dim colX As Collection
Dim objEmpl As CEmployee

    Set colX = New Collection
   
    Set objEmpl = New CEmployee
    objEmpl.FirstName = "Eric"
    objEmpl.LastName = "Moreau"
    colX.Add objEmpl, "ERIC"

    Debug.Print colX("ERIC").LastName
    Debug.Print colX!ERIC
End Sub

If you try it this way, you will have an error on the second Debug.Print line. The trick is to have a "Default property". I made the LastName property the default (from the Class builder utility or from Tools->Procedure attributes).
0
 
LVL 14

Expert Comment

by:mcrider
Comment Utility
Oh well, I misread the question...
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
Happens to everybody... :-)
0
 
LVL 13

Author Comment

by:crazyman
Comment Utility
Cool guys i didnt actually need this for anything it is just something ive always kinda wandered about.

How can u make something the default property via code not the class builder because i couldnt get that to work
0
 
LVL 13

Author Comment

by:crazyman
Comment Utility
if u do it like collection!key then that is assuming that a record set is a collection, call me stupid but how does a collection take on extra methods and properties such as a record set?



ie .movefirst,movelast

this is a subject i would greatly like to know more about.

This is touching on all sorts of issues such as inheritance, are there any good learning sites for this?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
Via code?????
Sorry, not possible.
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
You can use the Tools->Procedure attributes to set a property as the default one.
0
 
LVL 38

Expert Comment

by:PaulHews
Comment Utility
If you put the cursor in the property you wish to make default, you then select tools | Procedure attributes, click the advanced button and for Procedure ID, select "Default."

MS couldn't have hidden this feature any better if they tried.
0
 
LVL 13

Author Comment

by:crazyman
Comment Utility
cheers, i was looking for a check box or summin.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
Who of you knows then how to define enumerator, so that you can have code
FOR EACH
on you class

Without looking into MSDN (be honest)
0
 
LVL 13

Author Comment

by:crazyman
Comment Utility
So howcome if a record set is a collection it has more methods etc.. than any other collection?
How do you add these?
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
Use the class builder utility to create your collection. It could help you start fast!
0
 
LVL 13

Author Comment

by:crazyman
Comment Utility
Forgive me but where do i find that?
I have never used it before
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
It's an addin
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
From the Add-ins menu, select "Add-in manager..."
0
 
LVL 13

Author Comment

by:crazyman
Comment Utility
i havent got it.
where can i get it?
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
Which version of VB? How many lines do you see in the manager? If you see around 5 lines and you are using NT, see article Q190212.
0
 
LVL 13

Author Comment

by:crazyman
Comment Utility
it isnt in my addin list
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
Reinstall VB, choosing some Addition Tools...
0
 
LVL 4

Expert Comment

by:Jeremy_D
Comment Utility
>> Who of you knows then how to define enumerator, so that you can have code
>> FOR EACH
>> on you class
>> Without looking into MSDN (be honest)

I think you have to give your collection class a NewEnum property that returns the hidden _NewEnum property of the underlying collection. IIRC it has to be declared "As IUnknown", you have to make it hidden, and give it a rather bad documented procedure-id (which I would have to look up in the MSDN, but I think it was 4)

Am I correct? (I'll start looking in the MSDN now)
0
 
LVL 13

Author Comment

by:crazyman
Comment Utility
when you find out can you post the code with an example please...
0
 
LVL 4

Expert Comment

by:Jeremy_D
Comment Utility
Almost dead on. The procedure-id has to be -4 (just checked), but for the rest I was correct. Guess I've done this to often %)
0
 
LVL 4

Expert Comment

by:Jeremy_D
Comment Utility
Sure, this is from "Creating Your Own Collection Class: The House of Bricks" which you can find in "Visual Basic Concepts":

----------------------------------------

Enabling For Each … Next

Along with robustness, you get For Each … Next back. Once again you can delegate all the work to the Collection object, by adding the following method:

' NewEnum must return the IUnknown interface of a
' collection's enumerator.
Public Function NewEnum() As IUnknown
   Set NewEnum = mcolEmployees.[_NewEnum]
End Function

The important thing you're delegating to the Collection object is its enumerator. An enumerator is a small object that knows how to iterate through the items in a collection. You can't write an enumerator object with Visual Basic, but because the Employees class is based on a Collection object, you can return the Collection object's enumerator — which naturally enough knows how to enumerate the items the Collection object is holding.

The square brackets around the Collection object's _NewEnum method are necessary because of the leading underscore in the method name. This leading underscore is a convention indicating that the method is hidden in the type library. You can't name your method _NewEnum, but you can hide it in the type library and give it the procedure ID that For Each … Next requires.

To hide the NewEnum method and give it the necessary procedure ID

On the Tools menu, click Procedure Attributes to open the Procedure Attributes dialog box. In Name box, select the NewEnum method.


Click Advanced to show the advanced features. Check Hide this member to make NewEnum hidden in the type library.


In the Procedure ID box, type –4 (minus four) to give NewEnum the procedure ID required by For Each … Next. Click OK.
Important   In order for your collection classes to work with For Each … Next, you must provide a hidden NewEnum method with the correct procedure ID.

0
 
LVL 4

Expert Comment

by:Jeremy_D
Comment Utility
If you want to locate the article in the MSDN (contains a lot of info on writing your own collection classes, recommended reading), do a title-only search on "creating your own collection classes". It will take you to the "House of Straw", "House of Sticks" and "House of Bricks" examples.
0
 
LVL 38

Expert Comment

by:PaulHews
Comment Utility
Also in Books Online in Professional VB 5.0
0
 
LVL 13

Author Comment

by:crazyman
Comment Utility
So the answer to my question is indeed that a record set is not a collection but a collection class.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
a record is much more than a collection:
* navigation (MoveNext...)
* persistance (Update...)
* multicolumn (Fields collection)
* search (Find method)
0
 
LVL 4

Expert Comment

by:Jeremy_D
Comment Utility
Recordset and Collection are different things. The bang-character is a leftover from old times and office-like notations.

The fact that you can't duplicate it in VB says nothing. The recordset object (or indeed the whole ADO object-model) was not written in VB.
0
 
LVL 13

Author Comment

by:crazyman
Comment Utility
i know it isnt a collection, what i mean is it contains a collection class(or appears to) that holds the data, the rest of the stuff if you like is wrappered around this,i was justusing this as en example of using ! with collections,nothing specific.
0
 
LVL 13

Author Comment

by:crazyman
Comment Utility
who the hell do i give the points too?
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 20 total points
Comment Utility
Ask the community support to split points
0
 
LVL 13

Author Comment

by:crazyman
Comment Utility
Adjusted points from 50 to 60
0
 
LVL 3

Expert Comment

by:darinw
Comment Utility
Community Support has reduced points from 60 to 20
0
 
LVL 3

Expert Comment

by:darinw
Comment Utility
Hello everyone,

I am reducing the points on this question to 20 to allow for a split.

You can now accept one of the comments in this thread as an answer. To award the other Experts, you must create 2 new questions in this topic area with a title of 'For ExpertName -- 10347980' using the appropriate Expert username.

Remember, the Accept Comment as Answer button is in the header of the comment.

For your convenience, you can use this link to create the new question:
http://www1.experts-exchange.com/bin/NewQForm?ta=31

darinw
Customer Service
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now