Can you figure out how to do this in Access2002/VBA?
I want to get the value of an object property. The trick is that the name of the property to retrieve is stored in a table.
Here's how I've set it up:
I have a class module "objPerson" which has a property "FirstName".
This works: - objPerson.FirstName = "John Smith"
-
strFirstName = objPerson.FirstName
and it will correctly set
strFirstName = "John Smith"
So far, so good, right?
Next, in "MyTable" I have a field "MyField" which contains the text string "objPerson.FirstName"
I want to get the value of the item specified by the string in MyField. In other words, I want the program to determine the value of whatever object property that I have stored in MyField. In this case, I have opened the object objPerson and I want the program to give me the value of "objPerson.FirstName".
I've tried using Eval(), but so far it doesn't work: - strFirstName = Eval(rst.Fields("MyField"))
The result is: "Error 2482: Microsoft Access can't find the name 'objTenant' you entered in the expression."
MY OBJECTIVE
My objective is to do string replacement where the REPLACEMENT text is set in a table, like this: - In "MyTable":
-
-
Field:KEY Field:REPLACE_WITH
-
------------------------- -------------------------------------
-
Tenant.FirstName objTenant.FirstName
-
Owner.FirstName objOwner.FirstName
-
Balance objInvoice.BalanceAmount
-
(etc...)
-
-
strMsg = "Hello [Tenant.FirstName]. Your invoice balance is [Invoice.Balance]"
-
strMsg = MySpecializedReplacementFunction(strMsg)
-
strMsg: "Hello John. Your invoice balance is $10"
13 9254
Hi. What I would do is use the value in the table to activate a section of code in a Select Case statement; -
Select Case rs.Fields("Field1")
-
Case "objPerson.FirstName"
-
strText = objPerson.FirstName
-
-
Case "something else"
-
'peform whatever it says here
-
End Select
-
Hope this helps
Thank you, Willakawill. That is a workable alternative.
The only disadvantage is that the fields would have to be hardcoded in the SELECT-CASE statement. Ideally, updating the list of string Keys and the Replacement text would be maintainable via a table.
For just the property this should work:
dim arr
arr = split(rs!Replace_with,".")
select case arr(0)
Case "objTenant"
strX = objTenant.properties(arr(1))
Case .... etc ...
The Split will fill an array (zero based!) with the two parts and arr(0) will hold the object and arr(1) the property.
Getting the idea ?
Nic;o)
Hi nico5038,
I really like your idea. It allows the substitutions to be solved dynamically and that's important to me.
Nico,
I've added the code but it doesn't like the .Properties in objTenant.Properties. I get an error: - Dim objTenant As pm_Tenant
-
Set objTenant = New pm_Tenant
-
objTenant.Load (TenantID)
-
-
Debug.Print "name1=" & objTenant.FullName
-
' ^ up to here it works. It correctly displays: Name1=Joe Smith
-
-
' The following lines return an error: "Compile error: Method or data member not found"
-
Debug.Print "name2=" & objTenant.Properties("FullName")
-
Debug.Print "name3=" & objTenant.Properties!FullName
-
Of course, I'm using the hard-coded "FullName" just to test it. Once it's working, I'll change to using your suggestion, .Properties(arr(1))
Hmm, strange. Are you sure the properties have been defined correctly ?
To test use:
objTenant.properties.count
This will get the number of properties and check the available by using:
objTenant.properties(0).Name
You can see the property names there are.
Nic;o)
I tried and both statements give the error "method or data member not found". For some reason, my Access VBA doesn't understand ".properties":
objTenant.properties.count
objTenant.properties(0).FirstName
Maybe a reference needs to be set in Tools > References?
Am I setting the properties correctly? As you can see below, I have "Property Let" and "Property Get" procedures and they are working just fine. If I type "? objTenant.Name" I get the correct result. - Public Property Get FirstName() As String
-
FirstName = mstrFirstName
-
End Property
-
-
Public Property Let FirstName(NewValue As String)
-
mstrFirstName = NewValue
-
End Property
I'm startled, but have asked another expert to join.
Nic;o)
- Public Property Get FirstName() As String
-
FirstName = mstrFirstName
-
End Property
-
-
Public Property Let FirstName(NewValue As String)
-
mstrFirstName = NewValue
-
End Property
OK here is another way you can do this using vb6 and the following class module. I have tested this and it works :) -
'MyDog.cls class module
-
'local variable(s) to hold property value(s)
-
Private mvarDogName As String 'local copy
-
Private mvarTailWags As Integer 'local copy
-
Public Sub FeedDog(Optional NumCookies As Integer = 1)
-
mvarTailWags = mvarTailWags + Abs(NumCookies * 5)
-
End Sub
-
-
Public Property Let TailWags(ByVal vData As Integer)
-
'used when assigning a value to the property, on the left side of an assignment.
-
'Syntax: X.TailWags = 5
-
mvarTailWags = vData
-
End Property
-
-
-
Public Property Get TailWags() As Integer
-
'used when retrieving value of a property, on the right side of an assignment.
-
'Syntax: Debug.Print X.TailWags
-
TailWags = mvarTailWags
-
End Property
-
-
-
-
Public Property Let DogName(ByVal vData As String)
-
'used when assigning a value to the property, on the left side of an assignment.
-
'Syntax: X.DogName = 5
-
mvarDogName = vData
-
End Property
-
-
-
Public Property Get DogName() As String
-
'used when retrieving value of a property, on the right side of an assignment.
-
'Syntax: Debug.Print X.DogName
-
DogName = mvarDogName
-
End Property
-
-
Public Property Get NewValue(stValue As String) As String
-
Select Case stValue
-
Case "DogName"
-
NewValue = mvarDogName
-
Case "TailWags"
-
NewValue = CStr(mvarTailWags)
-
End Select
-
End Property
-
then in a form module: -
Private Sub cmdFeedTheDog_Click()
-
Dim MyNewDog As MyDog
-
Set MyNewDog = New MyDog
-
MyNewDog.DogName = "Fido"
-
MyNewDog.FeedDog 5
-
MsgBox CInt(MyNewDog.NewValue("TailWags"))
-
End Sub
-
This code
Dim objTenant As pm_Tenant
Set objTenant = New pm_Tenant
is this referring to pm_Tenant as a class object?
And FullName is a variable inside your class?
If that is the case, then u cant access it using Properties as this is not a valid method for classes. U need to write public functions or create the public property functions like willakawill has shown/
or am I thinking this wrong.
This code
Dim objTenant As pm_Tenant
Set objTenant = New pm_Tenant
is this referring to pm_Tenant as a class object?
And FullName is a variable inside your class?
If that is the case, then u cant access it using Properties as this is not a valid method for classes. U need to write public functions or create the public property functions like willakawill has shown/
or am I thinking this wrong.
You are right. This is a confusion between user defined classes and inherent system objects
Yes, pm_Tenant is a class module and .FullName is a property (a variable) of the class.
I think you are saying to create a function within the class module that returns the value, rather than using a property to return the value.
Thank you for your replies... I'll digest your example and see if it works for me.
Yes, pm_Tenant is a class module and .FullName is a property (a variable) of the class.
I think you are saying to create a function within the class module that returns the value, rather than using a property to return the value.
Thank you for your replies... I'll digest your example and see if it works for me.
Property and variable are not interchangeable. They are not the same thing
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Steven Bethard |
last post by:
In the "empty classes as c structs?" thread, we've been talking in some
detail about my proposed "generic objects" PEP. Based on a number of
suggestions, I'm thinking more and more that instead of...
|
by: Simon |
last post by:
Am using the following code.
<script language="JavaScript1.2">
function setquantity(productindex,productquantity)
{
//create the reference object
irefname_none = eval("document." +...
|
by: fred |
last post by:
I don't know if I'm doing this correctly. I have a little programming
experience in python, c++ and some others but this is my first time with
javascript. I'm trying have my website detect the...
|
by: Steve Neill |
last post by:
Can anyone suggest how to create an arbitrary object at runtime
WITHOUT using the deprecated eval() function. The eval() method works
ok (see below), but is not ideal.
function Client() { }...
|
by: yb |
last post by:
Hi,
Is there a standard for the global 'window' object in browsers? For
example, it supports methods such as setInterval and clearInterval, and
several others.
I know that w3c standardized...
|
by: Roman Ziak |
last post by:
Hello,
there were times when I used to be looking for a way to access
JavaScript Global object similar to those found in VBScript or PHP
($GLOBALS). At present this has only academic value for...
|
by: jm.suresh |
last post by:
Hi,
Is there any standard text format for storing data of object oriented
nature.
The text file should be readable.
That is, Is there any better way than having to write out a file like
this...
|
by: Daz |
last post by:
Hi everyone. Sorry for the confusing subject, I couldn't think how best
to word it.
What I would like to know, is if there is an equivilant to this code,
in using JSON.
<script...
|
by: =?Utf-8?B?QXNo?= |
last post by:
Hi,
I have an object, for example User. User contains various properties which i
have been able to bind to successfully using wizards and the form view.
However if the class User has a property...
|
by: FAQ server |
last post by:
-----------------------------------------------------------------------
FAQ Topic - How do I access a property of an object using
a string?...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |