Solved

Global Variables

Posted on 2000-03-14
7
288 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
ID: 2618237
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
ID: 2618288
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
ID: 2618312
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:tam97
ID: 2618329
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
ID: 2618724
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
ID: 2619285

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
ID: 2621750
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

839 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