Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Global Variables

Posted on 2000-03-14
7
Medium Priority
?
295 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 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

885 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