循环表和刷新查询

时间:2015-03-18 08:55:54

标签: excel vba excel-vba

我正在努力解决这个问题,并且不确定如何前进。 我有一个工作簿,有4"显示"床单。 它们包含数据和图表,并在具有统计数据和数字的大屏幕上显示。

数据是数据库链接查询,需要全天刷新。 工作表需要在一整天内不断循环。

我的代码是整个代码,因为我认为更容易看到整个图片。

如果我单步执行AutoLoad,它会完全按照我的意愿运行。它刷新查询,然后加载工作表5秒钟(作为J2中的输入)。

如果我执行代码,它会加载第一个工作表("每日调度数字"),但图表显示为全黑。 5秒后,第二个工作表加载(" Sales& GP Today"),但图表再次显示为黑色。

在此之后,它永远不会在屏幕上显示任何其他内容。它永远停留在" Sales&今日GP"工作表。

如果我按下ESC,代码退出,并且显示了我打破的循环中的工作表,因此代码在后台运行。

我以前曾尝试将查询设置为每1分钟自动刷新一次,甚至没有触及VBA中的刷新,但这似乎被忽略了,而任何"循环"代码正在运行,只有在我停止代码时才刷新,所以在循环工作时(使用与下面完全相同的方法),数据没有更新。

这是Excel 2013。

Sub Refresh()
RefreshQueries
MsgBox "Queries Refreshed"

End Sub



Sub RefreshQueries()
Dim tbl As ListObject
Dim DispatchTarget As String
Dim RepTargets As String
Dim DispatchesSql As String
Dim SalesGPSql As String
Dim RepvTargetSql As String
Dim x As Long
Dim Y As Integer

Set tbl = Sheets("Targets").ListObjects("tblMonthlyTargets")

For x = 2 To tbl.ListRows.Count + 1
    If x = tbl.ListRows.Count + 1 Then
    RepTargets = RepTargets & "('" & tbl.Range(x, 1).Value & "', '" & tbl.Range(x, 2).Value & "', '" & tbl.Range(x, 3) & "') "
    Else
    RepTargets = RepTargets & "('" & tbl.Range(x, 1).Value & "', '" & tbl.Range(x, 2).Value & "', '" & tbl.Range(x, 3) & "'), "
    End If
Next x


Set tbl = Sheets("Targets").ListObjects("tblDespTarget")
 DispatchTarget = tbl.Range(2, 1).Value

Set tbl = Sheets("Targets").ListObjects("tblMonthlyTargets")
MonthTarget = tbl.Range(Month(Date) + 1, 2).Value



' Sets Dispatches Summary Sql
DispatchesSql = _
"Select sum(isnull(((delivery_line_item.dli_qty*(order_line_item.oli_total_margin/order_line_item.oli_qty_required))/order_line_item.oli_price_per),0)) as TotalValue, " & _
DispatchTarget & " as 'Target', " & _
"case WHEN " & DispatchTarget & " -sum(isnull(((delivery_line_item.dli_qty*(order_line_item.oli_total_margin/order_line_item.oli_qty_required))/order_line_item.oli_price_per),0)) > 0  " & _
"THEN " & DispatchTarget & " -sum(isnull(((delivery_line_item.dli_qty*(order_line_item.oli_total_margin/order_line_item.oli_qty_required))/order_line_item.oli_price_per),0)) " & _
"Else 0.00 End as 'Remainder' " & _
"from delivery_header " & _
"join delivery_line_item on delivery_line_item.dli_dh_id = delivery_header.dh_id " & _
"join order_line_item on order_line_item.oli_id = delivery_line_item.dli_oli_id " & _
"where  dateadd(day,datediff(day,0,delivery_header.dh_datetime),0) = dateadd(day,datediff(day,0,getdate()),0) "



' Sets Sales GP SQL
SalesGPSql = _
"Select sum(order_header_total.oht_net) as 'TotalNet', " & _
"sum(order_header_total.oht_total_margin) as 'TotalGP' " & _
"from order_header " & _
"join order_header_total on order_header_total.oht_oh_id = order_header.oh_id " & _
"where order_header.oh_sot_id = 1 " & _
"and dateadd(day,datediff(day,0,order_header.oh_datetime),0) = dateadd(day,datediff(day,0,getdate()),0) "



' Sets Rep v Target SQL

RepvTargetSql = _
"With SalesPeople as (select X.Person, X.Month, X.Target From (Values    " & _
RepTargets & _
") as X (Person, Month, Target))   " & _
"SELECT user_detail.ud_username,  " & _
"isnull(TodaysTotal.Net, 0) AS 'Net Total ',  " & _
"CASE WHEN WorkingDaysLeft.DaysLeft = 0 THEN SalesPeople.Target - isnull(MonthTotals.Net, 0) ELSE (SalesPeople.Target - isnull(MonthTotals.Net, 0)) / WorkingDaysLeft.DaysLeft END AS 'Target',  " & _
"CASE WHEN (CASE WHEN WorkingDaysLeft.DaysLeft = 0 THEN SalesPeople.Target - isnull(MonthTotals.Net, 0) ELSE (SalesPeople.Target - isnull(MonthTotals.Net, 0)) / WorkingDaysLeft.DaysLeft END) - isnull(TodaysTotal.Net, 0) < = 0  " & _
"THEN 0 ELSE (CASE WHEN WorkingDaysLeft.DaysLeft = 0 THEN SalesPeople.Target - isnull(MonthTotals.Net, 0) ELSE (SalesPeople.Target - isnull(MonthTotals.Net, 0)) / WorkingDaysLeft.DaysLeft END) - isnull(TodaysTotal.Net, 0) END AS 'Remaining' "
RepvTargetSql = RepvTargetSql & _
"FROM user_detail " & _
"LEFT JOIN ( " & _
    "SELECT user_detail.ud_id, sum(order_header_total.oht_total_margin) AS Net " & _
    "FROM order_header " & _
    "JOIN order_header_total ON order_header_total.oht_oh_id = order_header.oh_id " & _
    "JOIN order_header_detail ON order_header_detail.ohd_oh_id = order_header.oh_id " & _
    "JOIN user_detail ON user_detail.ud_id = order_header_detail.ohd_sales_rep " & _
    "WHERE dateadd(day, datediff(day, 0, order_header.oh_datetime), 0) = dateadd(day, datediff(day, 0, getdate()), 0) AND order_header.oh_sot_id = 1 " & _
    "GROUP BY user_detail.ud_id ) AS TodaysTotal ON TodaysTotal.ud_id = user_detail.ud_id " & _
"LEFT JOIN ( " & _
    "SELECT user_detail.ud_id, sum(order_header_total.oht_total_margin) AS Net " & _
    "FROM order_header " & _
    "JOIN order_header_total ON order_header_total.oht_oh_id = order_header.oh_id " & _
    "JOIN order_header_detail ON order_header_detail.ohd_oh_id = order_header.oh_id " & _
    "JOIN user_detail ON user_detail.ud_id = order_header_detail.ohd_sales_rep " & _
    "WHERE dateadd(day, datediff(day, 0, order_header.oh_datetime), 0) = dateadd(day, datediff(day, 0, getdate()), 0) AND order_header.oh_sot_id = 1 " & _
    "GROUP BY user_detail.ud_id " & _
    ") AS MonthTotals ON MonthTotals.ud_id = user_detail.ud_id " & _
"JOIN (SELECT (DATEDIFF(dd, getdate(), dateadd(day, - 1, dateadd(month, datediff(month, 0, getdate()) + 1, 0))) + 1) - (DATEDIFF(wk, getdate(), dateadd(day, - 1, dateadd(month, datediff(month, 0, getdate()) + 1, 0))) * 2) -  " & _
    "(CASE WHEN DATENAME(dw, getdate()) = 'Sunday' THEN 1 ELSE 0 END) - (CASE WHEN DATENAME(dw, dateadd(day, - 1, dateadd(month, datediff(month, 0, getdate()) + 1, 0))) = 'Saturday' THEN 1 ELSE 0 END) AS DaysLeft " & _
    ") AS WorkingDaysLeft ON 1 = 1 " & _
"JOIN SalesPeople on SalesPeople.Person = ud_username and SalesPeople.Month = datename(month,getdate()) " & _
"WHERE user_detail.ud_active = 1 " & _
"ORDER BY user_detail.ud_username "

' Debug.Print DispatchesSql

 With ActiveWorkbook.Connections("qryDespatches").ODBCConnection
    .BackgroundQuery = False
    .CommandText = DispatchesSql
    .Refresh
End With

With ActiveWorkbook.Connections("qrySalesGPToday").ODBCConnection
    .BackgroundQuery = False
    .CommandText = SalesGPSql
    .Refresh
End With

With ActiveWorkbook.Connections("qryRepDaily").ODBCConnection
    .BackgroundQuery = False
    .CommandText = RepvTargetSql
    .Refresh
End With


With ActiveWorkbook.Connections("qryDespPicksOpenOrders").ODBCConnection
    .BackgroundQuery = False
    .Refresh
End With


End Sub






Sub AutoLoad()
Dim tbl As ListObject
Dim LoopTime As String

Set tbl = Sheets("Targets").ListObjects("tblTime")
 LoopTime = tbl.Range(2, 1).Value

LoopTime = Sheets("Targets").Range("J2").Value

Dim i As Long

On Error GoTo err_handler
Application.EnableCancelKey = xlErrorHandler
Application.ScreenUpdating = True

'sets Sql statements for today in queries
RefreshQueries

'Refresh Despatch ready for screen showing
RefreshDespatch

Do Until 1 = 2

Application.ScreenUpdating = True

'Loads up Daily Dispatch Figures worksheet
Sheets("Daily Dispatch Figures").Select
ActiveWindow.DisplayHeadings = False
Application.DisplayFormulaBar = False
Application.DisplayFullScreen = True
Range("A1:C36").Select
ActiveWindow.Zoom = True
Range("A1").Select
ActiveSheet.ChartObjects("DailyDespatchChart").Activate


'Refreshes Sales ready for next screen
RefreshSales

newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + LoopTime
Waittime = TimeSerial(newHour, newMinute, newSecond)


Application.Wait Waittime


'Loads up "Sales & GP Today" worksheet

Sheets("Sales & GP Today").Select
ActiveWindow.DisplayHeadings = False
Application.DisplayFormulaBar = False
Application.DisplayFullScreen = True
Range("A1:B35").Select
ActiveWindow.Zoom = True
Range("A1").Select
ActiveSheet.ChartObjects("SalesGPTodayChart").Activate

'Refresh Daily ready for next screen
RefreshDaily


newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + LoopTime
Waittime = TimeSerial(newHour, newMinute, newSecond)

Application.Wait Waittime



'Loads up Rep Daily Targets worksheet

Sheets("Rep Daily Targets").Select
ActiveWindow.DisplayHeadings = False
Application.DisplayFormulaBar = False
Application.DisplayFullScreen = True
Range("A1:B36").Select
ActiveWindow.Zoom = True
Range("A1").Select
ActiveSheet.ChartObjects("RepDailyChart").Activate

'Refreshes Live ready for next screen
RefreshLive


newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + LoopTime
Waittime = TimeSerial(newHour, newMinute, newSecond)

Application.Wait Waittime


'Loads up Live Summary worksheet

Sheets("Live Summary").Select
Range("A1").Select
ActiveWindow.DisplayHeadings = False
Application.DisplayFormulaBar = False
Application.DisplayFullScreen = True
Range("A1:B4").Select
ActiveWindow.Zoom = True
Range("A1").Select

'Refreshes Despatch ready for next screen
RefreshDespatch


newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + LoopTime
Waittime = TimeSerial(newHour, newMinute, newSecond)

Application.Wait Waittime



Loop


err_handler:
    If Err.Number = 18 Then
        EndReport
        MsgBox "Report Cancelled"
    End If

End Sub

Sub RefreshDespatch()
With ActiveWorkbook.Connections("qryDespatches").ODBCConnection
  .BackgroundQuery = False
  .Refresh
End With

End Sub

Sub RefreshSales()
With ActiveWorkbook.Connections("qrySalesGPToday").ODBCConnection
    .BackgroundQuery = False
    .Refresh
End With
End Sub

Sub RefreshDaily()
With ActiveWorkbook.Connections("qryRepDaily").ODBCConnection
    .BackgroundQuery = False
    .Refresh
End With
End Sub

Sub RefreshLive()

With ActiveWorkbook.Connections("qryDespPicksOpenOrders").ODBCConnection
    .BackgroundQuery = False
    .Refresh
End With
End Sub


Sub EndReport()
Application.DisplayFullScreen = False
ActiveWindow.DisplayHeadings = True
Application.DisplayFormulaBar = True
Application.ScreenUpdating = True
ActiveWindow.Zoom = 100
End Sub

1 个答案:

答案 0 :(得分:0)

我现在通过强制Do While延迟代码执行1秒来解决这个问题。 我认为2013年是如此快速地执行代码,以至于它在完成屏幕上的操作之前尝试执行下一行。

Sub Refresh()
RefreshQueries
MsgBox "Queries Refreshed"


End Sub



Sub RefreshQueries()




Dim tbl As ListObject
Dim DispatchTarget As String
Dim RepTargets As String
Dim DispatchesSql As String
Dim SalesGPSql As String
Dim RepvTargetSql As String
Dim x As Long
Dim Y As Integer

Set tbl = Sheets("Targets").ListObjects("tblMonthlyTargets")

For x = 2 To tbl.ListRows.Count + 1
    If x = tbl.ListRows.Count + 1 Then
    RepTargets = RepTargets & "('" & tbl.Range(x, 1).Value & "', '" & tbl.Range(x, 2).Value & "', '" & tbl.Range(x, 3) & "') "
    Else
    RepTargets = RepTargets & "('" & tbl.Range(x, 1).Value & "', '" & tbl.Range(x, 2).Value & "', '" & tbl.Range(x, 3) & "'), "
    End If
Next x


Set tbl = Sheets("Targets").ListObjects("tblDespTarget")
 DispatchTarget = tbl.Range(2, 1).Value

Set tbl = Sheets("Targets").ListObjects("tblMonthlyTargets")
MonthTarget = tbl.Range(Month(Date) + 1, 2).Value



' Sets Dispatches Summary Sql
DispatchesSql = _
"Select sum(isnull(((delivery_line_item.dli_qty*(order_line_item.oli_total_margin/order_line_item.oli_qty_required))/order_line_item.oli_price_per),0)) as TotalValue, " & _
DispatchTarget & " as 'Target', " & _
"case WHEN " & DispatchTarget & " -sum(isnull(((delivery_line_item.dli_qty*(order_line_item.oli_total_margin/order_line_item.oli_qty_required))/order_line_item.oli_price_per),0)) > 0  " & _
"THEN " & DispatchTarget & " -sum(isnull(((delivery_line_item.dli_qty*(order_line_item.oli_total_margin/order_line_item.oli_qty_required))/order_line_item.oli_price_per),0)) " & _
"Else 0.00 End as 'Remainder' " & _
"from delivery_header " & _
"join delivery_line_item on delivery_line_item.dli_dh_id = delivery_header.dh_id " & _
"join order_line_item on order_line_item.oli_id = delivery_line_item.dli_oli_id " & _
"where  dateadd(day,datediff(day,0,delivery_header.dh_datetime),0) = dateadd(day,datediff(day,0,getdate()),0) "



' Sets Sales GP SQL
SalesGPSql = _
"Select sum(order_header_total.oht_net) as 'TotalNet', " & _
"sum(order_header_total.oht_total_margin) as 'TotalGP' " & _
"from order_header " & _
"join order_header_total on order_header_total.oht_oh_id = order_header.oh_id " & _
"where order_header.oh_sot_id = 1 " & _
"and dateadd(day,datediff(day,0,order_header.oh_datetime),0) = dateadd(day,datediff(day,0,getdate()),0) "



' Sets Rep v Target SQL

RepvTargetSql = _
"With SalesPeople as (select X.Person, X.Month, X.Target From (Values    " & _
RepTargets & _
") as X (Person, Month, Target))   " & _
"SELECT user_detail.ud_username,  " & _
"isnull(TodaysTotal.Net, 0) AS 'Net Total ',  " & _
"CASE WHEN WorkingDaysLeft.DaysLeft = 0 THEN SalesPeople.Target - isnull(MonthTotals.Net, 0) ELSE (SalesPeople.Target - isnull(MonthTotals.Net, 0)) / WorkingDaysLeft.DaysLeft END AS 'Target',  " & _
"CASE WHEN (CASE WHEN WorkingDaysLeft.DaysLeft = 0 THEN SalesPeople.Target - isnull(MonthTotals.Net, 0) ELSE (SalesPeople.Target - isnull(MonthTotals.Net, 0)) / WorkingDaysLeft.DaysLeft END) - isnull(TodaysTotal.Net, 0) < = 0  " & _
"THEN 0 ELSE (CASE WHEN WorkingDaysLeft.DaysLeft = 0 THEN SalesPeople.Target - isnull(MonthTotals.Net, 0) ELSE (SalesPeople.Target - isnull(MonthTotals.Net, 0)) / WorkingDaysLeft.DaysLeft END) - isnull(TodaysTotal.Net, 0) END AS 'Remaining' "
RepvTargetSql = RepvTargetSql & _
"FROM user_detail " & _
"LEFT JOIN ( " & _
    "SELECT user_detail.ud_id, sum(order_header_total.oht_total_margin) AS Net " & _
    "FROM order_header " & _
    "JOIN order_header_total ON order_header_total.oht_oh_id = order_header.oh_id " & _
    "JOIN order_header_detail ON order_header_detail.ohd_oh_id = order_header.oh_id " & _
    "JOIN user_detail ON user_detail.ud_id = order_header_detail.ohd_sales_rep " & _
    "WHERE dateadd(day, datediff(day, 0, order_header.oh_datetime), 0) = dateadd(day, datediff(day, 0, getdate()), 0) AND order_header.oh_sot_id = 1 " & _
    "GROUP BY user_detail.ud_id ) AS TodaysTotal ON TodaysTotal.ud_id = user_detail.ud_id " & _
"LEFT JOIN ( " & _
    "SELECT user_detail.ud_id, sum(order_header_total.oht_total_margin) AS Net " & _
    "FROM order_header " & _
    "JOIN order_header_total ON order_header_total.oht_oh_id = order_header.oh_id " & _
    "JOIN order_header_detail ON order_header_detail.ohd_oh_id = order_header.oh_id " & _
    "JOIN user_detail ON user_detail.ud_id = order_header_detail.ohd_sales_rep " & _
    "WHERE dateadd(day, datediff(day, 0, order_header.oh_datetime), 0) = dateadd(day, datediff(day, 0, getdate()), 0) AND order_header.oh_sot_id = 1 " & _
    "GROUP BY user_detail.ud_id " & _
    ") AS MonthTotals ON MonthTotals.ud_id = user_detail.ud_id " & _
"JOIN (SELECT (DATEDIFF(dd, getdate(), dateadd(day, - 1, dateadd(month, datediff(month, 0, getdate()) + 1, 0))) + 1) - (DATEDIFF(wk, getdate(), dateadd(day, - 1, dateadd(month, datediff(month, 0, getdate()) + 1, 0))) * 2) -  " & _
    "(CASE WHEN DATENAME(dw, getdate()) = 'Sunday' THEN 1 ELSE 0 END) - (CASE WHEN DATENAME(dw, dateadd(day, - 1, dateadd(month, datediff(month, 0, getdate()) + 1, 0))) = 'Saturday' THEN 1 ELSE 0 END) AS DaysLeft " & _
    ") AS WorkingDaysLeft ON 1 = 1 " & _
"JOIN SalesPeople on SalesPeople.Person = ud_username and SalesPeople.Month = datename(month,getdate()) " & _
"WHERE user_detail.ud_active = 1 " & _
"ORDER BY user_detail.ud_username "

' Debug.Print DispatchesSql

 With ActiveWorkbook.Connections("qryDespatches").ODBCConnection
    .BackgroundQuery = False
    .CommandText = DispatchesSql
    .Refresh
End With

With ActiveWorkbook.Connections("qrySalesGPToday").ODBCConnection
    .BackgroundQuery = False
    .CommandText = SalesGPSql
    .Refresh
End With

With ActiveWorkbook.Connections("qryRepDaily").ODBCConnection
    .BackgroundQuery = False
    .CommandText = RepvTargetSql
    .Refresh
End With


With ActiveWorkbook.Connections("qryDespPicksOpenOrders").ODBCConnection
    .BackgroundQuery = False
    .Refresh
End With


End Sub






Sub AutoLoad()
Dim tbl As ListObject
Dim LoopTime As String
Dim Start As Single

Set tbl = Sheets("Targets").ListObjects("tblTime")
 LoopTime = tbl.Range(2, 1).Value

LoopTime = Sheets("Targets").Range("J2").Value

Dim i As Long

On Error GoTo err_handler
Application.EnableCancelKey = xlErrorHandler
Application.ScreenUpdating = True

'sets Sql statements for today in queries
RefreshQueries

'Refresh Despatch ready for screen showing
Start = Timer
'wait 1 sec...
Do While Start + 1 > Timer
    DoEvents
Loop
RefreshDespatch


Do Until 1 = 2

Application.ScreenUpdating = True

'Loads up Daily Dispatch Figures worksheet
Sheets("Daily Dispatch Figures").Select
ActiveWindow.DisplayHeadings = False
Application.DisplayFormulaBar = False
Application.DisplayFullScreen = True
Range("A1:C36").Select
ActiveWindow.Zoom = True
Range("A1").Select
ActiveSheet.ChartObjects("DailyDespatchChart").Activate


'Refreshes Sales ready for next screen
Start = Timer
'wait 1 sec...
Do While Start + 1 > Timer
    DoEvents
Loop
RefreshSales




newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + LoopTime
Waittime = TimeSerial(newHour, newMinute, newSecond)


Application.Wait Waittime


'Loads up "Sales & GP Today" worksheet

Sheets("Sales & GP Today").Select
ActiveWindow.DisplayHeadings = False
Application.DisplayFormulaBar = False
Application.DisplayFullScreen = True
Range("A1:B35").Select
ActiveWindow.Zoom = True
Range("A1").Select
ActiveSheet.ChartObjects("SalesGPTodayChart").Activate

'Refresh Daily ready for next screen
Start = Timer
'wait 1 sec...
Do While Start + 1 > Timer
    DoEvents
Loop
RefreshDaily


newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + LoopTime
Waittime = TimeSerial(newHour, newMinute, newSecond)

Application.Wait Waittime



'Loads up Rep Daily Targets worksheet

Sheets("Rep Daily Targets").Select
ActiveWindow.DisplayHeadings = False
Application.DisplayFormulaBar = False
Application.DisplayFullScreen = True
Range("A1:B36").Select
ActiveWindow.Zoom = True
Range("A1").Select
ActiveSheet.ChartObjects("RepDailyChart").Activate

'Refreshes Live ready for next screen
Start = Timer
'wait 1 sec...
Do While Start + 1 > Timer
    DoEvents
Loop
RefreshLive




newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + LoopTime
Waittime = TimeSerial(newHour, newMinute, newSecond)

Application.Wait Waittime


'Loads up Live Summary worksheet

Sheets("Live Summary").Select
Range("A1").Select
ActiveWindow.DisplayHeadings = False
Application.DisplayFormulaBar = False
Application.DisplayFullScreen = True
Range("A1:B4").Select
ActiveWindow.Zoom = True
Range("A1").Select

'Refreshes Despatch ready for next screen
Start = Timer
'wait 1 sec...
Do While Start + 1 > Timer
    DoEvents
Loop
RefreshDespatch


newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + LoopTime
Waittime = TimeSerial(newHour, newMinute, newSecond)

Application.Wait Waittime



Loop


err_handler:
    If Err.Number = 18 Then
        EndReport
        MsgBox "Report Cancelled"
    End If


End Sub

Sub RefreshDespatch()
With ActiveWorkbook.Connections("qryDespatches").ODBCConnection
  .BackgroundQuery = False
  .Refresh
End With

End Sub

Sub RefreshSales()
With ActiveWorkbook.Connections("qrySalesGPToday").ODBCConnection
    .BackgroundQuery = False
    .Refresh
End With
End Sub

Sub RefreshDaily()
With ActiveWorkbook.Connections("qryRepDaily").ODBCConnection
    .BackgroundQuery = False
    .Refresh
End With
End Sub

Sub RefreshLive()

With ActiveWorkbook.Connections("qryDespPicksOpenOrders").ODBCConnection
    .BackgroundQuery = False
    .Refresh
End With
End Sub


Sub EndReport()

Application.DisplayFullScreen = False
ActiveWindow.DisplayHeadings = True
Application.DisplayFormulaBar = True
Application.ScreenUpdating = True
ActiveWindow.Zoom = 100

End Sub