|
下面这段代码是查询数据库中的数据暗示到工作表上。数据库中的“销售编号“有二个表示方式:如:0808RK-00001(即8月8日RK的第1单)0808XS-00002(即8月8日RK的第2单)。下面的代码是显示所有的数据,我希望能只显示带RK的数据,请各位老师出手相助帮忙改一下代码,非常感谢!
Sub pppp() '刷新库存数据明细,应用于商品入库
Application.ScreenUpdating = False
Sheet20.Visible = xlSheetVisible '解除表式sheet4隐藏
Sheet20.Select
Set cnn = CreateObject("ADODB.Connection")
Set rs1 = CreateObject("ADODB.Recordset")
Set RS2 = CreateObject("ADODB.Recordset")
Dim DBPath$, DepartureDate$, SQL$, SQL1$, SQL2$, i%
DBPath = Sheets("主页").Range("L6")
On Error GoTo errhandler
With 商品入库
操作人 = UCase(.ComboBox4)
If Len(操作人) Then
Operator = " WHERE 操作人='" & 操作人 & "' "
Else
Operator = " WHERE TRUE "
End If
If IsDate(.TextBox10) Then
If IsDate(.TextBox11) Then
起 = DateSerial(Year(.TextBox10), Month(.TextBox10), Day(.TextBox10))
止 = DateSerial(Year(.TextBox11), Month(.TextBox11), Day(.TextBox11))
DepartureDate = "AND (日期 BETWEEN #" & 起 & "# AND #" & 止 & "#) "
Else
DepartureDate = "AND TRUE "
End If
End If
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DBPath & ";Jet OLEDB:Database Password=hqlin123456"
If cnn.State = 0 Then MsgBox "连接数据库错误!": Exit Sub
SQL1 = "SELECT 日期,销售编号,货物名称及规格,客户名称,购进数量,购进金额,销售数量,销售金额,0 AS FD1,单位,备注,销售单价 "
SQL1 = SQL1 & "FROM 销售表" & Operator & DepartureDate & "AND NOT 客户名称 IS NULL ORDER BY 日期"
rs1.Open SQL1, cnn, 1, 3
If rs1.RecordCount = 0 Then MsgBox 操作人 & "没有任何数据!": Exit Sub
Sheets("收付库存明细").Select
lastrow = Range("B" & Rows.Count).End(3).Row
If lastrow > 2 Then Range("B6:M" & lastrow).ClearContents
If rs1.RecordCount > 0 Then Range("B6").CopyFromRecordset rs1
lastrow = Application.WorksheetFunction.max(3, Range("B" & Rows.Count).End(3).Row)
Range("B" & lastrow + 1).CopyFromRecordset rs1
End With
Sheets("主页").Select
Range("C6").Select
exitsub:
Application.ScreenUpdating = True
'Unload Me
Exit Sub
errhandler:
MsgBox Err.Description
Resume exitsub
End Sub
|
|