Results 1 to 10 of 10

Thread: [RESOLVED] Operating on a Variable's properties

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Resolved [RESOLVED] Operating on a Variable's properties

    Sorry, that title is a bit confusing.

    What I have is a program with a lot of buttons. They all do a similar thing (take a piece of text from their related fields and save it to a file), but I'm trying to streamline the code by not having to have the full write code in each button.

    What I currently have is a global variable called "VSave". The idea being that in each button I define VSave as the name of that button's respective textbox and then call a function to process the text. So, each of the buttons has code like this:

    Code:
    Private Sub c1s_Click()
    VSave = "c1"
    Call SaveMe
    End Sub
    
    Private Sub c2s_Click()
    VSave = "c2"
    Call SaveMe
    End Sub
    
    Private Sub c3s_Click()
    VSave = "c3"
    Call SaveMe
    End Sub
    
    Private Sub c4s_Click()
    VSave = "c4"
    Call SaveMe
    End Sub
    
    Private Sub d1s_Click()
    VSave = "d1"
    Call SaveMe
    End Sub
    
    Private Sub d2s_Click()
    VSave = "d2"
    Call SaveMe
    End Sub
    
    Private Sub d3s_Click()
    VSave = "d3"
    Call SaveMe
    End Sub
    ... And etc.

    What I'm then trying to do in the main "write" function (SaveMe) is to operate on properties of SaveMe (the contents of SaveMe is the name of the textbox, for example:

    Code:
    Dim WriteString as String
    Dim FF As Integer
    FF = FreeFile()
    
    If VSave.Text = "" Then
        Do something
    End if
    
    WriteString = VSave.text
    ... Etc etc

    In other words, the content of VSave is the name of the textbox to be operated on, and I'm trying to perform those operations - however I cannot, as VSave itself is just a variable.

    I've also tried "Val(VSave).text" but this doesn't work either.

    Any ideas how I might be able to do this?

    Thanks!

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Operating on a Variable's properties

    If "vsave" is a variable, it doesn't have a "text" property.

    You could simply say "if vsave="" then..."

    or "if vsave<>"" then..."

    Make sense?

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Operating on a Variable's properties

    It would be much easier to tweak your save function
    Code:
    Public Function SaveMe(theControl As Control) As Boolean
    ....
    If theControl.Text = "" Then
        Do Something
    End If
    
    WriteString = theControl.Text
    SaveMe = True
    Then to call it...
    Code:
    Private Sub c1s_Click()
    Call SaveMe(c1)
    End Sub
    Last edited by LaVolpe; Sep 2nd, 2009 at 01:45 PM. Reason: corrected typo
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Operating on a Variable's properties

    Makes sense, but I also want to operate on the text inside the textbox who's name is stored in vsave. For example, button "b4s", when clicked, will make VSave = "b4". But b4 is also the name of a textbox and I need to take "b4.text" and put it into Writestring.

    The long was of doing this would be to put code in every button's properties saying "b4.text", etc, but there are hundreds of buttons - what I need is a way of dynamically referancing any given textbox's content depending on which button was clicked.

    Obviously I need to say "WriteString = b4.text", but that b4 could also be b1, b2, b3, etc etc.. So what I'd like to do is somehow say "Writestring = <WhicheverButtonTheUserClicked>.text"

    Is this possible?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Operating on a Variable's properties

    LaVolpe - Interesting, just trying to understand this code.. What is "theControl"?

    EDIT: Nevermind, I got it working using your solution LaVolpe, thanks a lot!
    Last edited by BubbleLife; Sep 2nd, 2009 at 01:49 PM.

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Operating on a Variable's properties

    What that function is asking for in its parameter is a control. theControl, in this case, is just a placeholder and reference to whatever control is passed to the function. Since you are passing a textbox I assume (c1, c2, etc are textboxes), theControl can be used as if it were the textbox itself.

    P.S. In the unedited version of the code I posted, I had the command button being passed. It was updated to pass the textbox. Take a new look at the posted code.

    Last but not least. If you used control arrays (i.e., indexed command buttons and indexed textboxes), you can reduce the amount of code considerably.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: [RESOLVED] Operating on a Variable's properties

    That's brilliant! Thank you for the advice there, it works well!

    I was curious about that, regarding arrays, as it suggested using them when creating lots of command buttons. Do you have any advice for working with arrays in this way? I assume the method I used for instructing each button to do something:

    Code:
    Private Sub a1s_Click()
    Call SaveMe(a1)
    End Sub
    
    Private Sub a2s_Click()
    Call SaveMe(a2)
    End Sub
    
    Private Sub a3s_Click()
    Call SaveMe(a3)
    End Sub
    
    Private Sub a4s_Click()
    Call SaveMe(a4)
    End Sub
    
    Private Sub a5s_Click()
    Call SaveMe(a5)
    End Sub
    
    Private Sub a6s_Click()
    Call SaveMe(a6)
    End Sub
    ... etc could be simplified into a couple of lines if one knew how?

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [RESOLVED] Operating on a Variable's properties

    You're welcome.

    Note: If you will only pass textboxes to the SaveMe, you might want to change "As Control" to "As VB.TextBox". That way you will get VB's intellisense.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [RESOLVED] Operating on a Variable's properties

    Regarding control arrays and using your 6 buttons and textboxes for example...

    Make a copy of your project first, just in case you don't want to save the following changes. Then, in that copied project:
    Code Update
    Delete each of the subs for a1s thru a6s

    Form Design Update
    1. Click on your 1st command button
    2. In the property sheet change its Index property from blank to 0
    3. In the property sheet change its name if desired. Then copy the name in memory (Ctrl+C)
    4. Click on each of the next 5 buttons and
    -- in its property sheet, click on its Name property
    -- hit Ctrl+V (paste the name you copied in memory)
    -- its Index property will be incremented by one

    Do pretty much the same thing for the textboxes, renaming your 1st textbox if desired.

    Update the Code
    1. In your form's code window, click on the command button's name in top left dropdown box
    2. In the click event add this. Note I am using SomeButton and SomeTextBox because I don't know what you decided to name your indexed controls. Change their names in the example as appropriate.
    Code:
    Private Sub SomeButton_Click(Index As Integer)
    Call SaveMe(someTextBox(Index))
    End Sub
    Last edited by LaVolpe; Sep 2nd, 2009 at 02:19 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: [RESOLVED] Operating on a Variable's properties

    That's brilliant, thank you so much for the assistance with that - I'm afflicted that my only exposure to VB education was one unit of one college course 3 years ago, so as you can imagine I'm a little bit of a newbie to say the least! I appreciate the help! I'll definately give this a go.

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