Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Database and Reporting > Searching numeric values


Reply
 
Thread Tools Display Modes
  #1  
Old 03-28-2004, 09:24 PM
Snoopy24 Snoopy24 is offline
Newcomer
 
Join Date: Feb 2004
Posts: 15
Default Searching numeric values


I am currently writing a organizer type program that has 27 buttons at the top (one for each letter in the alphabet and one for #) in an array. When one of the buttons is clicked, it goes through the database and returns all results that start with the letter that was clicked. Everything for the letters works great but where I am stuck is the searching of numbers (0-9). I don't really want to do a search for every number. I search for the letters as follows:

Char = Index + 64 'Calculates the character code for the letter of the button
With DBConnect.rsGames
.Sort = "Title ASC"
If Not .BOF Then .MoveFirst
.Find "Title like '" & Chr$(Char) & "*'"

Now how would I go about using this to search for ANY number and not specific ones
Reply With Quote
  #2  
Old 03-29-2004, 01:42 AM
wengwashere's Avatar
wengwashere wengwashere is offline
Contributor
 
Join Date: Apr 2002
Location: brgy Ginebra
Posts: 449
Default

you could do range search... where number between this and that...
Reply With Quote
  #3  
Old 03-29-2004, 02:00 AM
Andyh Andyh is offline
Junior Contributor
 
Join Date: Feb 2004
Location: Southampton, UK
Posts: 335
Default

If this is sqlserver (I don't know about Access) you can use regular expressions:

.Find "Title like '[0-9]%'"
Reply With Quote
  #4  
Old 03-29-2004, 06:32 AM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Instead of using the Find method, I would simply use SQL. You could write an SQL SELECT statement that finds all numbers or values LIKE your condition..you would need to use wildcards within your SQL statement (% for ADO, * for DAO).

But if your heart is set on the Find method you can use it with Access...the .Find method should look like this:

Code:
rs.Open sSQL, cn, adOpenKeySet, adLockOptimistic, adCmdText rs.Find "field1 LIKE 'hello'%"

You need to use a valid field within the opened recordset.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #5  
Old 03-29-2004, 06:37 AM
Andyh Andyh is offline
Junior Contributor
 
Join Date: Feb 2004
Location: Southampton, UK
Posts: 335
Default

his problem is not using a wildcard search, It is to find records starting with a numeric character.
Reply With Quote
  #6  
Old 03-29-2004, 06:47 AM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Use a function, such as isnumeric . If we need to check BEYOND the first character, we can write a function that loop until Len() of the recordset value finding ANY numerical characters. But then, what is the final result supposed to be? Say I find that a value starts with a numeric character, then what? If it starts with a number, are we adding it to a Grid control, displaying it somewhere. Something like this:

Code:
rs.open sSQL, cn, adOpenKeySet, adLockOptistic, adCmdText While rs.EOF = False If IsNumeric(Left(rs.Fields("myField").value, 1)) Then [do something] End If rs.moveNext Wend
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #7  
Old 03-29-2004, 06:57 AM
Andyh Andyh is offline
Junior Contributor
 
Join Date: Feb 2004
Location: Southampton, UK
Posts: 335
Default

Quote:
Originally Posted by MKoslof
Use a function, such as isnumeric
You can't put isnumeric into sql . Read snoopy's problem - he wants to retrieve records that START with a numeric character (not just contain a numeric value) when they press the "#" button as opposed to a button with A-Z on it. e.g. "99 red balloons"

this is done by using regular expression searches:

1) object method: .Find "Title like '[0-9]%'"

2) Sql string: select * from table where Title like '[0-9]%'
Reply With Quote
  #8  
Old 03-29-2004, 07:00 AM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Actually, no. YES you can put ISNUMERIC in an SQL Statement...have you tried it before?

And point two, his example uses the .find method, which implies he is OPENING a recordset and SEARCHING through it...something that is exactly the same as using a While loop and incrementing through the recordset.

But I appreciate your input and questions
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #9  
Old 03-29-2004, 08:03 AM
Snoopy24 Snoopy24 is offline
Newcomer
 
Join Date: Feb 2004
Posts: 15
Default Still not working

Thanx to all those that replied. I have tried some of the solutions posted here and am still unable to get this thing to work. As I am not very familiar with SQL statements, I would like to use another solution. Maybe I can explain a bit better. When you click the button for #, it goes to the database and finds the first result with a number as the first character. Once the result is found, it posts all the information on the form. Then there are 2 buttons (Next and previous) which can be used to search for more records that begin with a number. The problem I have is finding the records that begin with a number. The database field that I'm searching is "Title" and has entries like this:
21 Grams
2 Fast 2 Furious
3 Kings
Ace Ventura
Bad Boys
ect...
The database has over 800 records and growing so to loop through each record to see if it is numeric sounds like a lot of extra processing. Reading through the help files I found 2 other commands that might help (Seek and Filter) but haven't got them to work either
Reply With Quote
  #10  
Old 03-29-2004, 08:06 AM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

So use SQL for this...is this an Access mdb, try this:

(*return all records where the field in question STARTS with a number)

SELECT *
FROM Table1
WHERE IsNumeric(Left(field1,1))

The only *other solution* is to open the recordset and test each record, something I also showed a brief example of. There really is no other way to do this because you need to evaluate the records.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #11  
Old 03-29-2004, 08:16 AM
Andyh Andyh is offline
Junior Contributor
 
Join Date: Feb 2004
Location: Southampton, UK
Posts: 335
Default

You cannot use .find unless you load the result set with the whole table in the first place. What you should do is (like I have said before) is use regular expressions. Every time a user presses a button re-run a SQL search with new criteria:

if the user presses a Alpha-character button run this sql

Code:
SqlString = "SELECT * FROM TableName WHERE (((Title) Like '" & Chr$(Char) & "*'"
If the user presses the "#" button use this sql

Code:
"SELECT * FROM TableName WHERE (((Title) Like '[0-9]*'))"
This will work in access and SQLserver; If you don't believe me then run this sql in Northwind.mdb:

Code:
 
SELECT *
FROM Customers
WHERE (((Customers.Address) Like "[0-9]*"));
Reply With Quote
  #12  
Old 03-29-2004, 08:18 AM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

No, if Access, just test for ISNUMERIC. If SQL Server, you could use a CASE within your SELECT to test for the ISNUMERIC function, which returns 0 or 1 in SQL Server. If your condition is simply to find any value that starts with a number, the LIKE search is not needed.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #13  
Old 03-29-2004, 08:21 AM
Snoopy24 Snoopy24 is offline
Newcomer
 
Join Date: Feb 2004
Posts: 15
Default SQL Statement

If I were to use that SQL statement would it return the first result it finds with a number at the beginning and if so how would I tell it to find the next record with a number at the beginning when they press the "Next" button?
Reply With Quote
  #14  
Old 03-29-2004, 08:24 AM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

If you only want the first instance you still need to open the recordset..as I have stated and AndyH has stated. Basically, use the ISNUMERIC() call to open a recordset with only numeric characters as the first character. Then, you use can the .Find method, the .FindFirst method or loop until EOF to bring in specific values.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #15  
Old 03-29-2004, 08:31 AM
Snoopy24 Snoopy24 is offline
Newcomer
 
Join Date: Feb 2004
Posts: 15
Default Resolved

Thanx to all that have posted so quickly. I got it working with Isnumeric.
Reply With Quote
  #16  
Old 03-29-2004, 08:35 AM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

No problem...good luck to you!
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting values from an Excel graph/chart Jon Excel 8 11-26-2003 03:03 AM
Exported numeric values in Excel are text Lintz General 2 10-19-2003 09:53 PM
Adding values to a list box raze0811 Database and Reporting 4 10-02-2003 06:21 PM
Converting values From DB into Data Report intar Database and Reporting 1 12-19-2002 02:09 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->