Results 1 to 33 of 33

Thread: [RESOLVED]Q about Locking combobox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    156

    [RESOLVED]Q about Locking combobox

    Ok, There are 7 textboxes, named A-G and a combobox(combo1)
    I'm trying to block user's accesss to the combobox if the 7 textboxes are still empty,
    so the user has to fill in all 7 texboxes with any number to be able to access the combobox.
    So i lock the combobox at the start of the program :

    Code:
    Private Sub Form_Load()
    
    combo1.locked = true
    
    End Sub
    then i put this code in each textbox (A.text until G.text) :

    Code:
    If Not Len(A.Text) And Not Len(B.Text) And Not Len(C.Text) And Not Len(D.Text) And Not Len(E.Text) And Not Len(F.Text) And Not Len(G.Text) = 0 Then
    combo1.locked = false
    Now the problem is :
    If i put the numbers in order from A to G, the combobox would be unlocked.
    OK, at first I thought there was no problem...
    but if i put a number only in G a(the last textbox, the other textboxes are still empty) the combobox would be unlocked as well,
    now i don't want this to happen because the user has to fill in all textboxes before he could access the combobox,
    So how do make it work like that?
    Last edited by Deny Winarto; May 16th, 2005 at 06:13 AM.

  2. #2
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Q about Locking combobox

    Try changing all the And's To Or's

    That should amke it work

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

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

    Re: Q about Locking combobox

    Try this:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub ValidateTextBox(ParamArray MyTextBoxes() As Variant)
    4.         On Error Resume Next
    5.         Dim tbTextBox As Variant
    6.         For Each tbTextBox In MyTextBoxes
    7.            If tbTextBox.Text = vbNullString Then
    8.               Combo1.Locked = True
    9.            Else
    10.               Combo1.Locked = False
    11.            End If
    12.         Next
    13. End Sub
    14.  
    15. Private Sub Command1_Click()
    16. ValidateTextBox Text1, Text2, Text3, Text4, Text5, Text6, Text7
    17. End Sub

  4. #4
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Q about Locking combobox

    WOW, Nice code Hack!

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Q about Locking combobox

    Don't you need to do this, since the way it is now any filled textbox will unlock the combo?

    Option Explicit

    VB Code:
    1. Private Sub ValidateTextBox(ParamArray MyTextBoxes() As Variant)
    2.         On Error Resume Next
    3.         Dim tbTextBox As Variant
    4.         For Each tbTextBox In MyTextBoxes
    5.            If tbTextBox.Text = vbNullString Then
    6.               Combo1.Locked = True
    7.               [HL="#FFFF80"]Exit For[/HL]
    8.           Else
    9.               Combo1.Locked = False
    10.            End If
    11.         Next
    12. End Sub
    13.  
    14. Private Sub Command1_Click()
    15. ValidateTextBox Text1, Text2, Text3, Text4, Text5, Text6, Text7
    16. End Sub

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

    Re: Q about Locking combobox

    Quote Originally Posted by MartinLiss
    Don't you need to do this, since the way it is now any filled textbox will unlock the combo?

    Option Explicit

    VB Code:
    1. Private Sub ValidateTextBox(ParamArray MyTextBoxes() As Variant)
    2.         On Error Resume Next
    3.         Dim tbTextBox As Variant
    4.         For Each tbTextBox In MyTextBoxes
    5.            If tbTextBox.Text = vbNullString Then
    6.               Combo1.Locked = True
    7.               [HL="#FFFF80"]Exit For[/HL]
    8.           Else
    9.               Combo1.Locked = False
    10.            End If
    11.         Next
    12. End Sub
    13.  
    14. Private Sub Command1_Click()
    15. ValidateTextBox Text1, Text2, Text3, Text4, Text5, Text6, Text7
    16. End Sub
    Yes. The Exit For would make for a tighter routine.

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

    Re: Q about Locking combobox

    Something about skinning a cat seems appropo here....
    VB Code:
    1. Private Sub ValidateTextBox(ParamArray MyTextBoxes() As Variant)
    2.         On Error Resume Next
    3.         Dim tbTextBox As Variant
    4.  
    5.        Combo1.Locked = False
    6.  
    7.         For Each tbTextBox In MyTextBoxes
    8.            If Len(Trim$(tbTextBox.Text)) = 0 Then
    9.  
    10.              'If you simply compare against vbNullString, then all the user would need to do is hit the space bar in each text box.
    11.              'By using Trim, the space would be eliminated, and then you check the LEN of the resulting text and see if there's anything left.
    12.               Combo1.Locked = True
    13.            End If
    14.         Next
    15. End Sub
    16.  
    17. Private Sub Command1_Click()
    18. ValidateTextBox Text1, Text2, Text3, Text4, Text5, Text6, Text7
    19. End Sub

    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??? *

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    156

    Re: Q about Locking combobox

    Thanks guys, but that function still does the same thing as the code i used before..
    It only works if you enter the numbers in order(from textbox 1 to 7).
    If you put in a number in the 7th textbox first, press the command, the combobox will be unlocked..
    The other textboxes are supposed to be filled in first before it's unlocked.

    Also is there anyway to change to code so that it doesn't have to be called through a control?
    Or could the order be changed?
    Instead of enter numbers -> call function (in this case by clicking command1)
    could it be changed to
    call function -> enter numbers?

    So i can press command1 before entering the numbers first rather than pressing it after entering the numbers.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    156

    Re: Q about Locking combobox

    Bump

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

    Re: Q about Locking combobox

    Quote Originally Posted by Deny Winarto
    Thanks guys, but that function still does the same thing as the code i used before..
    It only works if you enter the numbers in order(from textbox 1 to 7).
    If you put in a number in the 7th textbox first, press the command, the combobox will be unlocked..
    We can address where and how to call it in a moment, but I'd like to focus on this. Now matter what I order I put stuff in what text box, the combo remains locked until all have something them when I use my (thrice updated ) ValidateTextBox routine. Would you please post the code for what you are doing. I can attach a sample project that demonstrates that this does work, but I'd like to look at what you are doing first.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    156

    Re: Q about Locking combobox

    Hmmmm..that's weird...I only copy-paste your first code..so i should get the same result..What did you change anyway?
    I can't tell you my code cause right now i'm not at home...maybe i'll try your latest code first and i'll tell you later.
    Could you answer my other question? so i can try it as well when i get back home. I want the order to be click command then enter number, what's the code to do that?

  12. #12

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

    Re: Q about Locking combobox

    Quote Originally Posted by Deny Winarto
    Could you answer my other question? so i can try it as well when i get back home. I want the order to be click command then enter number, what's the code to do that?
    If you press it before you enter the numbers then all that is going to result is that the combo box, that is already locked, will remain locked because there is nothing in the textboxes. If you move the validation routine from the command button, the only place that would make sense would be in the Validate routine of the textboxes, but that would mean copy/pasting the routine in seven different places (one for each combo) rather than calling it from one location. The Validate event would fire after the textbox lost focus, so this is an option, although I'm not sure why you would want to do it this way.

    Clearly you want the combo box to remain unaccessible until all seven textboxes are populated. What is the other requirement that you have that would make you want to validate them before they have text entered?

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    156

    Re: Q about Locking combobox

    If you move the validation routine from the command button, the only place that would make sense would be in the Validate routine of the textboxes, but that would mean copy/pasting the routine in seven different places (one for each combo) rather than calling it from one location. The Validate event would fire after the textbox lost focus, so this is an option, although I'm not sure why you would want to do it this way.

    Yes, i think that's what i want...basically i want the last entered textbox to trigger the event. So actually a command button isn't necesarry at all...that's why i asked if the command could be removed..
    So how do i do that? Copy paste the exact same code in all 7 textboxes?

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    156

    Re: Q about Locking combobox

    Hmmmm.. what if i want to use the command button to activate the function, and the last entered textbox to call the function. So that at the start of the program the function is disabled unless the user click the command button.
    I think i may have to do this, because those textboxes have another purpose than this one...

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

    Re: Q about Locking combobox

    Quote Originally Posted by Deny Winarto
    Hmmmm.. what if i want to use the command button to activate the function, and the last entered textbox to call the function. So that at the start of the program the function is disabled unless the user click the command button.
    I think i may have to do this, because those textboxes have another purpose than this one...
    I'm here to give you whatever assistance I can in solving your validatation issue. How you use it is up to you and the needs of your application. You know your app, I don't, so whatever works is the way to go.

    Post back, however, if you can't get that validation routine to work for you.

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    156

    Re: Q about Locking combobox

    If you move the validation routine from the command button, the only place that would make sense would be in the Validate routine of the textboxes, but that would mean copy/pasting the routine in seven different places (one for each combo) rather than calling it from one location. The Validate event would fire after the textbox lost focus, so this is an option, although I'm not sure why you would want to do it this way.

    ...Ok, but could you explain this to me? I want to try this too..
    Do i simply have to copy paste the same code to all textboxes or is there anything that should be changed ?

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

    Re: Q about Locking combobox

    First, remember that the Validate event fires only when the control has lost focus. However, if the text has received focus, then one would hope it would be because the user is putting something in it.

    If you take the one liner from the click event of the command button and paste into the Validate event of each textbox, I believe you will achieve the results you are looking for. Test it out and see what you think.

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    156

    Re: Q about Locking combobox

    Perfect! it works!..but.. i've got another problem now..:P
    How do i disable this function? In my program there are 4 or 5 other functions that use those textboxes.
    And whenever i run the other function,
    it will bump into this function because anyhting that uses those textboxes must go through the validate routine first,
    and sometimes it takes a long time to load it..
    So...how do i turn off this function? Hmmm... what if command1 is used to disable this function? What code should i put in it.
    Please answer it, i don't wanna make another thread just to ask this...

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

    Re: Q about Locking combobox

    Quote Originally Posted by Deny Winarto
    Perfect! it works!..but.. i've got another problem now..:P
    How do i disable this function? In my program there are 4 or 5 other functions that use those textboxes.
    And whenever i run the other function,
    it will bump into this function because anyhting that uses those textboxes must go through the validate routine first,
    and sometimes it takes a long time to load it..
    So...how do i turn off this function? Hmmm... what if command1 is used to disable this function? What code should i put in it.
    Please answer it, i don't wanna make another thread just to ask this...
    Piece of Cake Deny. Create a Form Level Boolean. I dunno, something like blnDoCheck or whatever. Set that boolean to True only when you want the check performed, and then set it back to false immediately thereafter. In the Validate event of your textboxes, say something like
    VB Code:
    1. If blnDoCheck = True Then
    2. 'run validatetextbox routine
    3. End If
    When you are done, set the boolean back to false so that when the other functions use those textboxes, the validatetextbox routine won't run.

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    156

    Re: Q about Locking combobox

    Hmmm..but that means i have to change the value manually, right?
    if i compile my program, wouldn't it be impossible for the user to change the value?

    What if we just call from a function? then the code to call it is placed in each of the commands that's used to trigger the other functions.

    How do i make a function like that?

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

    Re: Q about Locking combobox

    Quote Originally Posted by Deny Winarto
    Hmmm..but that means i have to change the value manually, right? if i compile my program, wouldn't it be impossible for the user to change the value?
    Well, then can't change the value unless you provide them with a machinism to do so.
    Quote Originally Posted by Deny Winarto
    What if we just call from a function? then the code to call it is placed in each of the commands that's used to trigger the other functions.
    So, you are saying take it out of the Validate event and call the check from a specific function? It would seem that the functions you have that use the textboxes (for which this check is not needed) could be simply modified to set blnDoCheck to False when they are run. You can then, in turn, set blnDoCheck to True at the top of the ValidateTextBox routine.

    Wouldn't that work?

  23. #23

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    156

    Re: Q about Locking combobox

    It would seem that the functions you have that use the textboxes (for which this check is not needed) could be simply modified to set blnDoCheck to False when they are run. You can then, in turn, set blnDoCheck to True at the top of the ValidateTextBox routine.

    Well, those functions are triggered with comboboxes and command buttons...so yeah it might be possible...
    But i still don't understand it (sorry i'm still new and i learn VB by myself :P)..
    Why do we have to create a new form for this?
    Couldn't we just put make 2 new functions, 1 to disable and the other 1 to enable this validate event, then place the function to disable in the click event in each controls that triggers the other functions?
    Is that possible?

  24. #24

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    156

    Re: Q about Locking combobox

    Bump.
    Please tell me how to make such function..i'm still a beginner and i don't know much about this..

  25. #25
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Q about Locking combobox

    Here's another solution that you need to put in the Change event of every text box. This would have been simplier if your text boxes would be in a control array.
    VB Code:
    1. Combo1.Enabled = (Len(Trim(A.Text)) * Len(Trim(B.Text)) * Len(Trim(C.Text)) * _
    2.  Len(Trim(D.Text)) * Len(Trim(E.Text)) * Len(Trim(F.Text)) * Len(Trim(G.Text)) > 0)
    If any of the textboxes would be empty you would multiply with zero so the result would be zero.

  26. #26

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    156

    Re: Q about Locking combobox

    Thanks Joacim, i'll try that..
    But actually, my purpose is not just locking or disabling the combobox, i was just using that as an example to make my question simpler
    the real program would add +200 items to 3 comboboxes everytime the validate event runs.

    The problem is, there are other functions that use those 7 textboxes as the output.
    and because the event is placed in the textboxes, all functions, events, procedures,
    or whatever that uses those textboxes must go through the validate event first.

    And it gets annoying because that's not necessary at all, and there's like 10-15 seconds delay whenever i run the other functions (7 textboxes x 200 items )

    So that's why i want to know how to disable or turn off the event where it's not needed, or in this case your code, is it possible to disable the code you just posted?
    I think the code to disable it should be put in the click event of the controls that trigger the other functions that i was talking about..
    What if it's in command1 control?

  27. #27
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Q about Locking combobox

    Well you can't really disable code, but you kan keep it from running by setting some variable flag to True or False.
    VB Code:
    1. If blnRunThis = True Then
    2.    '...run this code
    3. End If

  28. #28

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    156

    Re: Q about Locking combobox

    Errrr...i don't think i understand your answer...
    where should i put that code?
    Maybe i should use the real program as an example :

    command2 adds

    4
    6
    8
    3
    2
    3
    5

    to the textboxes i mentioned before (A to G)
    Assuming we use your code, when i click command2,
    it has to go through your code like this :

    "add 4 to textbox A", textbox A runs your code because there's an item in it.
    "add 6 to textbox B", textbox B runs your code because there's an item in it.
    and so on until textbox G

    So what code should i put in command2 to prevent your code from running?

  29. #29
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Q about Locking combobox

    Well, first of all I don't really understand why you can't let the Change event of the TextBoxes execute since it's so little code you wouldn't see any difference in speed. But if there is a particular reason then in the General Declaration section of your Form declare a Boolean variable.
    VB Code:
    1. Private blnSkip As Boolean
    In the Change event of the TextBoxes use code simular to this:
    VB Code:
    1. Private Sub A_Change()
    2.     If blnSkip = False Then 'only run the code if blnSkip is False
    3.         Combo1.Enabled = (Len(Trim$(A.Text)) * Len( '... and so on
    4.     End If
    5. End Sub
    In Command2 use this code:
    VB Code:
    1. Private Sub Command2_Click()
    2.     blnSkip = True
    3.     A.Text = "4"
    4.     B.Text = "6"
    5.     '... and so on
    6.     blnSkip = False
    7. End Sub

  30. #30

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    156

    Re: Q about Locking combobox

    Thanks alot Hack, joacim. I really appreciate your help

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

    Re: Q about Locking combobox

    Quote Originally Posted by Deny Winarto
    Thanks alot Hack, joacim. I really appreciate your help
    So, does this mean that you got everything working the way you need/want it work?

  32. #32

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    156

    Re: [RESOLVED]Q about Locking combobox

    Yep, why?

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

    Re: [RESOLVED]Q about Locking combobox

    Quote Originally Posted by Deny Winarto
    Yep, why?
    When I first asked RESOLVED wasn't added to the thread title, so I was just wondering if you had any further questions.

    Thats 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
  •  



Click Here to Expand Forum to Full Width