各位老师好,我写了如下代码,觉得速度有点慢,不知道能否再优化下以提高运行速度呢?
窗口 afterload 事件:
Forms("等待窗口").open()
Application.DoEvents
Dim sql,ex1,ex2 As String
ex1 = "Round(DateDiff( m,MAX(消费日期),GetDate()),0)"
ex2 = vbcrlf & "(Case" & vbcrlf
ex2+ ="When " & ex1 & ">= 13 Then '一星活跃'" & vbcrlf
ex2+ ="When " & ex1 & ">= 10 and " & ex1 & " < 13 Then '二星活跃'" & vbcrlf
ex2+ ="When " & ex1 & ">= 6 and " & ex1 & " < 10 Then '三星活跃'" & vbcrlf
ex2+ ="When " & ex1 & ">= 3 and " & ex1 & " < 6 Then '四星活跃'" & vbcrlf
ex2+ = "Else '五星活跃'" & vbcrlf
ex2+ ="End) As 活跃级别 "
'MessageBox.Show(ex2)
sql = "select * from (Select rjsytVIP消费信息}.VIP卡号, {rjsytVIP基本信息}.归属门店代号, {rjsyt管理层名单}.门店登录账号, {rjsytVIP拓展信息}.当前维护人 , " & ex1 & " As 几个月没来买, " & ex2 & " FROM "
sql+ = "((rjsyt管理层名单 LEFT JOIN rjsytVIP基本信息 ON rjsytVIP基本信息.归属门店代号 = rjsyt管理层名单.归属门店代号) RIGHT JOIN rjsytVIP消费信息 ON 卡号 = {rjsytVIP消费信息}.VIP卡号 RIGHT JOIN rjsytVIP拓展信息 ON {rjsytVIP拓展信息}.VIP卡号 = {rjsytVIP消费信息}.VIP卡号 ) where {rjsytVIP消费信息}.[_Identify] Is Null GROUP BY {rjsytVIP消费信息}.VIP卡号, {rjsytVIP基本信息}.归属门店代号,门店登录账号 , 当前维护人) As a Where " & " 门店登录账号='" & User.name & "'"
'MessageBox.Show(sql)
DataTables("VIP行为信息_VIP行为信息").Fill(sql,"VIP数据", True)
Forms("等待窗口").close()
输入加载条件后,点击【查询】可以加载数据的【查询】按钮的click事件:
Forms("等待窗口").open()
Application.DoEvents
Dim sql,ex1,ex2,filter As String
With e.Form.Controls("销售主管")
If .Value IsNot Nothing Then
Filter = "销售主管 = '" & .Value & "' and "
End If
End With
With e.Form.Controls("归属门店代号")
If .Value IsNot Nothing Then
Filter+ = "归属门店代号 = '" & .Value & "' and "
End If
End With
With e.Form.Controls("活跃级别")
If .Value IsNot Nothing Then
Filter+ = "活跃级别 = '" & .Value & "' "
End If
End With
ex1 = "Round(DateDiff( m,MAX(消费日期),GetDate()),0)"
ex2 = vbcrlf & "(Case" & vbcrlf
ex2+ ="When " & ex1 & ">= 13 Then '一星活跃'" & vbcrlf
ex2+ ="When " & ex1 & ">= 10 and " & ex1 & " < 13 Then '二星活跃'" & vbcrlf
ex2+ ="When " & ex1 & ">= 6 and " & ex1 & " < 10 Then '三星活跃'" & vbcrlf
ex2+ ="When " & ex1 & ">= 3 and " & ex1 & " < 6 Then '四星活跃'" & vbcrlf
ex2+ = "Else '五星活跃'" & vbcrlf
ex2+ ="End) As 活跃级别 "
'MessageBox.Show(ex2)
sql = "select * from (Select rjsytVIP消费信息}.VIP卡号, {rjsytVIP基本信息}.归属门店代号, {rjsyt管理层名单}.门店登录账号, {rjsytVIP拓展信息}.当前维护人 , " & ex1 & " As 几个月没来买, " & ex2 & " FROM "
sql+ = "((rjsyt管理层名单 LEFT JOIN rjsytVIP基本信息 ON rjsytVIP基本信息.归属门店代号 = rjsyt管理层名单.归属门店代号) RIGHT JOIN rjsytVIP消费信息 ON 卡号 = {rjsytVIP消费信息}.VIP卡号 RIGHT JOIN rjsytVIP拓展信息 ON {rjsytVIP拓展信息}.VIP卡号 = {rjsytVIP消费信息}.VIP卡号 ) where {rjsytVIP消费信息}.[_Identify] Is Null GROUP BY {rjsytVIP消费信息}.VIP卡号, {rjsytVIP基本信息}.归属门店代号,门店登录账号 , 当前维护人) As a Where " & " 门店登录账号='" & User.name & "'"
If Filter > "" Then
sql = "select * from (Select rjsytVIP消费信息}.VIP卡号, {rjsytVIP基本信息}.归属门店代号, {rjsyt管理层名单}.门店登录账号, {rjsytVIP拓展信息}.当前维护人 , " & ex1 & " As 几个月没来买, " & ex2 & " FROM "
sql+ = "((rjsyt管理层名单 LEFT JOIN rjsytVIP基本信息 ON rjsytVIP基本信息.归属门店代号 = rjsyt管理层名单.归属门店代号) RIGHT JOIN rjsytVIP消费信息 ON 卡号 = {rjsytVIP消费信息}.VIP卡号 RIGHT JOIN rjsytVIP拓展信息 ON {rjsytVIP拓展信息}.VIP卡号 = {rjsytVIP消费信息}.VIP卡号 ) GROUP BY {rjsytVIP消费信息}.VIP卡号, {rjsytVIP基本信息}.归属门店代号,门店登录账号 , 当前维护人) As a Where " & Filter & " 门店登录账号='" & User.name & "'"
' MessageBox.Show(sql)
DataTables("VIP行为信息_VIP行为信息").SQLLoad(sql)
End If
Forms("等待窗口").close()
以上为一个可以让用户自己输入加载条件进行加载的窗口的两段代码。
以下为一个自定义菜单里预先设置好条件进行加载的按钮的代码:
如:【三天回访】(即:根据最后购买日期后第三天的条件进行加载),用户只有点击,即可加载前两天购买的顾客,不用再输入加载条件。
Forms("VIP行为信息").Open()
MainTable = Tables("空表")
Forms("等待窗口").open()
Application.DoEvents
Dim sql,ex1,ex2 As String
ex1 = "Round(DateDiff( m,MAX(消费日期),GetDate()),0)"
ex2 = vbcrlf & "(Case" & vbcrlf
ex2+ ="When " & ex1 & ">= 13 Then '一星活跃'" & vbcrlf
ex2+ ="When " & ex1 & ">= 10 and " & ex1 & " < 13 Then '二星活跃'" & vbcrlf
ex2+ ="When " & ex1 & ">= 6 and " & ex1 & " < 10 Then '三星活跃'" & vbcrlf
ex2+ ="When " & ex1 & ">= 3 and " & ex1 & " < 6 Then '四星活跃'" & vbcrlf
ex2+ = "Else '五星活跃'" & vbcrlf
ex2+ ="End) As 活跃级别 "
sql = "select * from (Select rjsytVIP消费信息}.VIP卡号, {rjsytVIP基本信息}.归属门店代号, {rjsyt管理层名单}.门店登录账号, {rjsytVIP拓展信息}.当前维护人 , " & ex1 & " As 几个月没来买, " & ex2 & " FROM "
sql+ = "((rjsyt管理层名单 LEFT JOIN rjsytVIP基本信息 ON rjsytVIP基本信息.归属门店代号 = rjsyt管理层名单.归属门店代号) RIGHT JOIN rjsytVIP消费信息 ON 卡号 = {rjsytVIP消费信息}.VIP卡号 RIGHT JOIN rjsytVIP拓展信息 ON {rjsytVIP拓展信息}.VIP卡号 = {rjsytVIP消费信息}.VIP卡号 ) GROUP BY {rjsytVIP消费信息}.VIP卡号, {rjsytVIP基本信息}.归属门店代号,门店登录账号 , 当前维护人) As a Where " & " 门店登录账号='" & User.name & "'"
' MessageBox.Show(sql)
DataTables("VIP行为信息_VIP行为信息").SQLLoad(sql)
Dim dt2 As Date = Date.Today.AddDays(-2)
Tables("VIP行为信息_VIP行为信息").filter = "最后购买日 = #" & dt2 & "# "
Forms("等待窗口").close()
[此贴子已经被作者于2014-3-29 20:07:08编辑过]