473,699 Members | 2,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Checking Empty textbox

19 New Member
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 10264
scripto
143 New Member
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
IraqiAli
19 New Member
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 Top Contributor
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 New Member
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 Recognized Expert Expert
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
IraqiAli
19 New Member
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.Te xt)) = 0 with If statements and it worked

thank you everyone for your help
Feb 22 '07 #7
willakawill
1,646 Top Contributor
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
IraqiAli
19 New Member
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 Top Contributor
You are very welcome
Feb 22 '07 #10

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

Similar topics

0
1511
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 the textbox object using either the Controls orFindControl method but it's always returning an empty *.Textinstead of the value displayed on the screen. Below are snippetsof my code - anybody there who can help me PLEASE? =====*.vb <edit mode...
10
1378
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
38041
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 can i do it? thanks
14
1590
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 entering the numbers, they should click a button called 'Display Results'. How can I display a dialog box prompting the users to check their entry if they left any of the boxes empty? I have tried the .IsNaN function but in vein. Thanks.
3
4339
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 the textbox will be added to the listbox, but i figure that when
1
1974
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. Similarly for second textbox. and the sum of two numbers is stored third textbox. thank you,
1
31369
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. I then want to pass the value of this text box as a datetime variable to my stored procedure which inserts or updates the row. As I mentioned above, the textbox may be left blank - hence I would want to pass a null to the stored proc. How do I...
18
2170
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 SelectedIndex to -1. Do you have any suggestion for getting around this problem?
5
3626
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 System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles DetailsView1.ItemInserting Dim h As String
4
2964
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 find anything for VS2008. Are there any new improvements in spell checking with VS2008? Thanks Bill
0
8705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9197
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9054
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8941
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8897
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6549
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3071
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.