我正在撰寫一個訪問應用程式,我只是花時間設定一個類,使應用程式易于理解和高效。
但是,當我需要第二次單擊事件的物件時,該物件不再在記憶體中。
我讓用戶單擊設定物件并進行一些測驗的按鈕:
Public this_renewal As Renewal
Private Sub cmdMA_Click()
Set this_renewal = Factory.CreateRenewal(cMA)
Call BranchLabelVisibility(True)
Me.lblBranchToAdd.Caption = this_renewal.Abb
Call DateLabelVisibility(True)
Me.lblYearToAdd.Caption = this_renewal.Year
Me.lblMonthToAdd.Caption = this_renewal.Month
Call TestMonth
End Sub
這是我在一個名為 factory 的常規模塊中的 CreateRenewal 函式。我從另一個執行緒中得到了這個關于如何用屬性初始化類的想法:VBA:用值初始化物件?
Public Function CreateRenewal(strFileName As String) As Renewal
Dim renewal_obj As Renewal
Set renewal_obj = New Renewal
Call renewal_obj.InitiateProperties(strFileName)
Set CreateRenewal = renewal_obj
End Function
并在更新類中呼叫 InititateProperties:
Public Sub InitiateProperties(ByVal strFileName As String)
strRenewalFile = strFileName
strRenewalFullFileName = fnGetFullFileName()
strRenewalFileAndPath = cPath & strRenewalFullFileName
strBranchLetter = fnGetLetterFromFile(strRenewalFile)
strAbb = DLookup("BranchAbb", "tblBranches", "BranchLetter = '" & strBranchLetter & "'")
strBranchName = DLookup("Branch", "tblBranches", "BranchAbb = '" & strAbb & "'")
If Len(Mid(strRenewalFullFileName, 10, 2)) = 1 Then
strRenewalMonth = "0" & Mid(strRenewalFullFileName, 10, 2)
Else
strRenewalMonth = Mid(strRenewalFullFileName, 10, 2)
End If
strRenewal2DigitYear = Mid(strRenewalFullFileName, 12, 2)
strRenewalYear = "20" & strRenewal2DigitYear
strRenewalTable = strAbb & " " & strRenewalYear & " Renewals"
End Sub
然后用戶決定是否要匯入 this_renewal 檔案(這是記憶體中的物件)并單擊匯入按鈕,如果他們這樣做運行此代碼:
Private Sub cmdImport_Click()
DoCmd.SetWarnings False
Call FixExcelFile
Dim strTableName As String
Dim strImportName As String
strTableName = this_renewal.Table
'strImportName = Left(fnGetFileName(Me.tbFileToImport), 8)
Call ImportTable
'Count and Display Values of the two Tables
Call TableLabelVisibility(True)
Call GetTableValues
'Create Backup of Original
Call CreateBackup
'Run Queries to Update Data of Import
Call AppendQuery
Call UpdateMonth
Call UpdateStatus
Call UpdateUnderWriter
Call ShowResults
DoCmd.SetWarnings True
End Sub
If I step through the code during the cmdMA_Click event the object is created and those properties are initialized within the InitiateProperties sub in the class. After that click event finishes and I trigger the next click event (on the next button, on the same form) I receive "Object Variable or With Variable not set" for the object. Within the original click event I have no issue.
uj5u.com熱心網友回復:
由于Factory.CreateRenewal(cMA)未顯示相關代碼,因此我創建了一個如何重現錯誤的示例。
我有一個 this_renewal帶有以下代碼的課程
Option Explicit
Public Function myTest() As String
myTest = "Test"
End Function
然后我Factory用Attribute VB_PredeclaredId = True下面的代碼創建了一個類
Option Explicit
Public Function CreateRenewal(cMA As String) As this_renewal
' Doing nothing here
' result will be that CreateRenewal will return Nothing
' which will lead to the error
' "Object Variable or With Variable not set"
End Function
在用戶表單中,我有兩個帶有代碼的按鈕
Option Explicit
Public this_renewal As this_renewal
Private Sub btn1_Click()
Set this_renewal = Factory.CreateRenewal("cMA")
End Sub
Private Sub btn2_Click()
Debug.Print this_renewal.myTest
End Sub
如果我將Factory類中的代碼更改為
Public Function CreateRenewal(cMA As String) As this_renewal
Set CreateRenewal = New this_renewal
End Function
萬一我先按 btn1 然后再按 btn2,一切都會正常作業。
uj5u.com熱心網友回復:
如果該類 var 對表單的代碼是公開的并被創建?然后它將并且應該在該表單打開的整個生命周期內持續存在。您當然不能在全域級別或以其他形式參考您以該形式創建的類變數。
因此,您可以將 var 宣告移動到標準公共模塊(在表單之外),或者實際上以其他形式執行此操作:
在我們的表單中 - 在表單級別模塊中宣告類 - 為 public
Option Compare Database
Option Explicit
Public this_renewal As Renewal
所以,現在說你的表單運行代碼來設定物件,就像你這樣:
Set this_renewal = Factory.CreateRenewal(cMA)
現在,如果您打開任何其他表格?好吧,你有兩個選擇可以上你的課。
您可以參考上面的表格,并在代碼中執行此操作:
dim this_renewal As Renewal
set this_renewal = forms!ThatFirstFormabove.this_renewal.
因此,該類實體僅存在于您宣告 var (this_renewal) 的表單的背景關系中。
你也可以這樣做。以第一種形式說(使用 public this_renewal)。
您將在目標表單中,也可以在表單級別代碼中宣告 this_renewal。
然后你會并且可以這樣做:
docmd.OpenForm("frmB")
set forms("frmB").this_renewal = me.this_renewal
因此,要么傳遞類,要么直接從具有該類的當前表單參考它,或者將類的 var 宣告移出表單模塊代碼,并將其放在全域標準平面 jane 模塊中。在module1中說。
如果您這樣做,那么所有表單和所有代碼都可以使用該類的該實體。
uj5u.com熱心網友回復:
我想到了; 發生這種事真是太愚蠢了。因此,在第一次單擊事件期間呼叫的最后一個程序中:
Private Sub TestMonth()
If this_renewal.IsNewMonth Then
Me.cmdImport.Enabled = True
Me.lblFileMistake.Visible = False
Else
GoTo WrongFile
End If
endSub:
Exit Sub
WrongFile:
Me.cmdImport.Enabled = False
Me.lblFileMistake.Visible = True
Me.lblFileMistake.Caption = "Month " & this_renewal.Month & " was already added for branch " & this_renewal.Name & "!!"
GoTo endSub
End Sub
就在我有“Exit Sub”的地方,它是“End”。這清除了所有變數。一旦我將其更改為“退出子”,它現在可以正常作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/451411.html
