473,320 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Checking Empty textbox

Ok i have 6 textboxes, and the user is required to fill in each of them. so i want to do a check to make sure none are left blank, but im having a little trouble.

i tried doing

Expand|Select|Wrap|Line Numbers
  1. If txtChoice1.text =  "" Then
  2. MsgBox ("You have missed out a preference.")
  3. Else
  4. If txtChoice2.text = ""  Then
but that never seemed to work.

please help.
Feb 21 '07 #1
11 10162
scripto
143 100+
quick and dirty:

Expand|Select|Wrap|Line Numbers
  1. If txtChoice1.text = "" Then
  2.      MsgBox ("You have missed out a preference.")
  3. elseIf txtChoice2.text = "" Then
  4.      MsgBox ("You have missed out a preference.")
  5. elseIf txtChoice3.text = "" Then
  6.      MsgBox ("You have missed out a preference.")
  7. end if
make elseif one word.

if you're using VB6, look into using arrays of textboxes to make this easier.
Feb 21 '07 #2
quick and dirty:

If txtChoice1.text = "" Then
MsgBox ("You have missed out a preference.")
elseIf txtChoice2.text = "" Then
MsgBox ("You have missed out a preference.")
elseIf txtChoice3.text = "" Then
MsgBox ("You have missed out a preference.")
end if

make elseif one word.

if you're using VB6, look into using arrays of textboxes to make this easier.
it still never worked. even though i fill in all the textboxes - i still get the message.
Feb 21 '07 #3
willakawill
1,646 1GB
it still never worked. even though i fill in all the textboxes - i still get the message.
Try this:
Expand|Select|Wrap|Line Numbers
  1. If Len(Trim(txtChoice1.Text)) = 0 Then
This is a more generic way to do this. It will check all of the textboxes on your form:
Expand|Select|Wrap|Line Numbers
  1. Dim tmp As Control
  2.  
  3.     For Each tmp In Me.Controls
  4.         If TypeOf tmp Is TextBox Then
  5.             If Len(Trim(tmp.Text)) = 0 Then
  6.                 'put your message here
  7.             End If
  8.         End If
  9.     Next tmp
Feb 21 '07 #4
TNT
48
You could use:
Expand|Select|Wrap|Line Numbers
  1. If txtChoice1 <> "" Or txtChoice2 <> "" Or txtChoice3 <> "" Or txtChoice4 <> "" Or txtChoice5 <> "" Or txtChoice5 <> "" Then
  2. 'Code goes here
  3. End If
Feb 22 '07 #5
Killer42
8,435 Expert 8TB
it still never worked. even though i fill in all the textboxes - i still get the message.
That's really odd. Could you copy and paste back here the code which didn't work? Because willakawill's code really should have worked. I'd have to guess that you might have made a mistake in something like a control or variable name.
Feb 22 '07 #6
That's really odd. Could you copy and paste back here the code which didn't work? Because willakawill's code really should have worked. I'd have to guess that you might have made a mistake in something like a control or variable name.
well i couldnt use this code

Expand|Select|Wrap|Line Numbers
  1. Dim tmp As Control
  2.  
  3.     For Each tmp In Me.Controls
  4.         If TypeOf tmp Is TextBox Then
  5.             If Len(Trim(tmp.Text)) = 0 Then
  6.                 'put your message here
  7.             End If
  8.         End If
  9.     Next tmp
because it checks all the textboxes on the form, but i have other texboxes than the 6 that need to be filled in. But thanks for the code anyway - i used it for something else i was doing.

i used Len(Trim(tmp.Text)) = 0 with If statements and it worked

thank you everyone for your help
Feb 22 '07 #7
willakawill
1,646 1GB
You can still use a loop like this just by testing for the common name of your control array:
Expand|Select|Wrap|Line Numbers
  1. Dim tmp As Control
  2.  
  3.     For Each tmp In Me.Controls
  4.         If StrComp(Left(tmp.Name, 9), "txtChoice") = 0 Then
  5.             If Len(Trim(tmp.Text)) = 0 Then
  6.                 'put your message here
  7.             End If
  8.         End If
  9.     Next tmp
Feb 22 '07 #8
You can still use a loop like this just by testing for the common name of your control array:
Expand|Select|Wrap|Line Numbers
  1. Dim tmp As Control
  2.  
  3.     For Each tmp In Me.Controls
  4.         If StrComp(Left(tmp.Name, 9), "txtChoice") = 0 Then
  5.             If Len(Trim(tmp.Text)) = 0 Then
  6.                 'put your message here
  7.             End If
  8.         End If
  9.     Next tmp
that is really helpful. Thanks :)
Feb 22 '07 #9
willakawill
1,646 1GB
You are very welcome
Feb 22 '07 #10
Ok , would you like to use an errorprovider, cause it is better rather than to show a pop-up message everytime a user writes wrong, info. and an errorprovider gives a lot of room for mistakes, wihtout giving off too many msgboxes.

add this on every txtbox


Expand|Select|Wrap|Line Numbers
  1. if txtbox1.text=nothing then
  2. errorprovider1.seterror(txtbox1, "Txtbox must b\not be left blank!") 
  3. e.cancel'  

w/c tells a user that he must place a valid value in the 1st txtbox before he can leave and jump to another txtbox.


add this to the 2 lines above

Expand|Select|Wrap|Line Numbers
  1. elseif
  2.  errorprovider.seterror(txtbox1,"")
  3. 'which lets the user jump to another txtbox if the user entere a right txt or number
  4.  
  5. end sub
add this code to every txtboxes you have for validation purposses.

actually you can still add validating and validated event handler to every txtbox.

hope this may help ya...
Feb 28 '07 #11
willakawill
1,646 1GB
errorprovider is a .net class. No mention of .net in the op's question but that does not mean the op is not using .net. A lot of new programmers who come here don't seem to know what .net is. Largely, I guess, because of the new microsoft naming policy vb 2005
Feb 28 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Tetet B via .NET 247 | last post by:
I'm using VB.Net and trying to retrieve the data in the textboxcell of a datagrid after a successful display (used Databind).User can either display data or enter data in the textbox. I canalways get...
10
by: Samir | last post by:
Say I have 4 forms, all four have different numbers of text boxes. is there a script that I can use to check to make sure everything on the form is not blank?
8
by: Gidi | last post by:
hello, i have a textbox that represnts a date and i want to pare it to a DateTime, but sometimes this textBox can be empty string and i want to send it to the DataBase as null or as empty Date, how...
14
by: Xero | last post by:
Hello. I am using Visual Studio .NET (Academic Edition) to write a VB program. My computer is running Win XP Pro. I am writing a calculator and requires users to enter two numbers. After...
3
by: Husam | last post by:
Hi EveryBody: I made project by Vb.Net which consist the following items: 1\ Textbox 2\Button 3\Listbox When you write any thing in the textbox and press the button any text written in...
1
by: ravipatil | last post by:
hi i am adding two numbers in C#.net & i want to check wheather first textbox is empty or not. if empty, then it must display "enter the first number" otherwise value is stored in first textbox1....
1
by: Brad Pears | last post by:
I am using vb.net 2005 and SQL server 2000. In my table I have a date field of type "smalldatetime". In my vb application, the user may or may not enter a date value into the appropriate text box....
18
by: Academia | last post by:
I let the use modify the text of a combobox and then I replace the selected item with the new text (in Keyup event). But if he sets the Text property to an empty string ("") that sets the...
5
by: bob | last post by:
Hi, i want to check whether the textbox of the detailsview is not left empty, in insert mode. I did this: Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, ByVal e As...
4
by: BillE | last post by:
I have found articles on line about using word interop for spell checking with visual studio applications. Most of the articles are several years old, though - VS2003, maybe 2005. I couldn't...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.