We help IT Professionals succeed at work.

Filter a form : Like MsAccess ctrl+F

Fordraiders
Fordraiders asked
on
VB.NET 2008
Form1 that is unbound to a dataset.

I want to filter the form1 via a frmlookup form..

Type something in textbox1" on "frmLookup" and then filter the data on Form1.


Form1 declarations:
Dim das1 As New System.Data.DataSet()
Dim cnn1 As New OleDb.OleDbConnection( _
                "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                "Data Source=c:\Program Files\" & _
                "Cr\Beta.mdb")
    Dim query As String = "SELECT * FROM tblMainData" ' Order By MainTableID DESC "
   Dim dap1 As New System.Data.OleDb.OleDbDataAdapter(query, cnn1)


' form1 load event
'Fill the tblMainData table
dap1.Fill(das1, "tblMainData")



Hit a key combination like ctrl+f  and the frmLookup" popus up.

type in a search value in  textbox1  and use the field   "fldMfrnum"  in "das1"


Thanks
fordraiders



Comment
Watch Question

Shahan AyyubSenior Software Engineer

Commented:
Hi!

Do you want to show a form when Ctrl + F presses ??

Author

Commented:
I can get the form to show ok...Its just addressing the rest of the code..
Shahan AyyubSenior Software Engineer

Commented:
use return after the line which displays the form

Author

Commented:
why?

Author

Commented:
LookupFind.Show()
...is all it is..
That works fine...
It filtering the data set on Form1..from this LookupFind form
Shahan AyyubSenior Software Engineer

Commented:
do you not want to run the code after LookupFind.Show()  ??

if not I need more clarification please.

Shahan AyyubSenior Software Engineer

Commented:
>> Its just addressing the rest of the code..

What does it mean ??

Author

Commented:
I want to filter the form1 via a frmlookup form..

Type something in textbox1" on "frmLookup" and then filter the data on Form1.
Shahan AyyubSenior Software Engineer

Commented:
One way as I see: (using delegate

FormLookUp:

1 button
1 textbox

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Form1.Show()
        AddHandler Form1.DataFromLookForm, AddressOf Form1.DataFromLookup
        Form1.DataFromLookup(TextBox1.Text)
    End Sub

Form1:

1 label ( from FrmlookUp we will pass textbox1.Text and will receive in this label of Form1)

    Public Event DataFromLookForm As FromfrmLook
    Public Delegate Sub FromfrmLook(ByVal v As Object)

    Sub DataFromLookup(ByVal v As Object)
        MessageBox.Show(v.ToString())
        Label1.Text = v.ToString()
    End Sub



Is this what you are looking for ??
Shahan AyyubSenior Software Engineer

Commented:
I will recommend you to check this code in a separate project. this will allow you to how to adjust this code in your code.

Author

Commented:
ok...Form1 is a datacollection form..with several textboxes..I could possibly have collected 1-400 records.
When form1 is loaded it could have as many records as I have data collected.

On form1 I have a button that opens frmLookup.  
On frmLookup I have a textbox1 and a command button.
I type a value I'm looking for .
To make it less complicated I'm searching , in a fldCmfrnum.

Once I type something into the textbox1 and hit the command button.
form1 gets filtered to the records I'm searching for.
Like MsAccess does.. it will say 1 of 3 filtered


 

Author

Commented:
On the frmLookup  I'm not getting any errors but nothing happens on form1

Dim SearchJob As String = Me.TextBox1.Text
       frmiCart.MyData.das1.Tables("tblMainData").DefaultView.RowFilter = "fldCmfrnum =" & SearchJob
Shahan AyyubSenior Software Engineer

Commented:
Could you please post some code ??
Shahan AyyubSenior Software Engineer

Commented:
I think you could not succeed in adjust my code in your code?? for that I need portion of your code.

Author

Commented:
ok...what part...my databindings code...when the form loads ?  connection strings ?
Shahan AyyubSenior Software Engineer

Commented:
I am assuming that the code I provided you works for you in the separate project but you are unable to adjust that in your code. I need the code from where you want to pass value to Form1 and where you are receiving value.

Author

Commented:
ok...
        Dim drTarget() As DataRow = das1.Tables("tblMainData").Select("fldCmfrnum = " & TextBox1.Text)
               Label23.Text = drTarget(0).Item("MainTableID")
                Label22.Text = drTarget(0).Item("fldCmfrnum")
' just for reference      
 Label22.Refresh()
' just for reference
       Label23.Refresh()
        'Move to that position
        Me.BindingContext(das1, "tblMainData").Position =  ?? ' <--- move to the "MainTableID" I have found
        ' filling the records all count

this works , Now , if I know the primary id value I want to move to that record..


Shahan AyyubSenior Software Engineer

Commented:
this code is insufficient to understand. i am explaining my code:


   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e  As System.EventArgs) Handles Button2.Click
        Form1.Show()    // Display the form, to which value should be pass.
        // store the address of Form1's DataFromLookup method.
         AddHandler Form1.DataFromLookForm, AddressOf Form1.DataFromLookup  
         Form1.DataFromLookup(TextBox1.Text)  // call method and pass value, to form1
    End Sub

Form1:

1  label ( from FrmlookUp we will pass textbox1.Text and will receive in  this label of Form1)

    Public Event DataFromLookForm As  FromfrmLook
    Public Delegate Sub FromfrmLook(ByVal v As Object)

     Sub DataFromLookup(ByVal v As Object)
      // I think the code you provided SHOULD be placed here and set to ' v ' coming in arguments
       Me.BindingContext(das1, "tblMainData").Position =  v  // convert v to appropriate datatype before assigning , since v is object type here.
        Label1.Text =  v.ToString()   // If you want to display in a label
    End Sub

Author

Commented:
this is actually telling me what I nned to know so far.:
 Dim k As Integer
        Dim j As Integer
        Dim drTarget2() As DataRow = das1.Tables("tblMainData").Select("fldCmfrnum = " & TextBox1.Text)
        Label23.Text = drTarget2(0).Item("MainTableID")
        k = drTarget2(0).Item("MainTableID")
        Label22.Text = drTarget2(0).Item("fldCmfrnum")
        Label22.Refresh()
        Label23.Refresh()

The thing it is not telling me is where in the postion in the dataset this record sits.

mainDataID     fldCmfrnum
1                     rtr5r                       <----Position 1
2                     65567                    <----Position 2
4                     trt6y6                    <----Position 3
7                     ytyt4543               <----Position  4

Im trying to find the position number and move to that record...





Author

Commented:
I search for "ytyt4543" ...I find it and move to position 4 or record 4 on my form...

Shahan AyyubSenior Software Engineer

Commented:
Okay this is fine:

Form1 Code:

 Sub DataFromLookup(ByVal v As Object)
..........
...........
Dim drTarget2() As DataRow = das1.Tables("tblMainData").Select("fldCmfrnum  = " & v.ToString)
.........
.........
.........
        Label1.Text =  v.ToString()   // If you  want to display in a label
    End Sub

v is the value passed from frmlookup and in this case it will be v=ytyt4543

FrmLookup code:

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e   As System.EventArgs) Handles Button2.Click
        Form1.Show()    
         AddHandler Form1.DataFromLookForm, AddressOf Form1.DataFromLookup  
          Form1.DataFromLookup(TextBox1.Text)  //here textbox1's contents should be "ytyt4543"
    End Sub


Author

Commented:
ok..it shows me how the value is passing to and from each form, but, where does is the code on how the form is going to show that record...

Shahan AyyubSenior Software Engineer

Commented:
In this line:

Dim drTarget2() As DataRow = das1.Tables("tblMainData").Select("fldCmfrnum   = " & v.ToString)

your drTarget2 is getting that record which has fldCmfrnum =ytyt4543


because through query you have selected a record which has   fldCmfrnum =ytyt4543 and you will getting that row in drTarget2. display this dataset in datagridview. You will be getting what you want.

Author

Commented:
shahan, Thanks for sticking with the problem..

ok..thats my problem..displayng it in the datagridview...how to do that?

Thanks
fordraiders
Shahan AyyubSenior Software Engineer

Commented:
You could do like this:

Dim drTarget2() As DataRow = Db1DataSet1.Tables("tblMainData").Select("ldCmfrnum ='" & v.ToString & "'")  // to get specific row from the table
dataGridview.Datasource = drTarget2  // bind the row to source


Author

Commented:
sorry, my bad...

I need the form to go to the record not a datagridview sorry, wrking on to much stuff.

Author

Commented:
my data collection form ..."form1" to go to the record I'm looking for...
Shahan AyyubSenior Software Engineer

Commented:
I am sure this code is perfect. I tested it.

so where do you display your data ?? In Textboxes ???

Author

Commented:
TEXBOXES YES...
Shahan AyyubSenior Software Engineer

Commented:
     
Dim drTarget2() As DataRow = Db1DataSet1.Tables("tblMainData").Select("ldCmfrnum  ='" & v.ToString & "'")

Dim o() As Object = d(0).ItemArray()
TextBox1.Text = o(0).ToString() // column1 data in textbox1
TextBox2.Text = o(1).ToString() // column2 data in textbox2
TextBox3.Text = o(2).ToString() // column3 data in textbox3
TextBox4.Text = o(3).ToString() // column4 data in textbox4
.........
.......
........
.........

Senior Software Engineer
Commented:
a correction in my previous post see this one: (a typing mistake)

Dim drTarget2() As DataRow = Db1DataSet1.Tables("tblMainData").Select("ldCmfrnum   ='" & v.ToString & "'")

Dim o() As Object = drTarget2(0).ItemArray()
TextBox1.Text = o(0).ToString() // column1 data  in textbox1
TextBox2.Text = o(1).ToString() // column2 data in  textbox2
TextBox3.Text = o(2).ToString() // column3  data in textbox3
TextBox4.Text = o(3).ToString()  // column4 data in textbox4
.........
.......
........
.........

Author

Commented:
Thanks for the help !!!!!!!!