Hvordan få tilgang Visual Basic for å importere Excel data

Hvordan få tilgang Visual Basic for å importere Excel data


Visual Basic er et Microsoft programmeringsspråk som er relativt enkelt å bruke og Microsoft Access og Excel er programmer som tilbys i Microsoft Office-pakken. Visual Basic.NET kode er viktig når du arbeider med Excel regneark og trenger å importere data til en Access-database. Dataene kan være nødvendig i Access for ytterligere data manipulasjon med databaseverktøy.

Bruksanvisning

1 Start en ny Visual Basic.NET prosjekt og legge til en "knapp" i skjemaet. Velg "Project" -menyen og klikk "<prosjektnavn> Egenskaper." Velg "Referanser" og klikk "Legg til". Velg "COM" og klikk "Microsoft Office 12,0 Object Library». Velg "OK".

2 Opprett en ny Excel regneark og legg til følgende data:

field1 Felt 2

data Data2

data Data2

Lagre den på "C: \" som "ExcelToImport.xls."

3 Dobbeltklikk på "Button1" på skjemaet for å åpne "Form1.vb." Skriv inn følgende under "button1_click" for å lage en sub samtale:

importAllData ( "C: \ ExcelToImport.xls", "C: \ AccessFile.mdb")

MessageBox.Show("The import is complete!")

4 Skriv inn følgende for å opprette en ny under kalt "importAllData" som vil skape en ny database fil for import:

Privat felles Sub importAllData (ByVal Filbane As String, ByVal db_path As String)

Dim accessDB As Microsoft.Office.Interop.Access.Application

accessDB = Ny Microsoft.Office.Interop.Access.ApplicationClass ()

accessDB.Visible = False

accessDB.NewCurrentDatabase (db_path, Microsoft.Office.Interop.Access.AcNewDatabaseFormat.acNewDatabaseFormatAccess2007)

accessDB.CloseCurrentDatabase ()

accessDB.Quit (Microsoft.Office.Interop.Access.AcQuitOption.acQuitSaveAll)

accessDB = Ingenting

Dim conn Som OleDb.OleDbConnection = MakeExcelConnection (filbane)

FillAccessDatabase (Tilkobling, db_path, filbane)

End Sub

5 Skriv inn følgende for å opprette en ny under kalt "FillAccessDatabase":

Privat felles Sub FillAccessDatabase (ByVal Tilkobling, ByVal db_path, ByVal filbane)

conn = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & file_path & ";" & "Extended Properties=Excel 12.0;"

Dim forbindelse Som OleDb.OleDbConnection = Ny OleDb.OleDbConnection (conn)

Dim _command Som OleDb.OleDbCommand = Ny OleDb.OleDbCommand ()

_command.Connection = tilkobling

_command.CommandText = "SELECT * INTO [MS Access; Database =" & db_path & "] [Sheet1] Fra [Ark1 $]."

connection.Open ()

_command.ExecuteNonQuery ()

connection.Close ()

End Sub

6 Skriv inn følgende for å opprette en ny funksjon kalt "MakeExcelConnection":

Privat felles funksjon MakeExcelConnection (ByVal filnavn som String) As OleDb.OleDbConnection

Dim conn As String

conn = "Provider = Microsoft.ACE.OLEDB.12.0;" & "Data Source =" & filnavn og ";" & "Extended Properties = Excel 12,0;"

Dim forbindelse Som OleDb.OleDbConnection = Ny OleDb.OleDbConnection (conn)

Return tilkobling

End Function

7 Kjør programmet og klikk på "Button1" for å importere "Ark1" av Excel-regnearket til "Ark1" tabellen i Access.