|
-
May 12th, 2005, 11:22 AM
#1
Thread Starter
Addicted Member
[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.
-
May 12th, 2005, 11:26 AM
#2
Re: Q about Locking combobox
Try changing all the And's To Or's 
That should amke it work 
Cheers,
RyanJ
-
May 12th, 2005, 11:53 AM
#3
Re: Q about Locking combobox
Try this:
VB Code:
Option Explicit
Private Sub ValidateTextBox(ParamArray MyTextBoxes() As Variant)
On Error Resume Next
Dim tbTextBox As Variant
For Each tbTextBox In MyTextBoxes
If tbTextBox.Text = vbNullString Then
Combo1.Locked = True
Else
Combo1.Locked = False
End If
Next
End Sub
Private Sub Command1_Click()
ValidateTextBox Text1, Text2, Text3, Text4, Text5, Text6, Text7
End Sub
-
May 12th, 2005, 12:00 PM
#4
Re: Q about Locking combobox
WOW, Nice code Hack!
Cheers,
RyanJ
-
May 12th, 2005, 12:18 PM
#5
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:
Private Sub ValidateTextBox(ParamArray MyTextBoxes() As Variant)
On Error Resume Next
Dim tbTextBox As Variant
For Each tbTextBox In MyTextBoxes
If tbTextBox.Text = vbNullString Then
Combo1.Locked = True
[HL="#FFFF80"]Exit For[/HL]
Else
Combo1.Locked = False
End If
Next
End Sub
Private Sub Command1_Click()
ValidateTextBox Text1, Text2, Text3, Text4, Text5, Text6, Text7
End Sub
-
May 12th, 2005, 12:22 PM
#6
Re: Q about Locking combobox
 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:
Private Sub ValidateTextBox(ParamArray MyTextBoxes() As Variant)
On Error Resume Next
Dim tbTextBox As Variant
For Each tbTextBox In MyTextBoxes
If tbTextBox.Text = vbNullString Then
Combo1.Locked = True
[HL="#FFFF80"]Exit For[/HL]
Else
Combo1.Locked = False
End If
Next
End Sub
Private Sub Command1_Click()
ValidateTextBox Text1, Text2, Text3, Text4, Text5, Text6, Text7
End Sub
Yes. The Exit For would make for a tighter routine.
-
May 12th, 2005, 12:26 PM
#7
Re: Q about Locking combobox
Something about skinning a cat seems appropo here....
VB Code:
Private Sub ValidateTextBox(ParamArray MyTextBoxes() As Variant)
On Error Resume Next
Dim tbTextBox As Variant
Combo1.Locked = False
For Each tbTextBox In MyTextBoxes
If Len(Trim$(tbTextBox.Text)) = 0 Then
'If you simply compare against vbNullString, then all the user would need to do is hit the space bar in each text box.
'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.
Combo1.Locked = True
End If
Next
End Sub
Private Sub Command1_Click()
ValidateTextBox Text1, Text2, Text3, Text4, Text5, Text6, Text7
End Sub
Tg
-
May 13th, 2005, 02:33 AM
#8
Thread Starter
Addicted Member
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.
-
May 13th, 2005, 09:07 AM
#9
Thread Starter
Addicted Member
Re: Q about Locking combobox
-
May 13th, 2005, 09:16 AM
#10
Re: Q about Locking combobox
 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.
-
May 13th, 2005, 09:29 AM
#11
Thread Starter
Addicted Member
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?
-
May 13th, 2005, 09:47 AM
#12
Re: Q about Locking combobox
techgnome's code is the cleanest and should do what you want.
-
May 13th, 2005, 09:50 AM
#13
Re: Q about Locking combobox
 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?
-
May 13th, 2005, 09:59 AM
#14
Thread Starter
Addicted Member
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?
-
May 13th, 2005, 10:04 AM
#15
Thread Starter
Addicted Member
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...
-
May 13th, 2005, 10:06 AM
#16
Re: Q about Locking combobox
 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.
-
May 13th, 2005, 10:13 AM
#17
Thread Starter
Addicted Member
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 ?
-
May 13th, 2005, 10:17 AM
#18
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.
-
May 13th, 2005, 01:05 PM
#19
Thread Starter
Addicted Member
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...
-
May 13th, 2005, 01:09 PM
#20
Re: Q about Locking combobox
 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:
If blnDoCheck = True Then
'run validatetextbox routine
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.
-
May 13th, 2005, 01:23 PM
#21
Thread Starter
Addicted Member
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?
-
May 13th, 2005, 01:40 PM
#22
Re: Q about Locking combobox
 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.
 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?
-
May 13th, 2005, 01:54 PM
#23
Thread Starter
Addicted Member
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?
-
May 14th, 2005, 06:33 AM
#24
Thread Starter
Addicted Member
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..
-
May 14th, 2005, 06:58 AM
#25
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:
Combo1.Enabled = (Len(Trim(A.Text)) * Len(Trim(B.Text)) * Len(Trim(C.Text)) * _
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.
-
May 14th, 2005, 08:41 AM
#26
Thread Starter
Addicted Member
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?
-
May 14th, 2005, 04:47 PM
#27
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:
If blnRunThis = True Then
'...run this code
End If
-
May 15th, 2005, 09:44 AM
#28
Thread Starter
Addicted Member
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?
-
May 15th, 2005, 09:52 AM
#29
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:
Private blnSkip As Boolean
In the Change event of the TextBoxes use code simular to this:
VB Code:
Private Sub A_Change()
If blnSkip = False Then 'only run the code if blnSkip is False
Combo1.Enabled = (Len(Trim$(A.Text)) * Len( '... and so on
End If
End Sub
In Command2 use this code:
VB Code:
Private Sub Command2_Click()
blnSkip = True
A.Text = "4"
B.Text = "6"
'... and so on
blnSkip = False
End Sub
-
May 16th, 2005, 06:11 AM
#30
Thread Starter
Addicted Member
Re: Q about Locking combobox
Thanks alot Hack, joacim. I really appreciate your help
-
May 16th, 2005, 06:13 AM
#31
Re: Q about Locking combobox
 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?
-
May 16th, 2005, 06:18 AM
#32
Thread Starter
Addicted Member
Re: [RESOLVED]Q about Locking combobox
-
May 16th, 2005, 07:13 AM
#33
Re: [RESOLVED]Q about Locking combobox
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|