I am trying to create a Loop that checks a Textbox and makes sure it is not blank. What I came up with gives an Error that the textbox is empty but when I click OK to exit the MessageBox I can't. It will keep popping up
Here is what I Have:
Do Until txtCost.Text <> ""
If txtCost.Text <> "" Then Exit Do
MsgBox ("Ooops. Please Try Again")
Loop
End If
I'm sure I am missing something. I can't get back to my main form to Re-Enter Text into the Textbox........Please Help this newbie
2007-03-09
16:50:26
·
8 answers
·
asked by
savj14
2
in
Computers & Internet
➔ Programming & Design
I am not sure that I explained this correctly. I have a One form that will change the computer name with a button click. Within that button click is where I need to have the user input text in a couple of textboxes. Those need to have Text inside them before the computer name can be created. So I would like to give a message to the user to please try again until both Textboxes contain text.
So it would look like this:
Private Sub Command1_Click()
'Get Inputs from the User
If optDesktop.Value = True Then
strType = "D"
Else
strType = "L"
End If
strCost = txtCost.Text
strPortal = txtPortal.Text
strNewComputer = "K" & strCost & "-" & strPortal & strType
End Sub
Now I just need to make sure that strPortal and strCost have Text entered in the textbox. When I use the Loop I created it just keeps sending me the Message Box. Every time I click OK it appears again.
2007-03-09
17:44:22 ·
update #1
Here is a Link to where my Project Files are located. This will give you a better Idea of what I am talking about.
http://www.vbforums.com/showthread.php?t=456907
2007-03-09
17:55:57 ·
update #2
This is a VALIDATION problem.... So use the VALIDATING and VALIDATED events for the textbox in question....
Do not do the looping check you wrote which I assume you have placed in the button click event.
First create a module level boolean variable to indicated if valid data is available.
private m_DataValid as boolean 'this will be initialized to false upon form load
this boolean will be set or cleared by the validation events and will be checked by the button click event with an IF ElSE THEN statement.
The Validating event fires when the user attempts to leave the check box. This is where you test to see if you have a "" or null
dim strTest as string
strTest = myTextbox.text.trim
'trim removes all whitespace from the text like spaces
this prevents a space from being detected
you can now test several ways
Test for a "" string if strTest = ""
OR test the string lenght is greater than 0
IF strTest.leght > 0
If your tests pass then set
m_DataValid = TRUE
Now if your test fails meaning nothing has been typed you do a few things.
1.) Set your module level variable m_DataValid = FALSE
2.) alert the user with a message box and give them an option to ignore and leave anyway. They may be trying to exit the program.
3.) Depending on the results of the message box you will set the e.cancel property TRUE or FALSE
TRUE = Cancle the attempted move the focus will not shift from the text box
FALSE = Do Not Cancle the attempted move , Focus WILL shift to the next control
You can easily incorporate bells and whistles like changing textbox background colors based upon focus (Yellow) Error(pink) and VALIDATED data (white ir good, ReD if BAD)
The Button Click event now only has to check the m_DataValid
IF m_DataValid THEN
'allow the up date
Else
'Alert the user to place valid text in text box
exit sub
END IF
2007-03-10 01:49:38
·
answer #1
·
answered by MarkG 7
·
0⤊
0⤋
I think u hav so many boxes in ur window
so to check whether a box is empty or not when u click the 'ok' button
do one thing, in your ok button click event
sub okbutton_click()
if txt_cost.text<>""
//do these stuff
else
exit sub
end if
end sub
this will do instead of a loop as the text box is always empty the loop always bring the alert
2007-03-09 17:04:56
·
answer #2
·
answered by Jacksonjosekunnel 3
·
0⤊
0⤋
Interesting software code...try this and see if that doesn't make a difference: Private Sub cmdAsk_Click() Rem Option Explicit Dim MyValue, Response Dim UserInput Dim i As Integer QUESTION: UserInput = InputBox("Ask A Question:", "I know the answer to everything!") Do If UserInput = "" Then MsgBox "You did not ask a question." Else Randomize MyValue = Int((6 * Rnd) + 1) MsgBox MyValue Response = MsgBox("Ask another question? ", vbYesNo) If Response = vbYes Then GoTo QUESTION If Response = vbNo Then Exit Do Me.Caption = i End If Loop
2016-03-28 22:23:49
·
answer #3
·
answered by Anonymous
·
0⤊
0⤋
You need to only check if the textbox is empty when you lose focus and go to another control. Try this code:
Private Sub txtCost_LostFocus()
If txtCost.Text = "" Then
MsgBox ("Ooops. Please Try Again")
txtCost.SetFocus
End If
End Sub
2007-03-09 17:20:00
·
answer #4
·
answered by Travis_L_Smith 2
·
0⤊
0⤋
You don't use a loop to check one textbox. In the code above, if your textbox is empty, your loop will never stop. Remove the code from the loop.
Instead have this:
If txtCost.Text = "" Then
MsgBox ("Ooops. Please try Again")
End if
2007-03-09 17:12:36
·
answer #5
·
answered by gerosna 1
·
0⤊
0⤋
Hi,
Why do you want to use a loop, to check whether the text boxes are empty. You can use something like this..
if trim(txtCost.text) = "" then
Msgbox ("Please try again")
exit sub ' or function (Depends on wht you use)
end if
'Do other stuff
2007-03-09 17:55:42
·
answer #6
·
answered by manjukefernando 1
·
0⤊
0⤋
Do WHILE txtCost<>""
NO IF STATEMENT HERE
statements
LOOP
or
DO UNTIL txtCost = ""
STATEMENTS
LOOP
should work
This is using VB-6 code, since you didn't specify a particular version of the language.
2007-03-09 19:10:49
·
answer #7
·
answered by Richard H 7
·
0⤊
0⤋
Just a guess: should there not be a 'while' in this?
2007-03-09 17:01:06
·
answer #8
·
answered by Anonymous
·
0⤊
0⤋