Solved

Global Variables

Posted on 2000-03-14
7
284 Views
Last Modified: 2010-05-02
Hi

I have a database that has several view screens, each runs a query that takes in the two dates  From:  To:

I would like to be able to save the user from having to input these dates every time a different screen is chosen during a session.

I would like to store the two dates when first entered, as two global variables, into a location that is visible to all the view forms of the project.

The idea would then be to "collect" these dates (on Form_Load) when the user enters another view form and populate the two date textbox's, allowing the user to continue with the view query.

Any ideas please.

Rgds Tam97
0
Comment
Question by:tam97
7 Comments
 

Accepted Solution

by:
ckhlam earned 100 total points
Comment Utility
If you want all the forms can use the same value, it is a good choice to use global variabile.
However, you must take care if the global variable have not been set, or otherwise, runtime error may given when using these fields.
0
 

Author Comment

by:tam97
Comment Utility
Hi ckhlam

Can you advise on coding and method of implementing global variables in VB5, to suit the purposes outlined.

Rgds Tam97
0
 

Expert Comment

by:ckhlam
Comment Utility
from a Module, declare two variable:
public gDateFrom as String
public gDateTo as String

For every screen, in form_load:
txtDateFrom = iif (isdate(gdatefrom), gdatefrom, "")
txtdateto = iif(isdate(gdateto), gdateto, "")

For every screen, in form_unload:
gDateFrom = iif(isdate(trim(txtDateFrom)),trim(txtdatefrom), "")
gDateTo = iif(isdate(trim(txtDateTo)), trim(txtdateto), "")

the reason that not using date type in the variable is that it is difficult to determine the initial value or reset value
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:tam97
Comment Utility
Hi ckhlam

Thanks very much for code, I'll try it out in the morning.

It's past 3am here so I'll call it a day, will get back to you tomorrow.

Thanks again.

Rgds Tam97
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
Create a separate BAS Module (VB Menu.. Project.. Add Module) and name it Globals (Its not necessary to name it that.. but from a documentation and readibilty standpoint it makes it very easy to find your globals when you need them).

In the Declarations Section (right after the On Explicit statement) define your variables with the keyword Public in front of it. Its a good idea to put a g in front of the variable name so that in other parts of your program you know that it is a global and stored in a separate module.

Option Explicit

<----- Code Begin ----->

Public gdtDateFrom as Date
Public gdtDateTo as Date

<----- Code End ----->

Now.. in your first Form_Load (or Sub_Main) prime the date fields with today's date.

<----- Code Begin ----->

    gdtDateFrom = Date
    gdtDateTo = Date
    txtDateFrom.Text = Format(gdtDateFrom, "mm/dd/yyyy")
    txtDateTo.Text = Format(gdtDateTo, "mm/dd/yyyy")

<----- Code End ----->



With this being done, you can now access these variable names from anywhere in your program.. UserControls.. Classes.. Forms.. etc.

Now.. on each your forms you should add two textboxes.. so that the User can both see the dates.. and if he/she likes.. change them as appropriate.

To update the Global Variables in each form that has the Date textboxes.. add the following.. (assuming the textbox names are txtDateFrom / txtDateTo). At the end of the txtDateTo_LostFocus Sub is a For Each loop that will update all controls (in any form) named txtDateFrom / txtDateTo with the new date values.

<----- Code Begin ----->

Private Sub txtDateFrom_LostFocus()
    If IsDate(txtDateFrom.Text) _
    Then
        gdtDateFrom = CDate(txtDateFrom.Text)
    Else
        MsgBox "Please enter the Date in mm/dd/yyyy Format"
    End If

    txtDateFrom.Text = Format(gdtDateFrom, "mm/dd/yyyy")

End Sub

Private Sub txtDateTo_LostFocus()

    If IsDate(txtDateTo.Text) _
    Then
        gdtDateTo = CDate(txtDateTo.Text)
    Else
        MsgBox "Please enter the Date in mm/dd/yyyy Format"
        Exit Sub
    End If

    If gdtDateTo < gdtDateFrom _
    Then
        MsgBox "Ending Date is Less than Starting Date"
        txtDateFrom.SetFocus
        Exit Sub
    End If

'   Update ALL Form Dates
    Dim ctlWork as Control
    For Each ctlWork in Controls
         If ctlWork.Name = txtDateFrom _
         Then
             txtDateFrom.Text = Format(gdtDateFrom, "mm/dd/yyyy")
         End If  
         If ctlWork.Name = txtDateTo _
         Then
             txtDateFrom.Text = Format(gdtDateTo, "mm/dd/yyyy")
         End If  
    Next ctlWork

End Sub

<----- Code End ----->

In your SQL statements you can now use the globally stored variables.

strSQL = "SELECT * FROM MyTable " _
  & " WHERE MyDateField >= " & Format(gdtDateFrom, "mm/dd/yy") _
  & " AND MyDateField <= " & Format(gdtDateTo, "mm/dd/yy")


0
 

Expert Comment

by:KEE
Comment Utility

If many forms in the project are accessing the same variable, on my way, I will  use a file to save the value temparily.

Because it can be easier to handle all the events acting on the global value rather than defining a global variable that is accessing by a lot of forms. You can only set a constant for the file location only.
0
 

Author Comment

by:tam97
Comment Utility
Hi ckhlam

Your coding worked first time, so please accept the points for the question.


wsh2

Thanks for your excellent coding I'm going to have a good look at it ASAP, I'll send out a dummy question to you as you deserve points for your effort.

Thanks to everyone for your efforts.

Rgds Tam97
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now