|
- Sub Button1_Click()
- '引用Microsoft ActiveX Data Objects 2.x Library
- '引用Microsoft AD0 Ext 2.8 for DDL and Security
- Dim cnn As New ADODB.Connection
- Dim Cat As New ADOX.Catalog
- Dim myPath As String
- Dim myData As String
- Dim p As String
- Dim SQL As String
- myPath = ThisWorkbook.Path & ""
- p = Dir(myPath & "A list *.xls?")
- If p = "" Then
- MsgBox "没有发现数据源工作簿,无需更新。", vbInformation, "提醒"
- Exit Sub
- End If
- myData = myPath & "data.accdb"
- myTable = Split(p, " ")(0) & Split(p, " ")(1)
- If Dir(myData) = "" Then Cat.Create "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & myData
- cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & myData
- Set rs = cnn.OpenSchema(adSchemaTables, Array(Empty, Empty, myTable, Empty))
- If rs.EOF Then
- SQL = "CREATE TABLE " & myTable & "(SN long,[Number] long,[Assetment Number] text(20),SendTime date)"
- Else
- SQL = "DELETE FROM " & myTable
- End If
- cnn.Execute SQL
- SQL = "INSERT INTO " & myTable & "(SN,[Number],[Assetment Number],SendTime) " _
- & "SELECT f1 as SN,f2 as [Number],f3 as [Assetment Number],f4 as SendTime FROM [Excel 12.0;hdr=no;Database=" & myPath & p & ";].[Sheet1$a2:d]"
- cnn.Execute SQL
- MsgBox " 成功导入 ", vbInformation, " 导入数据库 "
- Set Cat = Nothing
- cnn.Close
- Set cnn = Nothing
- End Sub
复制代码 |
|