473,657 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with Select Case

8 New Member
I'm new to VB express 2005 and am having some trouble with coding.

I'm using a CASE structure in my program and i'm wondering if anyone can help me. Instead of Saying "Case 1,2,3,4" is there a way of saying "1 to 4". I've tried everything and nothing works.
(TextBox1.Text >= 1) And (TextBox1.Text <= 4) didn't work either.

PS: the actual problem is a bit more complex than just 1,2,3,4

Thanks guys, hope you can help.

Expand|Select|Wrap|Line Numbers
  1. Select Case TextBox1.Text
  2.             Case 1, 2, 3, 4
  3.                 Generator(TextBox1.Text)
  4.                 Exit Select
  5.             Case Else
  6.                 MsgBox("Please enter numeric values between 1 to 150.", vbOKOnly, "Generate_1")
  7.                 TextBox1.Clear()
  8.                 TextBox1.Focus()
  9.                 Exit Select
  10. End Select
Jul 18 '07 #1
18 2232
hariharanmca
1,977 Top Contributor
I'm new to VB express 2005 and am having some trouble with coding.

I'm using a CASE structure in my program and i'm wondering if anyone can help me. Instead of Saying "Case 1,2,3,4" is there a way of saying "1 to 4". I've tried everything and nothing works.
(TextBox1.Text >= 1) And (TextBox1.Text <= 4) didn't work either.

PS: the actual problem is a bit more complex than just 1,2,3,4

Thanks guys, hope you can help.

Select Case TextBox1.Text
Case 1, 2, 3, 4
Generator(TextB ox1.Text)
Exit Select
Case Else
MsgBox("Please enter numeric values between 1 to 150.", vbOKOnly, "Generate_1 ")
TextBox1.Clear( )
TextBox1.Focus( )
Exit Select
End Select
better you can use if condition.

Expand|Select|Wrap|Line Numbers
  1. Select Case val(TextBox1.Text)
  2.             Case 1, 2, 3, 4
  3.                 Generator(TextBox1.Text)
  4.             Case Else
  5.                 MsgBox("Please enter numeric values between 1 to 150.", vbOKOnly, "Generate_1")
  6.                 TextBox1.Clear()
  7.                 TextBox1.Focus()
  8. End Select
just try this
Jul 19 '07 #2
Killer42
8,435 Recognized Expert Expert
In VB6, you would say Case 1 To 4. Don't know about your version, though. What have you tried?
Jul 19 '07 #3
hariharanmca
1,977 Top Contributor
In VB6, you would say Case 1 To 4. Don't know about your version, though. What have you tried?

I never tried any thing. Just suggested him to use ‘if’ condition
Jul 19 '07 #4
Killer42
8,435 Recognized Expert Expert
...just try this
Try what? The IF statement?

Functionally, Select Case is about the same as an IF and a bunch of ELSEIFs. But one of the great things about Select Case has always been the way it allows you to write more easily readable code. It's much easier to interpret at a glance the meaning of something like "15 To 38" than "If blah >= 15 And blah <= 38 Then".
Jul 19 '07 #5
Killer42
8,435 Recognized Expert Expert
I never tried any thing. Just suggested him to use ‘if’ condition
I wasn't asking you. That was a response to the original poster.
Jul 19 '07 #6
nfenlon
8 New Member
Thanks guys. I've tried "Case 1 To 4", I've tried a series of "If Else" statements, I've tried "B>=1 And B<=4" but none of these work.
The full code i'm using checks a textbox for Null, for values too great, for values too small and for characters in order to trap erroneous entries. The only case that calls my function in if the entry is between 1 to 150. I have it working at the moment but i still thing there's a bug because the "Case Else" condition is never met, event when you enter a character! I thought maybe it's due to the fact that i'm checking characters and numeric values????

Hope you can help, the code is below.
Thanks guys!

See what i mean. The code is untidy!!!

Expand|Select|Wrap|Line Numbers
  1. Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         '
  3.         Select Case TextBox1.Text
  4.             Case 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150 
  5.                 Generator(TextBox1.Text)
  6.                 Exit Select
  7.             Case Is = ""
  8.                 MsgBox("Please enter numeric values between 1 to 150.", vbOKOnly, "Generate_1")
  9.                 TextBox1.Clear()
  10.                 TextBox1.Focus()
  11.                 Exit Select
  12.             Case Is > 150
  13.                 MsgBox("Please enter numeric values between 1 to 150.", vbOKOnly, "Generate_2")
  14.                 TextBox1.Clear()
  15.                 TextBox1.Focus()
  16.                 Exit Select
  17.             Case Is < 1
  18.                 MsgBox("Please enter numeric values between 1 to 150.", vbOKOnly, "Generate_3")
  19.                 TextBox1.Clear()
  20.                 TextBox1.Focus()
  21.                 Exit Select
  22.             Case Else
  23.                 MsgBox("Please enter numeric values between 1 to 150.", vbOKOnly, "Generate_4")
  24.                 TextBox1.Clear()
  25.                 TextBox1.Focus()
  26.                 Exit Select
  27.         End Select
  28.         '
  29.     End Sub
Jul 19 '07 #7
hariharanmca
1,977 Top Contributor
Try what? The IF statement?

Functionally, Select Case is about the same as an IF and a bunch of ELSEIFs. But one of the great things about Select Case has always been the way it allows you to write more easily readable code. It's much easier to interpret at a glance the meaning of something like "15 To 38" than "If blah >= 15 And blah <= 38 Then".
i said for only this case. because, that was very few.
Jul 19 '07 #8
cyberdaemon
38 New Member
Thanks guys. I've tried "Case 1 To 4", I've tried a series of "If Else" statements, I've tried "B>=1 And B<=4" but none of these work.
The full code i'm using checks a textbox for Null, for values too great, for values too small and for characters in order to trap erroneous entries. The only case that calls my function in if the entry is between 1 to 150. I have it working at the moment but i still thing there's a bug because the "Case Else" condition is never met, event when you enter a character! I thought maybe it's due to the fact that i'm checking characters and numeric values????

Hope you can help, the code is below.
Thanks guys!

See what i mean. The code is untidy!!!

Private Sub Button1_Click(B yVal sender As Object, ByVal e As System.EventArg s) Handles Button1.Click
'
Select Case TextBox1.Text
Case 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150
Generator(TextB ox1.Text)
Exit Select
Case Is = ""
MsgBox("Please enter numeric values between 1 to 150.", vbOKOnly, "Generate_1 ")
TextBox1.Clear( )
TextBox1.Focus( )
Exit Select
Case Is > 150
MsgBox("Please enter numeric values between 1 to 150.", vbOKOnly, "Generate_2 ")
TextBox1.Clear( )
TextBox1.Focus( )
Exit Select
Case Is < 1
MsgBox("Please enter numeric values between 1 to 150.", vbOKOnly, "Generate_3 ")
TextBox1.Clear( )
TextBox1.Focus( )
Exit Select
Case Else
MsgBox("Please enter numeric values between 1 to 150.", vbOKOnly, "Generate_4 ")
TextBox1.Clear( )
TextBox1.Focus( )
Exit Select
End Select
'
End Sub


You are converting TextBox1.text to integer and (integer <> string) this is why it does not work.

try

select case ctype(textbox1. text, integer)
...


or

select case textbox1.text
case "1", "2", "3", "4", ... , "148", "149", "150"
...



the ... in the case is me not writing every number, hopefully this helps you. Good luck and happy coding

Cyberdaemon
Jul 19 '07 #9
Killer42
8,435 Recognized Expert Expert
You are converting TextBox1.text to integer and (integer <> string) this is why it does not work.
I wouldn't be too certain about that. VB would probably have to do the same string-to-number conversion when it compares them, so the test may work.

Personally, I feel that if the To clause no longer works (thanks a heap, M$) then ELSEIF is the way to go. It allows something much easier to read. For example (I don't know VB.Net, so my syntax is probably messed up)...
Expand|Select|Wrap|Line Numbers
  1. Dim TheNumber As Single
  2. TheNumber = TextBox1.Text ' You can use Val() or CSng() if you like.
  3.  
  4. If TextBox1.Text = "" Then
  5.   MsgBox("Please enter numeric values between 1 to 150.", vbOKOnly, "Generate_1")
  6.   TextBox1.Clear()
  7.   TextBox1.Focus()
  8. ElseIf TheNumber > 150 Then
  9.   MsgBox("Please enter numeric values between 1 to 150.", vbOKOnly, "Generate_2")
  10.   TextBox1.Clear()
  11.   TextBox1.Focus()
  12. ElseIf TheNumber < 1 Then
  13.   MsgBox("Please enter numeric values between 1 to 150.", vbOKOnly, "Generate_3")
  14.   TextBox1.Clear()
  15.   TextBox1.Focus()
  16. ElseIf TheNumber <> Int(TheNumber) Then
  17.   ' Not a whole number.
  18.   MsgBox("Please enter numeric values between 1 to 150.", vbOKOnly, "Generate_4")
  19.   TextBox1.Clear()
  20.   TextBox1.Focus()
  21. Else
  22.   Generator(TextBox1.Text)
  23. End If
Jul 19 '07 #10

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

Similar topics

6
7861
by: R.Wieser | last post by:
Hello All, I'm trying to get a "Virtual Listbox" to work. I've currently got a form, and used CreateWindowExA to create a ListBox with the LBS_OWNERDRAWFIXED and LBS_NODATA flags on it. I've allso subclassed the window and do see all kinds of WS_??? messages coming by. But now I'm stuck :-\ I've got *no* idea what to do next, and all my searching on the web leads me
4
7299
by: Crystal | last post by:
Ok, I know this sounds weird, but it's really bugging me. I have a few list boxes on my form (basic pick a month, year, state stuff) and you can only choose one value. I need to be able to run a script to pick out those boxes and request the value and then response.write the value to a URL that I am creating. The script actually sends an email to me that contains a link to the form that the user filled in and populates the form with...
9
3123
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use SUBSTRING(ProductName, 1, CHARINDEX('(', ProductName)-2). I can get this result, but I had to use several views (totally inefficient). I think this can be done in one efficient/fast query, but I can't think of one. In the case that one query is not...
1
1131
by: Mike | last post by:
Hey guys need some help on the steps getting from one point to another. Here is the easy part. I have a combo box that is being loaded with 3 choices (small, medium, large) no problem here. Then I have 6 checkboxes (red, blue, green, yellow, white, black) no problem here. Here is where I am hung up, which is unfortunately the biggest piece! What I would like to do is to allow the user to choose a size and any
25
2688
by: Bjørn T Johansen | last post by:
I need to write a SQL that calculates the interval between a start time and a stop time. This is the easy part. The problem is that I only have the time part, i.e. no date, so how can I be sure to also calculate the interval if the start time is before midnight and the stop time is after midnight? Regards, BTJ
1
3328
by: Richard Hollenbeck | last post by:
Hello Newsgroup. You have all been very helpful in the past and I thank you. I try to ask relevant questions so that they don't just benefit me, but also benefit the group. I'm currently overwhelmed by useless examples across the web on how to make "dynamic crosstab reports" without myself having a basic understanding about how to retrieve and assign recordsources, etc., from fields in a query to fields in the report. I see all these...
0
2146
by: dnphamus13 | last post by:
I'm new to this and drowning right now. I would like to put my database online for viewing. I managed to do the filtering but i need to do PAGING as the XML doc get bigger. From what i understand this is what I need to do; transform the current XML doc with the filterring xsl, then somehow add the paging xsl. That's the part i'm having trouble with. I'm using JavaScript to apply the filterring XSL to XML doc. Filterring is based on what...
0
2252
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional assistance. I say additional because I've already had help which is greatly appreciated. I do try to take the time and understand the provided script in hopes on not having to trouble others on those. But here it goes...
5
3780
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public List<RoleData> GetRoles() { return GetRoles(null, false); }
9
3937
by: pic078 via AccessMonster.com | last post by:
I need serious help - I have a frontend/backend Access database (2 MDE Files) that remains stuck in task manager after exiting the application - you can't reopen database after exiting as a result - I have read every post out there and spent hours trying to figure out the problem with no success whatsoever - I have constrained the problem to one form however, and I think it's hiding somewhere in my code associated with this form, which is...
0
8394
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
8306
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
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
8732
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
8503
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,...
1
6164
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
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.