Eigenschappen . " Selecteer " Referenties " en klik op "Add . " Selecteer " COM " en klik op " Microsoft Office 12.0 Object Library . " Selecteer "OK . " Kopen van 2 Maak een nieuwe Excel-spreadsheet en voeg de volgende gegevens :
Veld1 Field2
gegevens
Data2
gegevens
Data2
slaan op " C : \\ " als " . ExcelToImport.xls "
3
Dubbelklik op " Button1 " op uw formulier te openen " Form1.vb . " Typ het volgende onder " Button1_Click " om een sub oproep maken:
importAllData
( " C : \\ ExcelToImport.xls " , " C : \\ AccessFile.mdb " )
MessageBox.Show
( " de import is voltooid ' ) op Twitter
Type 4 de volgende opties om een nieuwe sub genaamd creëren " importAllData " dat een nieuwe database-bestand zal creëren voor het importeren : !
Private Shared sub importAllData
( ByVal bestandspad als Koord , ByVal db_path als Koord )
Dim accessdb
Zoals Microsoft.Office.Interop.Access.Application
accessdb
= Nieuw Microsoft.Office.Interop.Access.ApplicationClass ( ) < br >
accessDB.Visible = False
accessDB.NewCurrentDatabase
( db_path , = Niets
Dim conn
Zoals OleDb.OleDbConnection = MakeExcelConnection ( bestandspad )
FillAccessDatabase
( conn , db_path , bestandspad ) op Twitter
End Sub
5
Typ het volgende om een nieuwe sub genaamd " FillAccessDatabase " te creëren :
Private Shared sub FillAccessDatabase ( ByVal conn , ByVal db_path , ByVal bestandspad )
conn = " Provider
= Microsoft.ACE.OLEDB .12.0 ; " & " data Source = " & bestandspad & " , " & " Uitgebreide eigenschappen = Excel 12.0 ; "
Dim verbinding
Zoals OleDb.OleDbConnection = Nieuw OleDb.OleDbConnection ( verb ) op
Dim _command Zoals OleDb.OleDbCommand = Nieuw OleDb.OleDbCommand ( )
_command.Connection
= verbinding
_command.CommandText
= "SELECT * INTO [ MS Access ; Database = " & db_path & " ] . [ Blad1 ] FROM Sub
Type 6 de volgende opties om een nieuwe functie genaamd " MakeExcelConnection " te creëren :
Prive Gedeelde Functie MakeExcelConnection
( ByVal fileName als Koord) als OleDb.OleDbConnection
Dim conn As String
conn =
" Provider = Microsoft.ACE.OLEDB.12.0 ; " & " data Source = " & fileName & " , " & " Uitgebreide eigenschappen = Excel 12.0 ; "
Dim verbinding Zoals OleDb.OleDbConnection = Nieuw OleDb.OleDbConnection ( verb )
Retouraansluiting
End Function
7
Run je programma en klik op " Button1 " naar " Blad1 " van uw Excel-spreadsheet te importeren naar " Blad1 " tabel in Access .