|
金蝶出入库流水(VBA,需要引入KIS控件):
Sub 出入库序时账簿()
'
'
'
Dim CNN As New ADODB.Connection
Dim RST As New ADODB.Recordset
Dim STR_SQL, F_SQL, W_SQL As String
Dim i As Integer
'连接数据库--根据全局变量STR_CNN传递数据库连接字符串
If CNNLogin.STR_CNN = "" Then '如果连接字符串为空,说明未曾登录过
连接金蝶数据库 '如果未登录,则调用LOGIN连接过程(重新赋值给STR_CNN)
End If
CNN.Open CNNLogin.STR_CNN
If CNN.State <> adStateOpen Then
ActiveCell = "数据库未连接"
Exit Sub
End If
'查询语句
F_SQL = "select "
F_SQL = F_SQL + "B.FDate as 日期,"
F_SQL = F_SQL + "T.FName as 单据类别,"
F_SQL = F_SQL + "B.FPosted as 记账,"
F_SQL = F_SQL + "B.FBillNo as 单号,"
F_SQL = F_SQL + "SD.FNumber as 入仓代码,"
F_SQL = F_SQL + "SD.FName as 入仓名称,"
F_SQL = F_SQL + "SC.FNumber as 出仓代码,"
F_SQL = F_SQL + "SC.FName as 出仓名称,"
F_SQL = F_SQL + "S.FNumber as 供应商代码,"
F_SQL = F_SQL + "S.FName as 供应商,"
F_SQL = F_SQL + "I.FNumber as 物料代码,"
F_SQL = F_SQL + "I.FName as 名称,"
F_SQL = F_SQL + "U.FName as 单位,"
F_SQL = F_SQL + "E.FPrice as 单价,"
F_SQL = F_SQL + "E.FQty as 数量,"
F_SQL = F_SQL + "E.FAmount as 金额 "
'来源表
F_SQL = F_SQL + "from "
F_SQL = F_SQL + "icstockbill as B," '单据头
F_SQL = F_SQL + "ICStockBillEntry as E," '单据内容
F_SQL = F_SQL + "t_Item as I," '物料表
F_SQL = F_SQL + "t_Item as SD," '(入库)仓库表
F_SQL = F_SQL + "t_Item as SC," '(出库)仓库表
F_SQL = F_SQL + "t_Item as U," '单位表
F_SQL = F_SQL + "t_Supplier as S," '供应商表
F_SQL = F_SQL + "ICTransType as T " '单据类别表
'查询条件
W_SQL = "where B.FBrNo=E.FBrNo "
W_SQL = W_SQL + "and B.FInterID=E.FInterID "
W_SQL = W_SQL + "and E.FItemID=I.FItemID "
W_SQL = W_SQL + "and E.FDCStockID=SD.FItemID "
W_SQL = W_SQL + "and E.FSCStockID=SC.FItemID "
W_SQL = W_SQL + "and E.FUnitID=U.FItemID "
W_SQL = W_SQL + "and B.FSupplyID=S.FItemID "
W_SQL = W_SQL + "and E.FBrNo=T.FBrNo "
'排序
W_SQL = W_SQL + "order by B.FBrNo,B.FInterID,E.FEntryID"
'合成查询语句
STR_SQL = F_SQL + W_SQL
'执行查询,得到RST数据集
On Error Resume Next
RST.Open STR_SQL, CNN
On Error GoTo 0
'遍历设置字段名 (标题行)
Dim R, C As Integer
R = ActiveCell.Row
C = ActiveCell.Column
For i = 0 To RST.Fields.Count - 1
Cells(R, C + i) = RST.Fields(i).Name
Next
Cells(R + 1, C).CopyFromRecordset RST '将RST数据集复制导出。注意,不带字段名(标题行)
'清除内存占用
Set RST = Nothing
Set CNN = Nothing
End Sub |
|