Question:
Visual Basic Looping Problem?
savj14
2007-03-09 21:50:26 UTC
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
Eight answers:
MarkG
2007-03-10 06:49:38 UTC
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
Jacksonjosekunnel
2007-03-09 22:04:56 UTC
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
2016-03-29 02:23:49 UTC
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
Travis_L_Smith
2007-03-09 22:20:00 UTC
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
gerosna
2007-03-09 22:12:36 UTC
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
manjukefernando
2007-03-09 22:55:42 UTC
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
Richard H
2007-03-10 00:10:49 UTC
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 22:01:06 UTC
Just a guess: should there not be a 'while' in this?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...