|
Private Sub 查询() '代码开始语句,name可变更
Set conn = CreateObject("adodb.connection") '无需变更
Set rs1 = CreateObject("Adodb.Recordset")
conn.Open "Driver=SQL Server;SERVER=采购内勤; Database=配料;uid=;pwd=" 'localhost为数据库ip地址,dbname为数据库名称,name为用户名,password为密码
sqll = "select count(distinct 总秤次) as 配秤次数, " _
& "count(distinct 原料名) as 原料种类数," _
& "ltrim(str(sum(配方重量),10,1)) as 预计总消耗," _
& "ltrim(str(sum(实际重量),10,1)) as 实际总消耗," _
& "ltrim(str(sum(误差),10,1)) as 总误差," _
& "ltrim(str(avg(误差),10,2)) as 平均误差 " _
& "from 配秤结果 " _
& "where 开始时间>=" & Sheet1.Range("B1") "and 开始时间<" & Sheet1.Range("B1") ";" _
'查询语句,按实际需求变更
Set rs1 = conn.Execute(sqll) '无需变更
For Each Field In rs1.Fields
Sheet1.[a5].Offset(0, i) = Field.Name '字段名
i = i + 1
Next
‘读取数据库查询的字段名
Sheet1.Range("A6").CopyFromRecordset rs1 '数据库查询结果将在 lie 列 hang 行开始展示
conn.Close '关闭数据库
End Sub '无需变更
这是一段SQL查询语句,里面由单元格时间作为条件
单元格B1 和 E1的时间格式为 yyyy-mm-dd hh-mm-ss
查询提示错误,有哪位大神帮解答一下
|
|