|
各位老师好! 用SQL 做了个凭证查询,可怎么也通不了,因为刚接触SQL,所以不知道哪儿出了问题,特此请教各位.以下为代码,附件已经上传.谢谢了!!
Sub 凭证查询()
Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sql As String
Dim Icount As Integer, iRow As Integer
iRow = [d14].End(xlUp).Row + 2
cnn.Open "Provider = Microsoft.Jet.Oledb.4.0;Extended Properties =Excel 8.0;Data Source =" & ThisWorkbook.FullName
sql = "select * from [记账数据库$a:t] where 凭号 = " & Sheets("凭证").Range("D3") & " and 年 = " & Sheets("凭证").Range("F3") & "and 月 = " & Sheets("凭证").Range("G3") & "and 所属公司 = '" & Sheets("凭证").Range("D2") & "'"
rs.Open sql, cnn, adOpenKeyset, adLockOptimistic, adCmdText
If rs.EOF = False Then
rs.MoveLast
Icount = rs.RecordCount
rs.MoveFirst
For j = 1 To Icount
Cells(iRow, 3) = rs.Fields("摘要")
Cells(iRow, 4) = rs.Fields("总账科目")
Cells(iRow, 5) = rs.Fields("明细科目")
Cells(iRow, 6) = rs.Fields("币别")
Cells(iRow, 7) = rs.Fields("汇率")
Cells(iRow, 8) = rs.Fields("原币借")
Cells(iRow, 9) = rs.Fields("原币贷")
Cells(iRow, 12) = rs.Fields("兑现状况")
Cells(iRow, 13) = rs.Fields("收付日期")
Cells(iRow, 14) = rs.Fields("现金流量项目定义")
Cells(iRow, 15) = rs.Fields("确认所属")
Range("K16").Value = rs.Fields("制单")
Range("H3").Value = rs.Fields("日")
rs.MoveNext
iRow = iRow + 1
Next j
Set rs = Nothing
cnn.Close
Else
Set rs = Nothing
cnn.Close
MsgBox "查询不到符合条件的凭证记录!"
End If
End Sub
|
|