Forms Keeping Information When Run Again...

Hello Experts,

I am currently using this form... which has a whole bunch of code behind the scene.

    Dim ItemNum(1 To 5) As String
    Dim ItemQty(1 To 5) As Integer
    Dim ItemDesc(1 To 5) As String
    Dim i As Integer
    
    
    
    ItemNum(1) = txtItem1Num.Value
    ItemNum(2) = txtItem2Num.Value
    ItemNum(3) = txtItem3Num.Value
    ItemNum(4) = txtItem4Num.Value
    ItemNum(5) = txtItem4Num.Value
    
    ItemQty(1) = Val(txtItem1Qty.Value)
    ItemQty(2) = Val(txtItem2Qty.Value)
    ItemQty(3) = Val(txtItem3Qty.Value)
    ItemQty(4) = Val(txtItem4Qty.Value)
    ItemQty(5) = Val(txtItem5Qty.Value)
    
    ItemDesc(1) = txtItem1Desc.Value
    ItemDesc(2) = txtItem2Desc.Value
    ItemDesc(3) = txtItem3Desc.Value
    ItemDesc(4) = txtItem4Desc.Value
    ItemDesc(5) = txtItem5Desc.Value
    
    For i = 1 To 5
        If ItemQty(i) <> 0 Then  ' if the qty is not a blank value then add the item
            strText = strText & "Item " & i & " - " & ItemNum(i) & " | " & ItemQty(i) & " | " & ItemDesc(i)
            strText = strText & "<br>"
        End If
        'if it was blank it will be skipped and the loop will move onto the next item (i will run from 1 to 10)
    Next i

Open in new window


qty
There is even more code, including other variables - where is where I think my problem lies.

When I run my form/code everything is fine the first time around.  But when I run it second time, all the information filled in the first time is still there.

How do I set it up so that my form is new everytime I open it?

Thanks

~ Geekamo
LVL 1
GeekamoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
How do you close the form?
0
GeekamoAuthor Commented:
Via a command button (Generate Email), or by the X if the user by any chance needed to close that way.
0
GeekamoAuthor Commented:
Actually to be more specific, when the user clicks on "generate email" - I also run this code, to close the form "    SampleRequest.Hide"
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

GrahamSkanRetiredCommented:
That's it. Use

Unload SampleRequest

instead
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GeekamoAuthor Commented:
Ok, half way there...

I have updated the code, and the form does show brand new each time.  But I think the variables are still storing data behind the scenes, because when my code runs - it's inputting old data along with the new.

Do I also need code to clear variables?

Any ideas?
0
GrahamSkanRetiredCommented:
The values of Global (Public) variables will persist until an End instruction is executed, so you should do that when you have finished. Either that, or don't rely on the values until you have explicitly set them up.
0
GeekamoAuthor Commented:
Hmm, so I have all of these put into a module named "Global Variables"

Option Explicit

'Declare global variables

'General variables

    Global mai As MailItem
    Global strText As String
    
'Sample request variables
    
    Global CustNum, CustName, ContNum, ContName, OrderNotes As String 'Customer information
    Global ShipName, ShipAdd1, ShipAdd2, ShipCity, ShipState, ShipZip, ShipAttn As String 'Shipping information
    Global Item1Num, Item2Num, Item3Num, Item4Num, Item5Num, Item6Num, Item7Num, Item8Num, Item9Num, Item10Num As String 'Items
    Global Item1Qty, Item2Qty, Item3Qty, Item4Qty, Item5Qty, Item6Qty, Item7Qty, Item8Qty, Item9Qty, Item10Qty As String 'Quantity
    Global Item1Desc, Item2Desc, Item3Desc, Item4Desc, Item5Desc, Item6Desc, Item7Desc, Item8Desc, Item9Desc, Item10Desc As String 'Descriptions

Open in new window

   
Can you provide me a little more direction as to what I should be doing? I'm lost here. :)

~ Geekamo
0
GrahamSkanRetiredCommented:
I'm a bit confused about where we are in this.

Is that some rapid recoding in response to my last comment, or was it like that before?

Either way, there a couple of things that I would advise against.

Firstly, keep the number of global variables to a minimum. That way it is easier to keep track of where the values get modified. This can be a real difficulty in complex projects.

Also you might not realise that you are declaring CustNum, CustName, ContNum and ContName as Variant type. Only OrderNotes in that line will be a string.

In our context, you are having problems because the values are remembered between sessions. From where you are now, the simplest thing would be just to add an End instruction.
Unload SampleRequest
End

Open in new window

0
Martin LissOlder than dirtCommented:
I'd like to understand why you believe you need all those global variables but to stop the form from including old data you could create a sub that does this

Sub Reset()

CustNum = ""
CustName = ""
ContNum = ""
' etc

End Sub

Open in new window


and call that sub when the form unloads.
0
GeekamoAuthor Commented:
Actually, Idk if I need them global.  My idea was is that I will have variables that many different forms can use.  And I ended up putting variables in there that would be specific to one form.  So I will eventually go through them and if its a variable that many will use I'll keep it global, and if its specific to a form, ill put it within that sub.  Atleast I think that's what I should be doing.

As for the clearing of variables, I can do all that, but what is the code I put in the form code to basically jump over to the new sub that will clean all the variables?
0
GrahamSkanRetiredCommented:
Have you tried using the 'End' statement?
0
Martin LissOlder than dirtCommented:
...what is the code I put in the form code to basically jump over to the new sub that will clean all the variables?

You don't actually need a new sub. just do this.

Private Sub UserForm_Terminate()

CustNum = ""
CustName = ""
ContNum = ""
' etc

End Sub

Open in new window


Or if you want you can call my 'Reset' sub this way.
Private Sub UserForm_Terminate()

Reset

End Sub

Open in new window

0
GeekamoAuthor Commented:
I have removed all of my global variables. (Code has changed quite a bit since I last posted.  Much cleaner looking...)

~ Geekamo
0
GrahamSkanRetiredCommented:
It is always a pleasure to help someone who seems to be learning and is actually taking on board the techniques and not just blocks of code.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.