How to code this?

R

Rodney

Ok, here's my scenerio.

I have a FORM which has a LIST BOX of computers.

I have a SUBFORM with in the FORM that is linked via the
computers name.

When I click the Computer's Name in the list box, the
subform is updated. This is done, now problem.

Now I have some code, various one's that I want to insert
into a Command Button that will do a certain task. For
example: (This will grab the service pack version of the
computer named in strComputer = "")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.CreateTextFile
("c:\scripts\test.txt", True)
strComputer = "Computer Name"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer
& "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("SELECT * FROM Win32_OperatingSystem")
objTextFile.WriteLine "ServicePackVersion"
For Each objOperatingSystems in colOperatingSystems
objTextFile.WriteLine
objOperatingSystems.ServicePackMajorVersion _
& "." &
objOperatingSystems.ServicePackMinorVersion
Next
ObjTextFile.Close


I want to be able to have some code GRAB the ComputerName
from the subform, and run this code against it. How can
this be accomplished?

Thanks
 
T

Ted

you should be able to use (not sure how wrapping will
affect this - but there is not a space between the word
Item and the following parenthesis):

strComputer = Me.SubformName.Controls.Item
("SubFormFieldName")

to assign the value of a subform field to your variable.
Just insert the appropriate subform and field names.

Hope this helps.

-Ted
 
R

Rodney

Seemed easy enough, but didn't work. Give's me a Compile
Error: Method of Data Member not found. And it
highlights the subform after Me.

Here's what it looks like:


strComputer = Me.frmsub3.Controls.Item("Cmp Name")
 
T

Ted

Couple of things you may want to try:

As you are typing the statement, after the word "Me" you
should get a drop-down list after each "." that you
type. Make sure to select from the list to avoid any
typos in your subform name, and also to confirm that it
is interpreting "Me" to be your main form (if it wasn't
you wouldn't see the subform listed after you type the
first ".").

Also, you may need to put []'s around your field name
because it has a space in it. You may want to try:

strComputer = Me.frmsub3.Controls.Item("[Cmp Name]")

If it still doesn't work, look at the properties of the
control on the subform to confirm that [Cmp Name] is the
correct control name.

Hopefully these ideas will help. Let me know if it works.

-Ted
 
G

Guest

Ok, typing and not copying and pasting works better. ;)

But, that did not work for me. I tried something else,
and it worked. I couldn't find the SubForm name listed in
the drop down, so I selected FORM, and it worked.

strComputer = Me.Form.Controls.Item("[Cmp Name]")

Now, it wasn't like I needed to grab in from another
form, since the subform and the main form were linked
with the Computer Name. Hmm.. So, for kicks, how to you
grab select another form?? All I saw was current form
Labels, properties..

Thanks Ted!





-----Original Message-----
Couple of things you may want to try:

As you are typing the statement, after the word "Me" you
should get a drop-down list after each "." that you
type. Make sure to select from the list to avoid any
typos in your subform name, and also to confirm that it
is interpreting "Me" to be your main form (if it wasn't
you wouldn't see the subform listed after you type the
first ".").

Also, you may need to put []'s around your field name
because it has a space in it. You may want to try:

strComputer = Me.frmsub3.Controls.Item("[Cmp Name]")

If it still doesn't work, look at the properties of the
control on the subform to confirm that [Cmp Name] is the
correct control name.

Hopefully these ideas will help. Let me know if it works.
-Ted

-----Original Message-----
Seemed easy enough, but didn't work. Give's me a Compile
Error: Method of Data Member not found. And it
highlights the subform after Me.

Here's what it looks like:


strComputer = Me.frmsub3.Controls.Item("Cmp Name")



.
.
 
T

Ted

Hi Rodney,

Glad you got it working.

It sounds like your code was probably running on your
subform rather than the form. In that case you could
probably have used the statement:

strComputer = Me![Cmp Name]

which is a shortcut for the same thing. If your button
is on your subform, the code that runs when it is clicked
will be a part of that subform as well. There are
various ways to know what form is running the code. One
easy way is to check under "Window" on the menu when you
are in the code editor and it will show the form name
that the code belongs to.

"Me" always refers to the form that is running the code
(or the form that contains the object running the code).

Regarding your other question about how to refer to other
forms. Think of it like a tree. To refer to things
higher in the tree, use .parent to go up a level (for
instance, if "Me" is a subform, then Me.parent will give
you all of the main form properties). To go down a level
(branch) use a "." and choose the item or property that
you want.

To refer to an object completely independent of the
source of the code that is running, you start with the
appropriate collection and go from there. For instance,
the Forms collection contains all forms, so Forms!
[Orders]![OrderDate] would give you the value of the
OrderDate control on the Orders form (provided that it is
opened). FYI, you can always use this long way of
referring to things, but for items on the current forms
the shorthand way is usually used.

You may want to look at the help under "About referring
to an object or its properties in expressions", and also
look up some different collections by doing a search for
the word "collection".

-Ted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top