Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Global Variables

Posted on 2000-03-14
7
Medium Priority
?
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 

Accepted Solution

by:
ckhlam earned 400 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

670 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