Solved

Filtering with figures

Posted on 2004-10-11
8
162 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

11 Experts available now in Live!

Get 1:1 Help Now