dcsimg
Results 1 to 4 of 4

Thread: Counting number of times a unique number appears in a CSV file

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    3

    Counting number of times a unique number appears in a CSV file

    Hi all, Iím relatively new to VB.Net, coming from a VBA background.

    Iím looking to be able to build a simple search of a csv file within a Windows Form Application within Visual Studio 2012.

    I need to be able to put the number into a text box, click the button and have the count of the amount of times the number appears in another text box.

    What I need to make sure, is if I search for 1234 and 123456 is in the list, is that it doesnít count that as 1 due to finding the first part of the number, it has to be the whole number.


    I have a similar function within VBA, where it will display the results into a list box but for obvious reason, this doesnít work with VB.Net.

    Any help is greatly appreciated.

  2. #2
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,223

    Re: Counting number of times a unique number appears in a CSV file

    Hi,

    you can OLEDB...

    if your CSV has Headers the use them, otherwise they show F1,F2 etc...

    Code:
    Imports System.Data.OleDb
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim _tb As New DataTable
    
            Me.DataGridView1.DataSource = _tb
    
            Dim SrcDir As String = "C:\"
            Dim sConn As String = String.Join(";", New String() { _
               "Provider=Microsoft.Jet.OLEDB.4.0", _
               "Data Source=" & SrcDir, _
               "Extended Properties=""Text; HDR=No; FMT=Delimited"""})
    
          
            Dim SQL As String = "Select * From [Test2.csv] Where F1 =" & TextBox1.Text & ";"
    
            Using Cn As New OleDbConnection(sConn), ta As New OleDbDataAdapter(SQL, Cn)
    
                Cn.Open()
    
                ta.Fill(_tb)
    
    
            End Using
            TextBox1.Text = DataGridView1.RowCount - 1 'don't count the new Row
        End Sub
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    3

    Re: Counting number of times a unique number appears in a CSV file

    Hi Chris, thank you for the response.

    I’m getting an error telling me the filepath location is invalid, but I know it’s not as I’m copying it from the Windows GUI and adding the file name on the end.

    Error:

    OleDbException was unhandled

    The file itself is just on my desktop for the time being and will be on a shared network drive once it’s built and working.

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    3

    Re: Counting number of times a unique number appears in a CSV file

    Actually after playing around I’ve got it working, didn’t realise the file name didn’t need to be in the filepath string.

    Will this work with multiple users all using the windows form at roughly the same time?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width