0

The problem I am having is I am requesting a WMI query in VB 6 for Modem Names & Ports

I have a FOR EACH LOOP, and there is more than 1 value for each (2 Ports show, so I have 2 values for each). How can I assign a variable so I can assign it to a Label or TextBox?

I would like a VB 6 code sample of how to assign a variable through the loop and how to call the variable?

This is my code (when I use MsgBox I can see it, it just pops up twice separately, but I want variables so I can assign them)

For Each objItem In colItems
    MsgBox ("Test -" & objItem.Name)
Next

I tried this, and I get a number, but I don't know how to reference it

For Each objItem In colItems
    Dim myCount
    myCount = myCount + 1

    Debug.Print objItem.Name & myCount  '** i just tested with Debug.Print
Next

Form1.TextBox1.Text = myCount(1)  '** THIS DOES NOT WORK
Form1.TextBox2.Text = myCount(2)

How Can I assign objItem.Name (it brings back 2 different objects)? This is what I get:

1SAMSUNG Mobile Modem #2
2SAMSUNG Mobile Modem Diagnostic Serial Port (WDM) (COM1)

(the 1 & 2 are from myCount)

Without using myCount, I just want to assign each value its own variable.

5
  • Have you considered using Listbox instead of textboxes? Consider this: What do you want to happen if colItems contains something else than 2 items (0, 1, 3, 4, ...)? Commented Mar 3, 2013 at 11:56
  • I WANT TO ASSIGN THEM TO VARIABLES... so i can RE-USE them, then how would i re-call them ? i know how to assign them to textboxes, but not array values, that i dont know Commented Mar 3, 2013 at 18:53
  • So, you do not know in advance how many modems there will be and also you do not know how many ports each modem has. You will have to create the Labels/TextBoxes dynamically then. Do some reading about Control arrays in VB6. Alternatively use a ListBox or TreeView. It is not so hard to find out yourself how to solve this problem and you will learn a lot by doing so. Good luck! Commented Mar 4, 2013 at 6:24
  • to: Dabblernl, Thx i got some examples for Control arrays, and yea that's what i needed.. and now i get it.. :-) so thx Commented Mar 5, 2013 at 5:27
  • @SecureCloud PLEASE STOP SHOUTING. If you want to signify code, use backticks (`) Commented Mar 5, 2013 at 14:03

3 Answers 3

1

Assuming you have 100 or less objects, with each object having 2 values, here is one way to store a pair of values into a 2 dimensional array:

Dim myVar(100,2) As String
Dim myCount as Integer
myCount = 0
For Each objItem In colItems
    If myCount Mod 2 = 0 Then     
        'read the first value
        myVar(myCount,1) = objItem.Name
    Else
        'read the second value then move to the next object
        myVar(myCount,2) = objItem.Name
        myCount = myCount + 1      
    End If
Next

'Now if you want to print the value of the fifth object:
MsgBox("(Object #5) has first value: " & myVar(5,1) )
MsgBox("And the second value is: " & myVar(5,2) )
Sign up to request clarification or add additional context in comments.

1 Comment

What if there are MORE than 2 values (CUZ THERE IS), and i DONT want to use MSGBOXes or DEBUG.PRINT, i want to assign VARIABLES TO TEXTBOXES OR LABELS... CAN YOU SHOW ME HOW TO DO THAT.... please..
0

From your description I assume that the .Name property contains several fields you want to store separately?

I don't know how the fields in .Name are separated, so in the example below I just consider them space delimited:

Option Explicit

Private Type ModemData
  strField() As String
End Type

Private mudtModems() As ModemData

Private Sub ReadModems()
  Dim intCount As Integer
  Dim strName As String
  ReDim mudtModems(31) As ModemData
  intCount = 0
  For Each objItem In colItems
    strName = objItem.Name
    mudtModems(intCount).strField = Split(strName, " ")
    intCount = intCount + 1
  Next
  ReDim Preserve mudtModems(intCount - 1) As ModemData
End Sub

Initially it creates an array to hold 32 modems, and in the end redims the array to the actual size

The strField array in each udtModem will have various lengths, depending on the number of fields in .Name

You will probably need another routine to split the fields of .Name correctly, use that routine instead of Split(strName, " ")

Comments

0

Actually, you already have your data in a variable. That variable is named colItems. colItems is a variable of type Collection. You can read more about collections on MSDN.

If you know that your collection contains 2 items and your collection is 1-based, you can use your collection like this:

myTextbox1.Text = colItems(1).Name
myTextbox2.Text = colItems(2).Name

or, if you want to assign them to variables:

Dim myString1 as String
Dim myString2 as String

myString1 = colItems(1).Name
myString2 = colItems(2).Name

The difficult part is that you rarely know how many items your collection will contain. Usually, the developer of the API you are using is giving you a collection because there is no way of knowing how many elements the function will return. In such cases, a Collection is a good fit.

When given a collection as a return value from a function, displaying it in a couple of textboxes is rarely a sufficient way of handling the data. A listbox of some kind is usually a better fit. If there is a good reason for using Textbox, then a control array of textboxes is a possible solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.