Results 1 to 18 of 18

Thread: How to enabling a command only after text boxes are filled

  1. #1
    New Member
    Join Date
    Oct 12
    Posts
    13

    How to enabling a command only after text boxes are filled

    hello

    i want to know How to enabling a command only after text boxes are filled

    in VB6

  2. #2
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,235

    Re: How to enabling a command only after text boxes are filled

    Try this
    Code:
    Private Sub Text1_Change()
        If Text1.Text = vbNullString Then
            Command1.Enabled = False
        Else
            Command1.Enabled = True
        End If
    End Sub
    OR
    Code:
    Private Sub Text1_Change()
        Command1.Enabled = Not (Text1.Text = vbNullString)
    End Sub

  3. #3
    PowerPoster
    Join Date
    Jun 01
    Location
    Trafalgar, IN
    Posts
    3,437

    Re: How to enabling a command only after text boxes are filled

    this would work also
    Code:
    Private Sub Text1_Change()
        Command1.Enabled = Len(Text1.Text)
    End Sub

  4. #4
    Frenzied Member
    Join Date
    May 06
    Location
    some place in the cloud
    Posts
    1,624

    Re: How to enabling a command only after text boxes are filled

    If there are several textboxes the you could iterate thru the form's controls (Controls Collection)
    Declare a boolean... set it to true
    Inside the loop
    Ask if it is textbox (TypeOf Is Textbox) and if it's filled... set the boolean to false if empty
    ...
    Outside of the loop
    Ask for the boolean variable
    If it is true then enable command button
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  5. #5
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,235

    Re: How to enabling a command only after text boxes are filled

    Ask if it is textbox (TypeOf Is Textbox)
    This is VB6 forum

  6. #6
    Frenzied Member
    Join Date
    May 06
    Location
    some place in the cloud
    Posts
    1,624

    Re: How to enabling a command only after text boxes are filled

    TypeOf is a valid keyword in VB6

    The next piece of code changes the forecolor of all Text Box controls on Form1 to red:
    Code:
    Dim c As Control 
    
    For Each c In Form1.Controls
        If TypeOf c Is TextBox Then
            c.ForeColor = vbRed
        End If 
    Next
    You'll also find this approach useful for many other tasks, such as unchecking all of the CheckBox controls.
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  7. #7
    Lively Member
    Join Date
    Aug 09
    Posts
    98

    Re: How to enabling a command only after text boxes are filled

    In form load make command button disabled. In each textbox's change event check for emptiness (Len(Text...)=0) of all text boxes, if any one is empty keep Command Button disabled.

  8. #8
    Super Moderator Hack's Avatar
    Join Date
    Aug 01
    Location
    Searching for mendhak
    Posts
    58,283

    Re: How to enabling a command only after text boxes are filled

    Along the same lines as Smartchap, you can grab the length of a series of textboxes and multiply those lengths together. If any textbox is empty, it will equate to 0 and any number multipled by 0 becomes 0 and 0 will equate to False. Example:
    Code:
    Option Explicit
    
    Private Sub EnableButton() 
    Command1.Enabled = Len(Text1.Text) _
                     * Len(Text2.Text) _
                     * Len(Text3.Text) _
                     * Len(Text4.Text) _
                     * Len(Text5.Text)
    
    End Sub
    
    Private Sub Command2_Click()
    EnableButton
    End Sub
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  9. #9
    New Member
    Join Date
    Oct 12
    Posts
    13

    Re: How to enabling a command only after text boxes are filled

    hello all

    thanks for all but i want that the command button disabled at default then enabled after After writing anything on textbox

  10. #10
    Super Moderator Hack's Avatar
    Join Date
    Aug 01
    Location
    Searching for mendhak
    Posts
    58,283

    Re: How to enabling a command only after text boxes are filled

    Quote Originally Posted by Aiman H View Post
    thanks for all but i want that the command button disabled at default then enabled after After writing anything on textbox
    There are a variety of responses to your question and all of them will do what you want. Pick one.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  11. #11
    New Member
    Join Date
    Oct 12
    Posts
    13

    Re: How to enabling a command only after text boxes are filled

    Quote Originally Posted by Hack View Post
    There are a variety of responses to your question and all of them will do what you want. Pick one.
    hello
    yes i've tried it but the problem is that the command button shift to disabled after writing anything on textbox and clean it

    i'm loocking for any way to disable the command button at default

    for exemple if you open my program you will see the command button disabled at default

  12. #12
    Super Moderator Hack's Avatar
    Join Date
    Aug 01
    Location
    Searching for mendhak
    Posts
    58,283

    Re: How to enabling a command only after text boxes are filled

    Set Enabled = False in design mode, on the button's property page.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  13. #13
    New Member
    Join Date
    Oct 12
    Posts
    13

    Re: How to enabling a command only after text boxes are filled

    Quote Originally Posted by Hack View Post
    Set Enabled = False in design mode, on the button's property page.
    thanks so much
    now it work but there is a small mistake
    i've 3 textbox after writing anything on one textbox, the command button shift to Enabled without considering the two other textbox

    this is the codethat i've used from 4x2y:
    Code:
    Private Sub Text1_Change()
        If Text1.Text = vbNullString Then
            calculateBtn.Enabled = False
        Else
            calculateBtn.Enabled = True
        End If
    End Sub
    
    Private Sub Text2_Change()
        If Text2.Text = vbNullString Then
            calculateBtn.Enabled = False
        Else
            calculateBtn.Enabled = True
        End If
    End Sub
    
    Private Sub Text3_Change()
        If Text3.Text = vbNullString Then
            calculateBtn.Enabled = False
        Else
            calculateBtn.Enabled = True
        End If
    End Sub

  14. #14
    Fanatic Member
    Join Date
    Sep 12
    Location
    To the moon and then left
    Posts
    528

    Re: How to enabling a command only after text boxes are filled

    Or he can do it like this:

    Code:
    Private Function EnableButton()  As Boolean
    EnableButton = Len(Text1.Text) _
                     * Len(Text2.Text) _
                     * Len(Text3.Text) _
                     * Len(Text4.Text) _
                     * Len(Text5.Text)
    
    End Function
    
    'In Every Change-Event of the particular TextBoxes:
    Private Sub Text1_Change()
        Command1.Enabled = EnableButton
    End Sub
    
    'The same in Form_Load-Event
    
    Private Sub Form_Load()
        Command1.Enabled = EnableButton
    End Sub
    For health reasons i try to avoid reading unformatted Code

  15. #15
    Fanatic Member
    Join Date
    Sep 12
    Location
    To the moon and then left
    Posts
    528

    Re: How to enabling a command only after text boxes are filled

    Quote Originally Posted by Aiman H View Post
    thanks so much
    now it work but there is a small mistake
    i've 3 textbox after writing anything on one textbox, the command button shift to Enabled without considering the two other textbox

    this is the codethat i've used from 4x2y:
    Code:
    Private Sub Text1_Change()
        If Text1.Text = vbNullString Then
            calculateBtn.Enabled = False
        Else
            calculateBtn.Enabled = True
        End If
    End Sub
    
    Private Sub Text2_Change()
        If Text2.Text = vbNullString Then
            calculateBtn.Enabled = False
        Else
            calculateBtn.Enabled = True
        End If
    End Sub
    
    Private Sub Text3_Change()
        If Text3.Text = vbNullString Then
            calculateBtn.Enabled = False
        Else
            calculateBtn.Enabled = True
        End If
    End Sub
    To stay in your example:

    vb Code:
    1. Private Function EnableButton()  As Boolean
    2.       EnableButton = Len(Text1.Text) * Len(Text2.Text) * Len(Text3.Text)
    3. End Function
    4.  
    5. Private Sub Text1_Change()
    6.    Command1.Enabled = EnableButton
    7. End Sub
    8.  
    9. Private Sub Text2_Change()
    10.     Command1.Enabled = EnableButton
    11. End Sub
    12.  
    13. Private Sub Text3_Change()
    14.    Command1.Enabled = EnableButton
    15. End Sub
    For health reasons i try to avoid reading unformatted Code

  16. #16
    New Member
    Join Date
    Oct 12
    Posts
    13

    Re: How to enabling a command only after text boxes are filled

    Quote Originally Posted by Zvoni View Post
    To stay in your example:

    vb Code:
    1. Private Function EnableButton()  As Boolean
    2.       EnableButton = Len(Text1.Text) * Len(Text2.Text) * Len(Text3.Text)
    3. End Function
    4.  
    5. Private Sub Text1_Change()
    6.    Command1.Enabled = EnableButton
    7. End Sub
    8.  
    9. Private Sub Text2_Change()
    10.     Command1.Enabled = EnableButton
    11. End Sub
    12.  
    13. Private Sub Text3_Change()
    14.    Command1.Enabled = EnableButton
    15. End Sub
    thanks but the problem is after set these code and run my program
    when it write anything on testbox i get an error with 3 button " end - debug - help"

  17. #17
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,235

    Re: How to enabling a command only after text boxes are filled

    What is the error message?

  18. #18
    New Member
    Join Date
    Oct 12
    Posts
    13

    Re: How to enabling a command only after text boxes are filled

    Quote Originally Posted by 4x2y View Post
    What is the error message?
    Now The Code Work Fine
    Thanks For All

Posting Permissions

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