Solved

Filtering with figures

Posted on 2004-10-11
8
163 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Tabcontrol Caption? 1 31
Close word object 13 38
Help with sorting data in Listbox using VB.NET 3 53
Need a starter for ETL protocol? 4 43
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

867 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now