25. september 2008 - 18:09Der er
5 kommentarer og 1 løsning
VBA kode der importere excel regneark til tabel
Hej folkens,
Jeg er ved at prøve på, at lave noget VBA kode, som kan importere et regneark ind i en tabel. Tabellens felter har samme navn, som "overskriften" i regnearket. Eksempelvis ser regnearket sådan her ud:
Navn | Adresse | Telefon | Mobil | Email | ------------------------------------------ Eva | Etsted | 13356 | 154 | asd@s | ------------------------------------------ add | adasd | dasdas | asd | asdas | ------------------------------------------
Osv. Det vil altså sige, at felterne Navn, Adresse, Telefon, Mobil og Email findes i min tabel i Access, som hedder "Emner".
Nu vil jeg gerne have, at den tager alle rækker og smider dem i min access tabel.
Jeg har lavet følgende kode:
Private Sub Kommandoknap1_Click() Dim conTemp As New ADODB.Connection Dim rsTemp As New ADODB.Recordset Dim rsAccess As New ADODB.Recordset Dim i As Integer, j As Integer
' Sets the Dialog Title to Open File CommonDialog1.DialogTitle = "Open File"
' Sets the File List box to Word documents and Excel documents CommonDialog1.Filter = "Excel Spreadsheets (*.xls)|*.xls"
' Set the default files type to Word Documents CommonDialog1.FilterIndex = 1
' Sets the flags - File must exist and Hide Read only CommonDialog1.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
' Set dialog box so an error occurs if the dialogbox is cancelled CommonDialog1.CancelError = True
' Enables error handling to catch cancel error On Error Resume Next ' display the dialog box CommonDialog1.ShowOpen If Err Then ' This code runs if the dialog was cancelled 'MsgBox "Dialog Cancelled" Exit Sub End If
' Change mousepointer to wait Screen.MousePointer = vbHourglass
' open ado connection to excel file conTemp.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & CommonDialog1.Filename & " ;Extended Properties=Excel 8.0; "
' open recordset containing all values for the excel file rsTemp.Open "Select * from [Ark1$]", conTemp, adOpenStatic
' opening another recordset for transferring values to access rsAccess.Open "Select * from Emner", con, adOpenKeyset, adLockOptimistic
' looping through all columns of the excel file For j = 0 To rsTemp.Fields.Count - 1
' looping through all records of the excel file For i = 0 To rsTemp.RecordCount - 1 ' New blank record to copy the excel record rsAccess.AddNew
' Add the value to the db rsAccess.Fields(rsTemp.Fields(j).Name) = rsTemp.Fields(j).Value
' Execute query rsAccess.Update Next i
rsTemp.MoveNext Next j
' Success MsgBox "Import of records for " & CommonDialog1.Filename & " successful", vbInformation
' Close connection Set rsTemp = Nothing Set rsAccess = Nothing Set conTemp = Nothing
' Switch mounsepointer back to normal Screen.MousePointer = vbDefault Exit Sub
Mit problem er, at den ikke ligger noget data ind i access databasen. Den læser fint excel filen. Det her er min nuværende kode. Den burde ligge ind , men gør det ikke:
Private Sub Kommandoknap1_Click() Dim con As New ADODB.Connection Dim conTemp As New ADODB.Connection Dim rsTemp As New ADODB.Recordset Dim rsAccess As New ADODB.Recordset Dim i As Integer Dim j As Integer
' Sets the Dialog Title to Open File CommonDialog1.DialogTitle = "Open File"
' Sets the File List box to Word documents and Excel documents CommonDialog1.Filter = "Excel Spreadsheets (*.xls)|*.xls"
' Set the default files type to Word Documents CommonDialog1.FilterIndex = 1
' Sets the flags - File must exist and Hide Read only CommonDialog1.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
' Set dialog box so an error occurs if the dialogbox is cancelled CommonDialog1.CancelError = True
' Enables error handling to catch cancel error On Error Resume Next ' display the dialog box CommonDialog1.ShowOpen If Err Then ' This code runs if the dialog was cancelled 'MsgBox "Dialog Cancelled" Exit Sub End If
' Change mousepointer to wait Screen.MousePointer = vbHourglass
' open ado connection to excel file conTemp.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & CommonDialog1.Filename & " ;Extended Properties=Excel 8.0;"
' open ado connection to access file con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Emnestyring.mdb;"
' open recordset containing all values for the excel file rsTemp.Open "Select * from [Ark1$]", conTemp, adOpenStatic
' opening another recordset for transferring values to access rsAccess.Open "Emner", con, adOpenKeyset, adLockOptimistic, adCmdTable
' looping through all records of the excel file For i = 0 To rsTemp.RecordCount - 1 ' New blank record to copy the excel record rsAccess.AddNew
' looping through all columns of the excel file For j = 0 To rsTemp.Fields.Count - 1 ' Add the value to the db rsAccess.Fields(rsTemp.Fields(j).Name) = rsTemp.Fields(j).Value Next j
' Execute query rsAccess.Update
' Go to next record rsTemp.MoveNext Next i
' Success MsgBox "Import of records for " & CommonDialog1.Filename & " successful", vbInformation
' Close connection rsTemp.Close rsAccess.Close conTemp.Close con.Close Set rsTemp = Nothing Set rsAccess = Nothing Set conTemp = Nothing Set con = Nothing
' Switch mounsepointer back to normal Screen.MousePointer = vbDefault
Problemet var låse tingen på tabellen. Der skulle bruges pessimistic i stedet
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.