Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL Query for Excel file

SQL Query for Excel file

Scheduled Pinned Locked Moved Database
databasehelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Swisher24
    wrote on last edited by
    #1

    I have several excel files(*.xls) that I'm extracting data from using Visual Basic and I'm having problems with my SQL statement. I would like to do a count & group by in the statement to input this data into a control sheet but I continue to get an error message when filling the dataset. Error: An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in microsoft.visualbasic.dll Code Snippet:

    Function groupxls(ByRef xlfile As String, ByRef xlsfile As String)

    Dim ex1 As String = "Provider=Microsoft.Jet.OLEDB.4.0; " & "data source=" & xlfile & ";" & "Extended Properties=Excel 8.0;"
    Dim conn1 As New OleDbConnection(ex1)
    Dim dtset1 = New DataSet

    Dim flnme As String
    y = xlsfile.LastIndexOfAny(".")
    flnme = xlsfile.Remove(y, 4)
    Dim col1 As String = "sc_typname" '<-----Column to group by

    Dim MyCommand1 As New OleDbDataAdapter("select count(*) from flnme group by col1", conn1)

        MyCommand1.Fill(dtset1)
        DataGrid1.Refresh()
        DataGrid1.DataSource = dtset1.Tables(0)
        conn1.Close()
        conn1.Dispose()
    
    End Function
    

    If anyone can take the time to look at this for me please know that it will be very much appreciated! Thanks in advance! :)

    Swish

    A 1 Reply Last reply
    0
    • S Swisher24

      I have several excel files(*.xls) that I'm extracting data from using Visual Basic and I'm having problems with my SQL statement. I would like to do a count & group by in the statement to input this data into a control sheet but I continue to get an error message when filling the dataset. Error: An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in microsoft.visualbasic.dll Code Snippet:

      Function groupxls(ByRef xlfile As String, ByRef xlsfile As String)

      Dim ex1 As String = "Provider=Microsoft.Jet.OLEDB.4.0; " & "data source=" & xlfile & ";" & "Extended Properties=Excel 8.0;"
      Dim conn1 As New OleDbConnection(ex1)
      Dim dtset1 = New DataSet

      Dim flnme As String
      y = xlsfile.LastIndexOfAny(".")
      flnme = xlsfile.Remove(y, 4)
      Dim col1 As String = "sc_typname" '<-----Column to group by

      Dim MyCommand1 As New OleDbDataAdapter("select count(*) from flnme group by col1", conn1)

          MyCommand1.Fill(dtset1)
          DataGrid1.Refresh()
          DataGrid1.DataSource = dtset1.Tables(0)
          conn1.Close()
          conn1.Dispose()
      
      End Function
      

      If anyone can take the time to look at this for me please know that it will be very much appreciated! Thanks in advance! :)

      Swish

      A Offline
      A Offline
      aztekka
      wrote on last edited by
      #2

      Swisher24 wrote:

      Dim col1 As String = "sc_typname" '<-----Column to group by Dim MyCommand1 As New OleDbDataAdapter("select count(*) from flnme group by col1", conn1)

      You are passing the name of the variable to the query string instead of the value of the variable, try the following:

      Dim MyCommand1 As New OleDbDataAdapter("select count(*) from flnme group by " + col1, conn1)

      S 1 Reply Last reply
      0
      • A aztekka

        Swisher24 wrote:

        Dim col1 As String = "sc_typname" '<-----Column to group by Dim MyCommand1 As New OleDbDataAdapter("select count(*) from flnme group by col1", conn1)

        You are passing the name of the variable to the query string instead of the value of the variable, try the following:

        Dim MyCommand1 As New OleDbDataAdapter("select count(*) from flnme group by " + col1, conn1)

        S Offline
        S Offline
        Swisher24
        wrote on last edited by
        #3

        Thanks aztekka! I tried this sql statement and I'm still getting an Unhandled Exception Error. I'm thinking that VB 2003 doesn't like the statement somehow. I will continue to work on this and any more help that can be provided will be very appreciated.

        Swish

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups