Error "Child list for field [Sheet1$] cannot be created" when loading data from excel to datagridview - vb.net

I am trying to fill the DataGridView (grvExcelData in this case) with data from an excel file. But I am getting the following error:
"Child list for field [Sheet1$] cannot be created"
Below is my code fragment where I am trying to fill the data grid view. Also I am using Visual Studio 2010 for programming
Thanks for your help in advance
Public Sub fillgrid()
Dim conn1 As String
conn1 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & filenamepath & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=2"""
Dim connection As OleDbConnection = New OleDbConnection(conn1)
Dim da As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM [Sheet1$]", connection)
Dim ds As DataSet = New DataSet()
Dim emptyfile As String = "The file does not have any records for inserting."
Dim selectCmd As OleDbCommand = New OleDbCommand()
selectCmd.Connection = connection
selectCmd.CommandText = "SELECT * FROM [Sheet1$]"
If connection.State = ConnectionState.Closed Then
connection.Open()
End If
da.SelectCommand = selectCmd
Dim dsCounter As Integer = da.Fill(ds, "[Sheet1$]")
If dsCounter = 0 Then
MessageBox.Show(emptyfile, "dsCounter")
End If
grvExcelData.DataSource = ds
grvExcelData.DataMember = "Sheet1"
grvExcelData.SelectionMode = DataGridViewSelectionMode.FullRowSelect
da.Dispose()
If connection.State = ConnectionState.Open Then
connection.Close()
connection.Dispose()
End If
End Sub

Change
grvExcelData.DataMember = "Sheet1"
to
grvExcelData.DataMember = "[Sheet1$]"

Related

Having trouble with OleDbConnection

I'm working on a program that will go out to an access database, wipe out the records for all 4 of the tables that I specify, before creating a new table in the database that contains all of the data from my Excel "Table of Contents" file. I'm having trouble with the OleDbConnection side of things. It keeps erroring out on the line that contains conn.Open() with the error "Invalid Argument"
My code is as follows:
Private Sub btnAccess_Click(sender As System.Object, e As System.EventArgs) Handles btnAccess.Click
Dim AccessPath As String = ""
Dim com1 As OleDbCommand
Dim com2 As OleDbCommand
Dim com3 As OleDbCommand
Dim com4 As OleDbCommand
Dim DatabaseFile As String = ""
Dim DatabaseFileTitle As String = ""
Dim ExcelFile As String = ""
Dim ExcelFileTitle As String = ""
Dim connect As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ExcelFile & _
";Extended Properties=Excel 12.0;"
OpenFileDialog2.DefaultExt = "*.accdb"
MsgBox("Select the ACCESS FILE from the working directory.", , "BROWSE FOR THE ACCESS DATABASE")
OpenFileDialog2.InitialDirectory = Microsoft.VisualBasic.FileIO.FileSystem.CurrentDirectory
'Text (*.txt)|*.txt|Pictures (*.bmp;*.ico)|*.bmp;*.ico
OpenFileDialog2.Filter = "Access Database (*.accdb)|*.accdb"
OpenFileDialog2.Title = "Select ACCESS DATABASE File."
OpenFileDialog2.ShowDialog()
DatabaseFile = OpenFileDialog2.FileName
DatabaseFileTitle = System.IO.Path.GetFileNameWithoutExtension(OpenFileDialog2.FileName)
OpenFileDialog3.DefaultExt = "*.xlsx"
MsgBox("Select the EXCEL TOC FILE from the working directory.", , "BROWSE FOR THE EXCEL TOC")
OpenFileDialog3.InitialDirectory = Microsoft.VisualBasic.FileIO.FileSystem.CurrentDirectory
'Text (*.txt)|*.txt|Pictures (*.bmp;*.ico)|*.bmp;*.ico
OpenFileDialog3.Filter = "Excel Spreadhseet (*.xlsx)|*.xlsx"
OpenFileDialog3.Title = "Select EXCEL TOC File."
OpenFileDialog3.ShowDialog()
ExcelFile = OpenFileDialog3.FileName
ExcelFileTitle = System.IO.Path.GetFileNameWithoutExtension(OpenFileDialog3.FileName)
Using conn As New OleDbConnection(connect)
Using cmd As New OleDbCommand()
cmd.Connection = conn
cmd.CommandText = "SELECT * INTO [MS Access;Database=" & DatabaseFile & "].[New Table] FROM [Sheet1$]"
conn.Open()
cmd.ExecuteNonQuery()
com1 = New OleDbCommand("delete from tblArtId", conn)
com2 = New OleDbCommand("delete from tblFigure", conn)
com3 = New OleDbCommand("delete from tblSubGroups", conn)
com4 = New OleDbCommand("delete from tblGroups", conn)
com1.ExecuteNonQuery()
com2.ExecuteNonQuery()
com3.ExecuteNonQuery()
com4.ExecuteNonQuery()
conn.Close()
End Using
End Using
MsgBox("Records Deleted")
End Sub
If anyone is curious....my updated code is as follows:
Private Sub btnAccess_Click(sender As System.Object, e As System.EventArgs) Handles btnAccess.Click
Dim AccessPath As String = ""
Dim com1 As OleDbCommand
Dim com2 As OleDbCommand
Dim com3 As OleDbCommand
Dim com4 As OleDbCommand
Dim DatabaseFile As String = ""
Dim DatabaseFileTitle As String = ""
Dim ExcelFile As String = ""
Dim ExcelFileTitle As String = ""
OpenFileDialog2.DefaultExt = "*.accdb"
MsgBox("Select the ACCESS FILE from the working directory.", , "BROWSE FOR THE ACCESS DATABASE")
OpenFileDialog2.InitialDirectory = Microsoft.VisualBasic.FileIO.FileSystem.CurrentDirectory
'Text (*.txt)|*.txt|Pictures (*.bmp;*.ico)|*.bmp;*.ico
OpenFileDialog2.Filter = "Access Database (*.accdb)|*.accdb"
OpenFileDialog2.Title = "Select ACCESS DATABASE File."
OpenFileDialog2.ShowDialog()
DatabaseFile = OpenFileDialog2.FileName
DatabaseFileTitle = System.IO.Path.GetFileNameWithoutExtension(OpenFileDialog2.FileName)
OpenFileDialog3.DefaultExt = "*.xlsx"
MsgBox("Select the EXCEL TOC FILE from the working directory.", , "BROWSE FOR THE EXCEL TOC")
OpenFileDialog3.InitialDirectory = Microsoft.VisualBasic.FileIO.FileSystem.CurrentDirectory
'Text (*.txt)|*.txt|Pictures (*.bmp;*.ico)|*.bmp;*.ico
OpenFileDialog3.Filter = "Excel Spreadhseet (*.xlsx)|*.xlsx"
OpenFileDialog3.Title = "Select EXCEL TOC File."
OpenFileDialog3.ShowDialog()
ExcelFile = OpenFileDialog3.FileName
ExcelFileTitle = System.IO.Path.GetFileNameWithoutExtension(OpenFileDialog3.FileName)
Dim connectExcel As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ExcelFile & _
";Extended Properties=Excel 12.0;"
Dim connectAccess As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DatabaseFile & ";"
Using connExcel As New OleDbConnection(connectExcel)
Using cmd As New OleDbCommand()
cmd.Connection = connExcel
cmd.CommandText = "SELECT * INTO [MS Access;Database=" & DatabaseFile & "].[New Table] FROM [TOC Output for Excel$]"
connExcel.Open()
cmd.ExecuteNonQuery()
connExcel.Close()
End Using
End Using
Using connAccess As New OleDbConnection(connectAccess)
Using cmdAccess As New OleDbCommand()
com1 = New OleDbCommand("delete from tblArtId", connAccess)
com2 = New OleDbCommand("delete from tblFigure", connAccess)
com3 = New OleDbCommand("delete from tblSubGroups", connAccess)
com4 = New OleDbCommand("delete from tblGroups", connAccess)
connAccess.Open()
com1.ExecuteNonQuery()
com2.ExecuteNonQuery()
com3.ExecuteNonQuery()
com4.ExecuteNonQuery()
connAccess.Close()
End Using
End Using
MsgBox("Staging Table has been created. All Records Deleted from Group, SubGroup, Figure, and ArtID tables")
End Sub

Trouble loading data into textbox

Upon the click of a button, I want the program to load in details about the user (stored in an access database) into the textboxes.
This is what I have to far
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\Administratot\Documents\RailwayDatabase2.accdb"
Dim MyConn As New OleDbConnection(connString)
Dim da As OleDbDataAdapter
Dim ds As DataSet
Try
MyConn.Open()
da = New OleDbDataAdapter("Select * from tbl_employees WHERE FirstName = """ & EmployeeLogin.usersname & """", MyConn)
ds = New DataSet
da.Fill(ds)
TextBox1.Text = ds.Tables("tbl_employees").Rows(0).Item(2)
MyConn.Close()
Catch ex As Exception
If MyConn.State <> ConnectionState.Closed Then MyConn.Close()
End Try
End Sub
any idea why this isn't working? thanks

cant find Isam installable CSV Files

Hey I am having a problem taking an excel spread sheet and inserting it into a data-grid in Visual Studio 2013 I was wondering if anyone could give me a hand.
Here is the code I have at the moment.
getting the error Could not find installable ISAM only when I have clicked on the csv fle iut works for ll the other files?
OpenFileDialog1.ShowDialog()
Dim filePath As String = OpenFileDialog1.FileName
Dim extension As String = Path.GetExtension(filePath)
Dim header As String = If(rbHeaderYes.Checked, "YES", "NO")
Dim conStr As String, sheetName As String
conStr = String.Empty
Select Case extension
Case ".xls"
'Excel 97-03
conStr = String.Format(Excel03ConString, filePath, header)
Exit Select
Case ".xlsx"
'Excel 07
conStr = String.Format(Excel07ConString, filePath, header)
Exit Select
Case ".csv"
'CSV
conStr = String.Format(ExcelCsv, filePath, header)
Exit Select
End Select
'Get the name of the First Sheet.
Using con As New OleDbConnection(conStr)
Using cmd As New OleDbCommand()
cmd.Connection = con
con.Open()
Dim dtExcelSchema As DataTable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
sheetName = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
con.Close()
End Using
End Using
'Read Data from the First Sheet.
Using con As New OleDbConnection(conStr)
Using cmd As New OleDbCommand()
Using oda As New OleDbDataAdapter()
Dim dt As New DataTable()
cmd.CommandText = (Convert.ToString("SELECT * From [") & sheetName) + "]"
cmd.Connection = con
con.Open()
oda.SelectCommand = cmd
oda.Fill(dt)
con.Close()
'Populate DataGridView.
dataGridView1.DataSource = dt
End Using
End Using
End Using
End Sub

Datagridview WHERE SQL Error

I'm trying to automatically fill a datagridview upon loading. This is what I have so far
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\Administratot\Downloads\RailwayDatabase2.accdb"
Dim MyConn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim tables As DataTableCollection
Dim source1 As New BindingSource
MyConn = New OleDbConnection
MyConn.ConnectionString = connString
ds = New DataSet
tables = ds.Tables
da = New OleDbDataAdapter("Select * from tbl_shifts WHERE EmployeeName = '" & EmployeeLogin.usersname & "' AND Completed = True", MyConn)
Dim view As New DataView(tables(0))
source1.DataSource = view
DataGridView2.DataSource = view
When I attempt this, I am met with an error reading
Cannot find table 0.
You must fill the dataset first.
da = New OleDbDataAdapter("Select * from tbl_shifts WHERE EmployeeName = '" & EmployeeLogin.usersname & "' AND Completed = True", MyConn)
da.Fill(ds)
Dim view As New DataView(tables(0))
This might help. Keep it simple.
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\Administratot\Downloads\RailwayDatabase2.accdb"
Dim MyConn As New OleDbConnection(connString)
Dim da As OleDbDataAdapter
Dim ds As DataSet
--For error handling, do this
Try
'Open the connection
MyConn.Open()
'Fill the dataset
da = New OleDbDataAdapter("Select * from tbl_shifts WHERE EmployeeName = '" & EmployeeLogin.usersname & "' AND Completed = True", MyConn)
ds = New DataSet
da.Fill(ds)
'Fill datagridview
DataGridView2.DataSource = ds.Tables(0)
'Close the connection
MyConn.Close()
Catch ex As Exception
'Make sure connection is closed
If MyConn.State <> ConnectionState.Closed Then MyConn.Close()
End Try
And of course, you will put second group of code in Form_Load Event.

How to use ACE OLEDB to import Excel data into VB?

Originally I was using Office Interop to import data, but that was a headache and a half for both me and my computer. Right now I'm attempting to load it with ACE, but my data grid isn't being populated. Once that's up and running I need to know how to use that data in other ways, and how to grab specific cells of data from that DataSet. I'm using Visual Studio 2008, by the way.
Right now I have...
Public Function funcUpdate(ByVal sFileLoc As String) As Boolean
'Determine connection string properties
Dim dbProperty As String
If updFileExt = ".xlsx" Then
dbProperty = "Excel 12.0 Xml;HDR=No"
ElseIf updFileExt = ".xls" Then
dbProperty = "Excel 12.0;HDR=No"
Else
MessageBox.Show("FATAL: File type error on updater", "OHGAWDNO", MessageBoxButtons.OK, MessageBoxIcon.Error)
updateTerm()
Return False
End If
Dim dbConn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & updFile & ";Extended Properties=" & dbProperty & ";")
Dim dbCommand As New OleDb.OleDbDataAdapter("select * from [sheet1$]", dbConn)
Dim dtSet As New DataSet
Try
dbCommand.TableMappings.Add("Table", "ExcelTest")
dbCommand.Fill(dtSet)
Form1.DataGrid1.DataSource = dtSet.Tables(0)
Catch exlErr As Exception
Finally
dbConn.Close()
End Try
updateTerm()
End Function
Try to do this.
Dim path As String = "c:\example.xlsx"
Dim constr As String = [String].Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;" & _
"HDR=YES;IMEX=1;""", path)
Dim adapter As New OleDbDataAdapter
Using cn As New System.Data.OleDb.OleDbConnection(constr)
Try
cmdselcet = New OleDbCommand("SELECT * FROM [Sheet1$]", cn)
cn.Open()
adapter.SelectCommand = cmdselcet
Dim ds As DataSet
ds = New DataSet
'Display
adapter.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
MsgBox("Done!")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Using