Results 1 to 3 of 3

Thread: What seems to be the problem with this code? and another Question 8-)

  1. #1

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    Question

    Hello i got a problem!

    well i have been reading this book, this has to be the 3rd time reading it! and here is the code i'm trying to get working:
    [code]
    Option Explicit

    Private Sub Command1_Click()
    ToBeEnabledControl.Enabled = True


    End Sub

    Private Sub Form_Load()
    Dim ctrl As Control
    Dim ToBeEnabledControl As Control

    For Each ctrl In Me.Controls
    If Not (ctrl.Enabled) Then
    Set ToBeEnabledControl = ctrl
    Exit For
    End If
    Next

    End Sub
    [code/]

    it never works....its suppose to search threw all
    the controls on the form and if 1 of the controls
    properites(Enabled property) is false it will
    Enable it...
    but there seems to be some problem....it says
    Variable not defined and the "ControlToBeEnabed"
    is the Var,Please help!

    oh yeah and another question is...
    Should you use Class Moduels just like regualr Moduels?
    Because i wanna learn OOP and i wanna always use that technique(i know VB is all OOP)
    but you know what i mean,
    Like using Classes and what not, connecting everyting yoruself so it is much more cleaning and easier to debug,
    i have been having alot of problems trying to make
    Procedure that changes the caption of
    all controls on a form, or centering text/form
    on screen...
    i think i may be taking it the wrong way about always
    using Class Mods, if i am please inform me

    [Edited by struntz on 08-22-2000 at 12:53 AM]

  2. #2
    Addicted Member
    Join Date
    Jan 2000
    Location
    Sydney, Australia
    Posts
    196
    one of the problems as i see it is that the code to enable the control is in the click event of a button called command1.

    however, the control variable definition:

    Code:
    Dim ToBeEnabledControl As Control
    is in the:

    Code:
    Private Sub Form_Load()
    which means that the code in the command1_click sub does not know of the variable called ToBeEnabledControl.

    On top of this, assuming that the command1_click sub is in fact attached to a command button (assumptions are the brother of all f%^k-ups - lock stock and 2 smoking barrels), this code will not be executed until the button is clicked.

    If what you want is that when the form loads all the controls are enabled then you could approach it like this:

    put the variable definition of ToBeEnabledControl outside of all the subs <like right at the top of the file> - it will then be visible to all the subs.


    Code:
    'change this
    Set ToBeEnabledControl = ctrl
    
    'to this
    Set ToBeEnabledControl = ctrl
    Command1_Click
    
    'so that the enable sub is actually forced to happen

    Note - this is not the best way to do it - like the sub that enables the control shouldn't really be the command1 button click sub. And if all you want to do is enable the control you don't need to do it in a sub, but for a learning experience it could be useful.


    there was a thread a little while back also about the class module versus normal modules thingy,

    i'll try and find it and post a link...

    hope this helps...

  3. #3
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    [/code]Your problem occured because ToBeEnabledControl was declared only in Form_Load, thus was unavailable in Command1_Click.
    You'll get over it.
    Code:
    Option Explicit
    
    Dim ToBeEnabledControl As Control
    
    Private Sub Command1_Click()
        ToBeEnabledControl.Enabled = True
    End Sub
    
    Private Sub Form_Load()
        Dim ctrl As Control
        
        For Each ctrl In Me.Controls
            If Not (ctrl.Enabled) Then
                Set ToBeEnabledControl = ctrl
                Exit For
            End If
        Next
    End Sub

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