|
本帖最后由 autumnalRain 于 2015-7-23 12:30 编辑
想要实现的结果是:where子句条件使用【打印清单】中的凭证号和项目名称两个变量,并将其记录写到【查询结果】表中.因为是循环,凭证号和项目名称是两个变量,该如何书写呢?
即下面代码行的 ' And "项目名称= '" & SProject & "'"如何写进SQL语句中?
Sub 查询结果()
Dim Rmax As Integer, i As Integer
Dim SVoucher, SProject
Rmax = Sheets("打印清单").Range("a1").CurrentRegion.Rows.Count ' MsgBox Rmax
For i = 2 To Rmax
SVoucher = Sheets("打印清单").Cells(i, 1)
SProject = Sheets("打印清单").Cells(i, 2)
Sheets("查询结果").Cells.Clear
Dim cnn As Object, SQL
Set cnn = CreateObject("ADODB.Connection")
cnn.Open "provider=microsoft.ACE.OLEDB.12.0;extended properties='Excel 12.0;hdr=yes';data source=" & ThisWorkbook.FullName
SQL = "select 凭证号,名称,规格,单位,数量,单价,金额,项目名称 from [项目名称$] where 凭证号='" & SVoucher & "'" ' And "项目名称= '" & SProject & "'"
Sheets("查询结果").[a2].CopyFromRecordset cnn.Execute(SQL)
cnn.Close
Set cnn = Nothing
Next i
End Sub
|
|