Search code examples
exceloledbconnection

How to read data from Access database and Excel together using same OleDbConnection in VB .Net


I have a table in Access database and another table in Excel with a unique column called [Number]. I would like to know how to connect to both these source together using a single OleDbConnection so that I can execute a query something like this in VB .Net.

SELECT [Sheet1$].[Number], [Sheet1$].[Vendor],[Sheet1$].[Details], ,TableinAccess.[Datefield], TableinAccess.[Agreement],TableinAccess.Comments 
FROM  ([Sheet1$] LEFT OUTER JOIN TableinAccess ON [Sheet1$].[Number] = TableinAccess.[Number])

Idea is to fill the Gridview with data from both source.

Eg:

Excel have below table(Sheet1),

Number    Vendor    Details
1            vend1            xxx
2            vend2             xxx
3            vend3             xxx

Access have below table(TableinAccess),
Number    Datefield    Agreement    Comments
1            vend1             xxx            something
2            vend2             xxx             something

I have to fill my Gridview like this,

Number    Vendor    Details        Datefield        Agreement     Comments
1            vend1             xxx             vend1             xxx             something
2            vend2             xxx             vend2             xxx             something
3            vend3             xxx             null                 null               null


Solution

  • You can't do what you describe because the two data sources are completely isolated from each other.

    An OLEDB connection can only connect to a single "thing" (DB Server, file, etc.) at a time.

    To do a JOIN, you would need to copy the Excel data to a temporary table in Access or link to it from Access, and then do the join in Access.