|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Option Explicit
Sub A()
Dim cnn, rs As Object, SQL As String, TMP, TMP1, tmp2 As String, I As Byte, j As Integer, arr
arr = Array("[供应入InBase]", "[供应出OutBase]", "[生产入InBase]", "[生产出OutBase]", "[外购入InBase]", "[外购出OutBase]")
Set cnn = CreateObject("ADODB.CONNECTION")
Set rs = CreateObject("ADODB.Recordset")
Application.ScreenUpdating = False
If [A2] = "" Or [B2] = "" Then
MsgBox "开始日期;结束日期必须填写!": Exit Sub
End If
If [A2] > [B2] Then MsgBox "开始日期不能大于结束日期": Exit Sub
cnn.Open "Provider=Microsoft.ACE.OleDb.12.0; Data Source=" & ThisWorkbook.Path & "\MyData.accdb;"
SQL = "delete * FROM [tmp]"
cnn.Execute (SQL)
SQL = ""
For I = 0 To UBound(arr)
SQL = SQL & "select 车型,零件号,物资名称 FROM " & arr(I) & " where 日期 BETWEEN #" & [A2] & "# AND #" & [B2] & "# UNION "
Next
SQL = "SELECT * FROM (" & Left(SQL, Len(SQL) - 6) & ")"
rs.Open SQL, cnn, 1, 1
If rs.RecordCount = 0 Then
rs.Close
Set rs = Nothing
cnn.Close
Set cnn = Nothing
MsgBox "无此记录": Exit Sub
Else
rs.Close
SQL = "INSERT INTO [tmp] " & SQL
cnn.Execute (SQL)
End If
SQL = ""
Range("a6:i9999").ClearContents
Range("a6:i9999").Borders.LineStyle = 0
TMP = "SELECT A.车型,A.零件号,A.物资名称,B.数量1,C.数量1,D.数量1,E.数量1,F.数量1,G.数量1 FROM ((((([tmp] A LEFT JOIN ("
tmp2 = " where 日期 BETWEEN #" & [A2] & "# AND #" & [B2] & "# "
SQL = TMP & "SELECT 车型,零件号,物资名称,SUM(数量) AS 数量1 FROM [供应入InBase]" & tmp2 & "GROUP BY 车型,零件号,物资名称) B ON " _
& "A.车型=B.车型 AND A.零件号=B.零件号 AND A.物资名称=B.物资名称) left join " _
& "(SELECT 车型,零件号,物资名称,SUM(数量) AS 数量1 FROM [供应出OutBase] " & tmp2 & "GROUP BY 车型,零件号,物资名称) C ON " _
& "A.车型=C.车型 AND A.零件号=C.零件号 AND A.物资名称=C.物资名称) LEFT JOIN " _
& "(SELECT 车型,零件号,物资名称,SUM(数量) AS 数量1 FROM [生产入InBase] " & tmp2 & "GROUP BY 车型,零件号,物资名称) D ON " _
& "A.车型=D.车型 AND A.零件号=D.零件号 AND A.物资名称=D.物资名称) LEFT JOIN " _
& "(SELECT 车型,零件号,物资名称,SUM(数量) AS 数量1 FROM [生产出OutBase] " & tmp2 & "GROUP BY 车型,零件号,物资名称) E ON " _
& "A.车型=E.车型 AND A.零件号=E.零件号 AND A.物资名称=E.物资名称) LEFT JOIN " _
& "(SELECT 车型,零件号,物资名称,SUM(数量) AS 数量1 FROM [外购入InBase] " & tmp2 & "GROUP BY 车型,零件号,物资名称) F ON " _
& "A.车型=F.车型 AND A.零件号=F.零件号 AND A.物资名称=F.物资名称) LEFT JOIN " _
& "(SELECT 车型,零件号,物资名称,SUM(数量) AS 数量1 FROM [外购出OutBase] " & tmp2 & "GROUP BY 车型,零件号,物资名称) G ON " _
& "A.车型=G.车型 AND A.零件号=G.零件号 AND A.物资名称=G.物资名称"
If [C2] <> "" Or [D2] <> "" Then
For I = 3 To 4
If Cells(2, I) <> "" Then
TMP1 = TMP1 & Cells(1, I) & " LIKE '%" & Cells(2, I) & "%' AND "
End If
Next
TMP1 = " WHERE " & Left(TMP1, Len(TMP1) - 4)
SQL = "SELECT * FROM (" & SQL & ")" & TMP1
End If
rs.Open SQL, cnn, 1, 1
If rs.RecordCount = 0 Then
rs.Close
Set rs = Nothing
cnn.Close
Set cnn = Nothing
MsgBox "无此记录": Exit Sub
End If
[a6].CopyFromRecordset cnn.Execute(SQL)
j = [a65536].End(xlUp).Row
Range("a6:i" & j).Borders.LineStyle = 1
rs.Close
Set rs = Nothing
cnn.Close
Set cnn = Nothing
Application.ScreenUpdating = True
End Sub
在制品查询.rar
(76.08 KB, 下载次数: 206)
|
评分
-
1
查看全部评分
-
|