Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Filtering with figures

Posted on 2004-10-11
8
165 Views
Last Modified: 2011-09-20
Hi,

Here's my problem:

My App Users have to enter a minimum age and a maximum age in 2 TextBoxes (respectively : txtAge1 and txtAge2)

Dim Age1 As String = txtAge1.Text
 
Dim Age2 As String = txtAge2.Text

I have a data field in my DataTable that is named "Age".  Therefore "Age" >= Age1 And  "Age" <= Age2 which reads (My code) :

If txtAge1.Text.Trim <> "" And txtAge2.Text.Trim <> "" Then
strFilter += "Age >= '" & txtAge1.Text & "' AND "
strFilter += "Age <= '" & txtAge2.Text & "'    '"
End If

The filter works fine except for certain values e.g. min 5 and max 25. It seems that this is due to the fact that these are STRING comparisons, which means 5 in higher than 25 because 5 > 2.

Does anyboduy know how to resolve this issue?

Thanks
0
Comment
Question by:Ghanisen
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 34

Expert Comment

by:flavo
ID: 12282915
Dim Age1 As integer = cType(txtAge1.Text, integer)

Dave
0
 
LVL 34

Expert Comment

by:flavo
ID: 12282919
Its gonig to throw a casting error if the user entered for exmple a though...  you'll need to handle that.

Maybe on the leave entent on the textbox have something like


If not IsNumberic(Me.txtAge1.text) then
  msgbox "Enter a number mate!"
   me.txtAge1.text = 0
end if

Idea??

Dave
0
 
LVL 7

Expert Comment

by:J_Mak
ID: 12283693
Try converting the String into an integer first before doing the comparisons, like:

Dim int_Age1 as Integer
Dim int_Age2 as Integer

int_Age1 = CInt(Val(txtAge1.Text))
int_Age2 = CInt(Val(txtAge2.Text))

Hope that helps!
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Ghanisen
ID: 12284926
Hi flavo, and J Mak,

Thanks for the suggestions. I had already tried that (converting to integer) but it did not work for one simple reason:

RowFilter is a string and at the end of the day the values converted to integer have to be converted back to string values ans are processed as such in the filtering.

This is a tough issue and I'll try this morning to use an SQL query to filter directely from the database.

Keep on searching please and thanks for your efforts.


0
 
LVL 13

Accepted Solution

by:
Corey2 earned 500 total points
ID: 12286418
Since it is a string filter then you have to have the same number of characters to have a proper numeric comparison.

to handle this try this

Dim Age1len as long
Dim Age2len as long

age1len = len(txtage1.text)

age2len = len(txtage2.text)

while age1len < age2len
     txtage1.text = "0" txtage1.text
     age1len = len(txtage1.text)
wend

'then your comparison

regards,
Corey
0
 
LVL 13

Expert Comment

by:Corey2
ID: 12286433
seems i missed an "&" in this line.

txtage1.text = "0" & txtage1.text
0
 

Author Comment

by:Ghanisen
ID: 12286750
Hi Corey,

You gave me a fantastic solution far better than the one I have found:

If txtAge1.Text <> "" Then
            If txtAge1.Text.Length <> 2 Then
                MsgBox("The minimum and maximum age must both be included between 10 et 99 years!")
                txtAge1.Focus()
            Else
                Exit Sub
            End If
End If

Since my users are adults, my solution works but your's is so elegant that I'm in deep admiration.

Corry you really deserved your 500 points. Many thanks
0
 
LVL 13

Expert Comment

by:Corey2
ID: 12286792
My pleasure.
Thank you for your kind words.

Corey
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question