运行VBA代码时收到错误消息。我试过调试,看起来我的连接字符串有问题。我收到一条错误消息"未找到数据源名称且未指定默认驱动程序。
Sub Macro176()
Sheets("176Tk").Select
Dim i As Integer
i = 17
Set oConn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
oConn.Open "Driver={MySQL ODBC 5.1 Driver};" & _
"server=myserveraddress;" & _
"database=mydatabasename; uid= myuserId; pwd=mypassword;" & _
"option=3"
SQLstr = "SELECT SUBSTRING(dbo.collection_points.name, CHARINDEX('-', dbo.collection_points.name) + 2, 3) + 'Tk' AS 'Tank', " & _
"dbo.data_values.actual_date_time AS 'Date', " & _
"dbo.data_values.value_text AS 'Drain Time', " & _
"dbo.data_points.name AS 'Description', " & _
"dbo.data_values.user_id AS 'Field Operator' " & _
"FROM " & _
"dbo.collection_points INNER JOIN " & _
"dbo.area ON dbo.collection_points.area_rec_id = dbo.area.area_rec_id INNER JOIN " & _
"dbo.data_points ON dbo.collection_points.cp_rec_id = dbo.data_points.cp_rec_id INNER JOIN " & _
"dbo.data_values ON dbo.data_points.dp_rec_id = dbo.data_values.dp_rec_id INNER JOIN " & _
"dbo.system_levels ON dbo.area.ownership_sysid = dbo.system_levels.system_id " & _
"WHERE " & _
"((dbo.system_levels.description = 'CX5 Crude Tank Farm' and dbo.area.name = 'Crd TF Draining') OR " & _
"(dbo.system_levels.description = 'CX5 Melvindale Tank Farm' and dbo.area.name = 'Mel Tank Draining') OR " & _
"(dbo.system_levels.description = 'CX5 Tank Farm (CP)' and dbo.area.name = 'CP Tank Draining') OR " & _
"(dbo.system_levels.description = 'CX5 Unifiner Tank Farm' and dbo.area.name = 'LabT.F. Draining')) AND " & _
"((dbo.data_values.value_text <> 'Yes') AND (dbo.data_values.value_text <> 'Complete') AND (dbo.data_values.value_text <> 'Incomplete') AND " & _
"(dbo.collection_points.name NOT like '%Roof%')) AND " & _
"dbo.data_values.nominal_date_time>='2017-01-01 00:00:00' and " & _
"dbo.data_values.nominal_date_time<'2018-12-31 11:59:59' " & _
"ORDER BY dbo.collection_points.name, dbo.data_values.actual_date_time"
rs.Open SQLstr, oConn, adLockOptimistic, adCmdTable
Do Until rs.EOF
Sheets("176Tk").Cells(i, 1).Value = rs.Fields("Tank")
Sheets("176Tk").Cells(i, 2).Value = rs.Fields("Date")
Sheets("176Tk").Cells(i, 3).Value = rs.Fields("Drain Time")
Sheets("176Tk").Cells(i, 4).Value = rs.Fields("Field Operator")
i = i + 1
Loop
rs.Close
oConn.Close
Set rs = Nothing
Set oConn = Nothing
End Sub