如何在VBA中把excel中的数据 写入数据库
sql 数据库为: myDatabase 它有个表为:tb1 数据库用戶名:sa 数据库密码为:123
当Excel表保存时,如何把单元格A1和A2的值插入到 数据库 myDatabase 的表 tb1中
[解决办法]
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Dim cn As ADODB.Connection Dim strSQL As String Dim lngRecsAff As Long Set cn = New ADODB.Connection cn.Open "Provider=SQLOLEDB;Data Source=<server>;" & _ "Initial Catalog=<database>;User ID=<user>;Password=<password>" 'OPENDATASOURCE strSQL = "SELECT * INTO XLImport6 FROM " & _ "OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0', " & _ "'Data Source=C:\test\xltest.xls;" & _ "Extended Properties=Excel 8.0')...[Customers$]" Debug.Print strSQL cn.Execute strSQL, lngRecsAff, adExecuteNoRecords Debug.Print "Records affected: " & lngRecsAff cn.Close Set cn = NothingEnd Sub