Results 1 to 6 of 6

Thread: VB6 User Defined Types (UDT) does not re-initiates (keeps old values)

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2020
    Posts
    7

    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

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2020
    Posts
    7

    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

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2020
    Posts
    7

    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!!!

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    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
  •  



Click Here to Expand Forum to Full Width