Results 1 to 12 of 12

Thread: [RESOLVED] Loops.

  1. #1

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Resolved [RESOLVED] Loops.

    I'm trying to implement a loop on a set of variables of the same type which are named strn, where n is an integer.
    Why does this code doesn't work? How should I define the intermediate variable aux?

    Code:
    Dim auxAs Object
    Dim str1 As String
    Dim str2 As String
    
    str1 = "str1"
    str2 = "str2"
    
    For i = 1 To 2
        Set obj= "Str" & 1
        MsgBox aux.Name
    Next i
    Last edited by Fonty; Sep 10th, 2007 at 04:50 PM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Loops.

    One thing that immediately jumps out is that you forgot an "AS"
    Code:
    Dim auxAs Object
    'should be
    Dim auxAs As Object

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Loops.

    And, I don't see why you need it anyway as you are not using auxAs for anything.

    This worked, although I'm not completely sure what you are trying to do
    Code:
    Private Sub Command1_Click()
    
    Dim str1 As String
    Dim str2 As String
    Dim i As Long
    str1 = "str1"
    str2 = "str2"
    
    For i = 1 To 2
        str1 = "Str" & 1
        MsgBox str1
    Next i
    End Sub

  4. #4

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Re: Loops.

    name property was just an example. In fact my interest is to extract the value of that string.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Loops.

    The value of str1?

  6. #6

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Re: Loops.

    Yes. The value of strn.

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Loops.

    Try this
    Code:
    Private Sub Command1_Click()
    Dim str1 As String
    Dim intCounter As Integer
    Dim i As Long
    str1 = "str1"
    
    For i = 1 To 2
        intCounter = intCounter + 1
        str1 = "Str" & intCounter
        MsgBox str1
    Next
    End Sub

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Loops.

    I see this a LOT in here... and everytime, people don't quite get what the poster is asking for.... mainly because it's not possible. You can't create a variable to hold a variable's name and expect to get the variable's value.

    Going back to the OP's original code, I'll make a change, then state what I think the OP is looking for, and see if I've got it right.

    Code:
    Dim str1 As String
    Dim str2 As String
    
    str1 = "Hack"
    str2 = "TechGnome"
    
    For i = 1 To 2
        Set obj= "Str" & 1
        MsgBox obj
    Next i
    I'm guessing that in this case the OP would expect "Hack" in the first message box and "TechGnome" in the second box.

    Yeah, it doesn't work that way. What you would get is "Str1" followed by "Str2"... not quite what you are looking for is it?

    There are ways to accomplish what you are doing, but not in the simplistic manner that you want - hint: Arrays, collections.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Loops.

    Well, you are right about that. I certainly did not get that out of what was originally asked.

  10. #10

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Re: Loops.

    Techgnome is right on how the code is supposed to work. However I still cannot make it work.
    You can't set a object that way...

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Loops.

    Right.... it just doesn't work that way. IF you set something dimmed as object to the literal text string "Str1"... that's exactly what it will contain.... actually, I think you may even get an error, about TypeConversion.

    But at any rate, in a simplistic manner, what you are looking to do cannot be done. You'll have to use something else. Hint: Array or Collection

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Re: Loops.

    I solved it perfectly with an array. Thanks a lot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width