|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub qq()
Dim mySQL As String, i As Long
Dim mycnn As ADODB.Connection
Dim rs As New ADODB.Recordset
Application.ScreenUpdating = False
'********************************************************************************************
'清除C1:W1000区域的数据
Range("q4:aq35").Clear
Set mycnn = CreateObject("adodb.connection")
mycnn.Open ("provider=microsoft.jet.oledb.4.0;extended properties=excel 8.0;data source=" & ThisWorkbook.FullName)
'***********************************SQL语句修改部分
mySQL = "select 床号,款号,工序,数量,单价,金额 from [汇总单$] where 姓名 ='张山'"
'***********************************执行一个SQL语句,并把结果给到rs这个数据集里
rs.Open mySQL, mycnn, adOpenKeyset, adLockOptimistic
'***********************************循环提取字段名
For i = 0 To rs.Fields.Count - 1
Cells(3, i + 17) = rs.Fields(i).Name
Next
'***********************************执行SQL语句
Range("q4").CopyFromRecordset rs
'***********************************释放内存
rs.Close
Set rs = Nothing
mycnn.Close
Set mycnn = Nothing
'********************************************************************************************
Application.ScreenUpdating = True
End Sub
其中
mySQL = "select 床号,款号,工序,数量,单价,金额 from [汇总单$] where 姓名 ='张山'"
where 姓名 ='张山'"
能否改成where 姓名 = ’ 单元格A1里填的姓名吗‘ 语句怎么写
试着写成这样 报错
mySQL = "select 床号,款号,工序,数量,单价,金额 from [汇总单$] where 姓名 = " & Range("A1").Value |
|