-
Apr 23rd, 2020, 10:10 AM
#1
Thread Starter
New Member
VB6 User Defined Types (UDT) does not re-initiates (keeps old values)
Hello friends,
i have a weird problem.
I created a UDT as PUBLIC in a module (PublicTypes.bas).
Code:
Public Type tApplication
ID As Long
Kid_ID As Long
Period_id As Long
Appl_Date As Date
Comments As String
Approved As Byte
Income As Currency
Garden() As Long
End Type
i use it in a form (FillApp.frm), through a "CollectData()" private sub to collect user inputs...
and i have declare it as shown below
Code:
Option Explicit
Private vApplication As tApplication
then i pass the vApplication variable to another public sub, in another module (DB_Application.bas)...
Code:
Public Function SaveApplication(ByRef p As tApplication) As Byte
... in order to save to database.
Works very well for the first time i open the form (FillApp.frm)
but when i unload it and re-opened it, the variable vApplication has not be re-initialized,
but contains the data from the last use of the form.
Why this is happening?
I want the variable to initialize at default values every time i open the FillApp.frm
Thank you a lot
-
Apr 23rd, 2020, 10:18 AM
#2
Re: VB6 User Defined Types (UDT) does not re-initiates (keeps old values)
When you close a form it's not actually unload, because of the implicit references in the usage of forms
2 methods to solve this
Code:
' Your routine where you show the form (using the implicit reference)
FillApp.Show vbModal
Set FillApp = Nothing
Or, the way I normally do it:
Code:
Dim fApp As FillApp ' create an explicit reference
Set fApp = New FillApp
fApp.Show vbModal
Set fApp = Nothing
-
Apr 23rd, 2020, 12:56 PM
#3
Thread Starter
New Member
Re: VB6 User Defined Types (UDT) does not re-initiates (keeps old values)
Thank you Arnoutdv for the fast reply!!!!
I will adapt your solution.
At this very moment, i try the following.
Code:
Private Function rst() As tApplication
End Function
and at Form_Load event i do
Code:
...
vApplication = rst()
...
I would appreciate your opinion.
Thank you
-
Apr 23rd, 2020, 01:03 PM
#4
Re: VB6 User Defined Types (UDT) does not re-initiates (keeps old values)
There are several ways to reset items you add to your form's declarations section. In order of my preference:
1. If showing the form modally, then set the form to Nothing on the next line of code, or
2. In Form_Unload, reset them all, or
3. In Form_Load, reset them all
In my opinion using a function to reset vApplication is less efficient than simply: Set vApplication = Nothing
-
Apr 23rd, 2020, 01:25 PM
#5
Thread Starter
New Member
Re: VB6 User Defined Types (UDT) does not re-initiates (keeps old values)
Thank you LaVolpe!!!
I 'll try the "Set vApplication = Nothing".
I never used variable=nothing in the past (except of very rare cases, like recordsets)
and never had problems with form/module level scope variables.
But this time was another story...
Thank you again!!!
-
Apr 23rd, 2020, 01:32 PM
#6
Re: VB6 User Defined Types (UDT) does not re-initiates (keeps old values)
Personally, I use the forms' auto-instantiate variables, although many frown on this, but I don't know why.
For instance, if I've got a Form1, in the Form_Unload event, as the last line of code, I always place the following:
Code:
Private Sub Form_Unload(Cancel As Integer)
' Other cleanup stuff.
Set Form1 = Nothing
End Sub
What we always have to remember is that forms have two components: 1) a UI component, and 2) a COM/code component.
When we use Load Form1, Form1.Show, Form1.Hide, Form1.Visible = True/False, or Unload Form1, we're just messing with the UI component of the form. However, Load Form1 and Form1.Show may instantiate the COM/code component if it's not already instantiated.
But, most importantly, the only way to get rid of the COM/code component is to uninstantiate it (typically, by setting it to Nothing). And, if we don't do this, that COM/code component will stay instantiated (and that's where your module level variables are, including UDTs), and that's true even if the UI component is unloaded. And therein lies your problem.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|