VB 2010 express beginner needs help

Answered VB 2010 express beginner needs help

  • 2012年8月19日 17:52
     
     
    hi all,

    I'm new to programming and trying to self teach. I'm finding task
    online and using them to develop my skills. I've come across one that has me...


    I have access to a csv file which I have to read and filter using VB
    2010 express. the file has firstname, lastname and date of birth in
    it

    What i'm trying to do it:
    1. Filter and display only lines with a
    certain firstname or surname e.g. find firstname, surname and DoB or someone
    called "Smith"
    2. I want to filter the lines but using the month in the Dob
    e.g. display every line in the file (each person) who was borm in
    january


    Can anyone help as I've been at this for what feels like a
    life time and I'm getting nowhere

    Thansk for any help that is given

すべての返信

  • 2012年8月19日 18:15
     
     

    HI Dave075

    Welcome to MSDN

    You are doing very good job. Keep up.

    Which part do you have difficulty? What code do you have now?

    P.s. There is a Forum for the Express Edition http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/threads I don't know if this apply.


    Be a good forum member. Make this forum a great place to meet and interact with others around the world.
    Helpful Links:


  • 2012年8月19日 20:51
     
     回答済み

    have a look at this:

    http://code.msdn.microsoft.com/filtered-CSV-c2a25185


    thanks for any help

  • 2012年8月20日 0:04
     
     
    hi all,

    I'm new to programming and trying to self teach. I'm finding task
    online and using them to develop my skills. I've come across one that has me...


    I have access to a csv file which I have to read and filter using VB
    2010 express. the file has firstname, lastname and date of birth in
    it

    What i'm trying to do it:
    1. Filter and display only lines with a
    certain firstname or surname e.g. find firstname, surname and DoB or someone
    called "Smith"
    2. I want to filter the lines but using the month in the Dob
    e.g. display every line in the file (each person) who was borm in
    january


    Can anyone help as I've been at this for what feels like a
    life time and I'm getting nowhere

    Thansk for any help that is given

    Dave,

    Are you looking for a quick answer, or are you looking for learning?

    There are many ways this can be done and I'll be glad to put something together (tomorrow) to assist in that - that is, if you really want to know, and not just grab some code and go with it.

    I'm by far not the only one - many of us will help in that endeavor.

    Do let us know and I'll reply back tomorrow. :)


    Please call me Frank :)

  • 2012年8月20日 1:29
     
     

    hi all,

    I'm new to programming and trying to self teach. I'm finding task
    online and using them to develop my skills. I've come across one that has me...


    I have access to a csv file which I have to read and filter using VB
    2010 express. the file has firstname, lastname and date of birth in
    it

    What i'm trying to do it:
    1. Filter and display only lines with a
    certain firstname or surname e.g. find firstname, surname and DoB or someone
    called "Smith"
    2. I want to filter the lines but using the month in the Dob
    e.g. display every line in the file (each person) who was borm in
    january


    Can anyone help as I've been at this for what feels like a
    life time and I'm getting nowhere

    Thansk for any help that is given

    Dave,

    Are you looking for a quick answer, or are you looking for learning?

    There are many ways this can be done and I'll be glad to put something together (tomorrow) to assist in that - that is, if you really want to know, and not just grab some code and go with it.

    I'm by far not the only one - many of us will help in that endeavor.

    Do let us know and I'll reply back tomorrow. :)


    Please call me Frank :)

    As Frank says, there are many ways to skin a cat.

    As i said in my Samples Gallery submission, i'll answer any questions about the code i posted.

    A lot can be learned from analyzing the code used in a working example, as i'm sure Frank would agree.


    thanks for any help

  • 2012年8月20日 2:15
     
     回答済み コードあり

    Hello, as others have indicated there are several ways to read data from a text file. Whatever method you select unless there is a huge amount of data I would read all lines in at once then as needed filter the data and include an option to remove a filter.

    The example below uses OleDb data provider to read in a comma delimited text file (test text file contents shown below code). There are comments in the code to assist understanding how things work.

    MSDN Page on reading text using OleDb

    Below code requires three buttons, one DataGridView (make sure to read the comments in the code for the DataGridView columns)

    There are three columns in the text file, first and last name along with birthday. In the DataTable loaded with this data a 4th column is added to store the month for each birthday since we can not extract the month of a birthday into a DataColumn Expression.

    Imports System.Data.OleDb
    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim dt As New DataTable With {.TableName = "People"}
            dt.Columns.AddRange(New DataColumn() {New DataColumn("FirstName", GetType(System.String)),
                                                  New DataColumn("LastName", GetType(System.String)),
                                                  New DataColumn("Birthday", GetType(System.DateTime)),
                                                  New DataColumn("BirthMonth", GetType(System.Int32))})
            ' Three columns are added to the DataGridView and Data Property is set to each of the
            ' first three columns above. The fourth column has no need to be displayed so no 4th column.
            DataGridView1.AutoGenerateColumns = False
            ' File name to read from DataSource below.
            Dim FileName As String = "People.txt"
            ' DataSource is the folder where People.txt resides.
            ' The folder must end with a back slash as done below
            Dim Builder As New OleDbConnectionStringBuilder With
                {
                    .Provider = "Microsoft.Jet.OLEDB.4.0",
                    .DataSource = Application.StartupPath & IO.Path.DirectorySeparatorChar
                }
            ' Indicates People.txt data is delimited by comma and the first
            ' row is data.
            Builder.Add("Extended Properties", "text;HDR=No;FMT=Delimited(,)")
            Using cn As New OleDbConnection With
                {
                    .ConnectionString = Builder.ConnectionString
                }
                Using cmd As New OleDbCommand With
                    {
                        .Connection = cn,
                        .CommandText =
                        <SQL>SELECT F1 As FirstName, F2 As LastName, F3 As Birthday FROM <%= FileName %></SQL>.Value
                    }
                    cn.Open()
                    dt.Load(cmd.ExecuteReader)
                    ' Uncomment if first row column 0 has strange chars at the beginning of data.
                    'dt.Rows(0).Item(0) = System.Text.RegularExpressions.Regex.Replace(dt.Rows(0).Item(0).ToString, "[^A-Za-z0-9]", "")
                    ' Used for RowFilter below in Button2 Click event
                    For Each row As DataRow In dt.Rows
                        row.Item("BirthMonth") = CDate(row.Item("Birthday")).Month
                    Next
                    dt.AcceptChanges()
                    DataGridView1.DataSource = dt
                End Using
            End Using
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            CType(DataGridView1.DataSource, DataTable).DefaultView.RowFilter = "LastName='Smith'"
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            CType(DataGridView1.DataSource, DataTable).DefaultView.RowFilter = "LastName='Smith' and birthmonth = 1"
        End Sub
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            CType(DataGridView1.DataSource, DataTable).DefaultView.RowFilter = ""
        End Sub
    End Class

    People.txt

    Kevin,Gallagher,8/11/1960
    Karen,Smith,1/23/1934
    Tim,Wilson,4/2/1987
    Mary,Jones,2/13/1966
    Bill,Smith,3/2/1991


    KSG

  • 2012年8月20日 14:35
     
     

    hi all,

    I'm new to programming and trying to self teach. I'm finding task
    online and using them to develop my skills. I've come across one that has me...


    I have access to a csv file which I have to read and filter using VB
    2010 express. the file has firstname, lastname and date of birth in
    it

    What i'm trying to do it:
    1. Filter and display only lines with a
    certain firstname or surname e.g. find firstname, surname and DoB or someone
    called "Smith"
    2. I want to filter the lines but using the month in the Dob
    e.g. display every line in the file (each person) who was borm in
    january


    Can anyone help as I've been at this for what feels like a
    life time and I'm getting nowhere

    Thansk for any help that is given

    Dave,

    Are you looking for a quick answer, or are you looking for learning?

    There are many ways this can be done and I'll be glad to put something together (tomorrow) to assist in that - that is, if you really want to know, and not just grab some code and go with it.

    I'm by far not the only one - many of us will help in that endeavor.

    Do let us know and I'll reply back tomorrow. :)


    Please call me Frank :)

    As Frank says, there are many ways to skin a cat.

    As i said in my Samples Gallery submission, i'll answer any questions about the code i posted.

    A lot can be learned from analyzing the code used in a working example, as i'm sure Frank would agree.


    thanks for any help

    I'm quite sure that you're right Paul.

    :)


    Please call me Frank :)