Seeking item in table column
Seeking item in table column
Seeking item in table column
Seeking item in table column
Seeking item in table column
Seeking item in table column Seeking item in table column Seeking item in table column Seeking item in table column Seeking item in table column Seeking item in table column Seeking item in table column Seeking item in table column
Seeking item in table column Seeking item in table column
Seeking item in table column
Go Back  Xtreme Visual Basic Talk > > > Seeking item in table column


Reply
 
Thread Tools Display Modes
  #1  
Old 02-19-2012, 08:17 AM
Jimj Jimj is offline
Freshman
 
Join Date: Sep 2004
Location: Miami
Posts: 47
Default Seeking item in table column


I am trying seek by looping thru a table for the closest match to a search item.

The following code works to a degree. It gets me within say 20 records of the target. I think it works somewhat but it may not be at all.

Code:
    Private Sub Find_Item_In_Column(ByVal Search_Field As String)
        'Pass a field name to do a string search on
        Dim Find_Item As String = InputBox("Enter an Image File Name")
        Dim Incr As Integer
        TrainImageBindingSource.Sort = "Image_File"

        For Incr = 0 To Train_ImagesDataSet.Tables("TrainImage").Rows.Count - 1
            If IsDBNull(Train_ImagesDataSet.Tables("TrainImage").Rows(Incr).Item(Search_Field)) Then
                ' A blank image_file name is of no use in this instance.
            Else
                If Train_ImagesDataSet.Tables("TrainImage").Rows(Incr).Item(Search_Field) >= Trim(Find_Item) Then
                    TrainImageBindingNavigator.PositionItem.Text = Incr.ToString

                    TrainImageBindingSource.Position = Incr
                    Exit For
                End If
            End If
        Next

    End Sub
I used the data wizard to establish the connection and I am using VB 2008 Express.

In this case the data field will be a file name and it will be unique. Since the search for string will be a partial of what a file name might be. My expectation is that INCR will match the data row and can then be used to position the TrainImageBindingSource. So far it gets me close, but I am wondering if it is really working at all. I would like to arrive at the closest match (up or down one) found.

Can any one see any glaring errors or point me to an example(s) of similar code snippets?
Reply With Quote
  #2  
Old 02-19-2012, 11:33 AM
Gruff's Avatar
GruffSeeking item in table column Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Why are you not using a SQL query to return a recordset of rows that match your criteria then step through them?

"SELECT * FROM TrainImage WHERE Search_Field LIKE " & Find_Item & "%"
LEARN ABOUT LIKE HERE
I'd not use databound controls...Ever.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #3  
Old 02-19-2012, 03:53 PM
Jimj Jimj is offline
Freshman
 
Join Date: Sep 2004
Location: Miami
Posts: 47
Default

I was using this project to find out what could and couldn't be done with databound controls ( by someone like me whose day job is not programming.) It is for my own use and VB.net practice.

But given that, if I use the Sql query to return the record set, will the bindingsource position be placed where I want it? (if I am asking the right question).

I have noticed that the Google searches I have undertaken about this subject generally refer back to doing a query as the much preferred method. There must be a reason SEEK was done away with, but I don't understand why.

Also, when I loop thru I arrive at a row in the table that is where I want to be. However, I haven't found away to position the binding source on that same record. Setting the bindingsource.position to ...rows.Indexof doesn't get me on the target record, but rather a few away.

I would expect there is a link, but I haven't found it. Should I be looping thru by incrementing the binding source position instead of the table?

Thanx for you help and any enlightenment wil be appreciated.
Reply With Quote
  #4  
Old 02-20-2012, 10:44 AM
Jimj Jimj is offline
Freshman
 
Join Date: Sep 2004
Location: Miami
Posts: 47
Default

This expierience is raising new questions. I have an approach that works better in terms of doing the lookup. But it is a kludgey way of getting there.

Now my question is, if I have a given row in a table, can I then expose (or otherwise reveal) the corresponding position in the bindingsource?

I loop thru the table rows to get the closest match. If I set the bindingsource.position to the table's row number I get a lower position number than the target row, but it is close. I then do a bindingsource.movenext loop until I match the search string with a textbox tha contains it. Then I am at the target record

Code:
        For Each row As DataRow In Train_ImagesDataSet.Tables("TrainImage").Rows
            If row.Item(Search_Field) <= Find_Item Then
            Else
                TrainImageBindingSource.Position = Train_ImagesDataSet.Tables("TrainImage").Rows.IndexOf(row)
                Exit For
            End If
        Next
        'Loop using the bindingsource.position to get to actual target record
        Train_ImagesDataSet.
        For Incr = TrainImageBindingSource.Position To TrainImageBindingSource.Count - 1
            TrainImageBindingSource.MoveNext()

            a = Microsoft.VisualBasic.Left(Image_FileTextBox.Text, Len(Trim(Find_Item)))
            If a <= Trim(Find_Item) Then
            End If
            If a = Trim(Find_Item) Then
                Exit For
            End If
            If a >= Trim(Find_Item) Then
                Exit For
            End If
        Next
Reply With Quote
  #5  
Old 02-20-2012, 12:53 PM
Jimj Jimj is offline
Freshman
 
Join Date: Sep 2004
Location: Miami
Posts: 47
Default Now it works and nicely.

I have achieved my version of a SEEK with bound data.

I loop thru the table to find the closest match to my search string. with that I get an exact match to an item that will be in a field. ie if the field contains "Guess What" and I search for "Guess" I need that field contents to use the second step in my search.

Now I use the BindingSource.Find to get to the exact record. And this works very well.

Code:
    Private Sub Find_Item_In_Column(ByVal Search_Field As String)
        'Pass a field name to do a string search on
        Dim Find_Item As String = InputBox("Enter an Image File Name")
        TrainImageBindingSource.Sort = (Search_Field)
        'This gets the the closest item to the string in Find item 
        For Each row As DataRow In Train_ImagesDataSet.Tables("TrainImage").Rows
            If row.Item(Search_Field) < Find_Item Then
            Else
                Find_Item = row.Item(Search_Field)
                Exit For
            End If
        Next

        'TrainImageBindingSource.DataSource = to Train_ImagesDataSet in this case and is already set
        Dim itemFound As Integer = TrainImageBindingSource.Find(Search_Field, Find_Item)
        TrainImageBindingSource.Position = itemFound
        Exit Sub

    End Sub
I don't know how universal this is, but it gets me what I wanted.

If any one knows how to soft/fuzzy/wildcard search with BindingSource.Find I would like to know about it.
Thanx
Reply With Quote
  #6  
Old 02-21-2012, 10:10 AM
Gruff's Avatar
GruffSeeking item in table column Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Jimj,

Hopefully you come back to this thread and read this.

Using Queries is the most efficient way to find things in a database. They take care of
all the complexity for you. Imagine you wanted to find records for all Blue eyed girls with red hair between ages 20 and 30 over six foot tall that live in Miami who's first name begins with "Jan". How much work would it take to do that with the method your using?

Further if you live in Miami but the database your accessing is in Toronto. A server side query would only have to send you the few resulting rows across the web rather than the entire contents of a table or multiple tables.

DataBound programs can use queries as well.

What I have against databound systems is that in older (and improperly constructed new) database programs is that it can limit or deny access to more than one user at a time and that they are inflexible should you want to make change.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #7  
Old 02-21-2012, 12:08 PM
Jimj Jimj is offline
Freshman
 
Join Date: Sep 2004
Location: Miami
Posts: 47
Default

Gruff,

To quite a degree you are preaching to the choir, but I do appreciate that you provided your points. They make sense to me.

My amateur progamming efforts (it isn't my day job) are most extensive in what is now an old data command language called Clipper, and I do miss the record pointer. I also miss what was called seek and I never got relational. I did find it a little frustrating that the seek concept seems to be danced around in VB while SQL implements a very powerful version of it.

At any rate, this project is for my own use and is probably to specific to be of wider use. My implementation gives instant results as things currently stand.

My next step is to improve on where I am at and continue my learning and relearning. I will now pursue following your first suggestion because at this time I have to learn about how it all fits together and what I can put together. While this is fun, it doesn't all come easy. That is why I appreciate this forum.

Thanx
Jim Jones
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

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
Seeking item in table column
Seeking item in table column
Seeking item in table column Seeking item in table column
Seeking item in table column
Seeking item in table column
Seeking item in table column Seeking item in table column Seeking item in table column Seeking item in table column Seeking item in table column Seeking item in table column Seeking item in table column
Seeking item in table column
Seeking item in table column
 
Seeking item in table column
Seeking item in table column
 
-->