我正在嘗試更新標簽標題屬性作為正在執行的代碼進度的一部分,以讓用戶知道正在執行什么任務。在代碼完成之前,一切都很好。最后,它顯示的是FALSE值,而不是零售所顯示的所有訊息。我如何確保它保留顯示的所有訊息?Forms!Main!Message.Caption = Forms!Main!Message.Caption是向標簽控制元件顯示訊息進度的訊息。
這是示例代碼:
If strSource_System <> "" Then
'--Download AP Data
Forms!Main!.ctrlProgressBar.Value = Timer - startTime
Forms!Main!Message.Caption = Forms!Main!Message.Caption & vbCrLf & "Downloading AP Data for System " & strSource_System & ". Please be patient..."
SleepSec (1)
Forms!Main!.ctrlProgressBar.Value = Timer - startTime
Call Download_AP_Data(Source_CONNECTION, "SOURCE")
Forms!Main!.ctrlProgressBar.Value = Timer - startTime
'Reset Clock
startTime = Timer
Forms!Main!Message.Caption = Forms!Main!Message.Caption & vbCrLf & "AP Data Downloaded for System " & strSource_System & " Successfully."
SleepSec (1)
Forms!Main!.ctrlProgressBar.Value = Timer - startTime
End If
uj5u.com熱心網友回復:
您將所有訊息附加到同一標簽。標簽的文本在某些時候可能會超過其最大長度并產生錯誤。如果您有On Error Resume Next,則不會看到此錯誤。
我建議使用 aListBox代替。這允許您將訊息作為新專案添加到ListBox. ListBox 的行源型別必須是Value List。
一個標簽最多可以包含 2,048 個字符。ValueList 模式下的 ListBox 最多可容納 64K 個字符(即多 32 倍)。
如果這還不夠,請將 ListBox 系結到一個表并添加最多 64 條記錄(即最多 64k 條訊息)。
但是您也可以限制顯示的訊息數量,并在超過此限制時開始洗掉第一條訊息以獲得滾動顯示。
messageListBox.AddItem "Downloading AP Data for System " & strSource_System & ". Please be patient..."
DoEvents
如果你想自動滾動到最后添加的專案,你可以這樣做
' Scroll to the last item
messageListBox.SetFocus 'Next line does not work otherwise.
messageListBox.ListIndex = messageListBox.ListCount - 1
' Unselect it
messageListBox.Selected(messageListBox.ListCount - 1) = False
將此代碼提取到另一個 Sub 更容易
Private Sub DisplayMessage(ByVal message As String)
messageListBox.AddItem message
messageListBox.SetFocus
messageListBox.ListIndex = messageListBox.ListCount - 1
messageListBox.Selected(messageListBox.ListCount - 1) = False
DoEvents
End Sub
然后用
DisplayMessage "some message"
如果您將此 Sub 公開,那么您甚至可以將其放在 Main 表單中,然后從模塊中呼叫它
Forms!Main.DisplayMessage "some message"
這樣做的好處是該模塊不必知道任何關于標簽或 ListBox 的資訊。Sub 也可以這樣做ShowProgress。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/512863.html
標籤:vba毫秒访问
