Results 1 to 9 of 9

Thread: public variables

  1. #1

    Thread Starter
    Fanatic Member mikeycorn's Avatar
    Join Date
    Jun 2000
    Location
    Aliso Viejo, CA, USA
    Posts
    526
    This isn't seeming to be enough to get this variable from one form to the next. Is there anything else I need to do?

    Public Correct As Integer


    I'm using option explict because it seems that most of the good downloads have that as the first line. I don't know if that would affect it. I tried restating the Public Correct in the final form after not having it stated, but each time, Correct = 0.


    mikeycorn

  2. #2
    Addicted Member
    Join Date
    Jan 2000
    Location
    Sydney, Australia
    Posts
    196
    Where did you declare the public variable. If you want 2 forms to be able to get it's contents you are best to declare it in a module - not in the code of a form.

    Also - apologies if this sounds extremely obvious but you say your variable always has 0 in it. Variables (integers anyway) will initialise to 0, so are you actually assigning anything to the variable at any stage - like:

    Code:
    Correct = 42
    No, option explicit will not make any difference - it's good that you are using it. All option explicit does is force you to declare your variables.

    like your

    Code:
    Public Correct As Integer
    that is an explicit variable declaration. If you don't use option explicit then you do not have to declare your variables (with public / private / static / dim etc) - which may sound better but it is much worse because you can end up inadvertantly creating new variables byt making typing mistakes.

    Hope this helps...

  3. #3
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Declare it in the module

    like funkyd77 said.

    If you don't put Option Explicit at the start of every single module and form code, you will run the risk of having code that runs one day and doesn't the next because you've used a variable in a module and it's value is overriding your form value (for example)

    If you want to pass a variable from one form to the next, not relying on a module decalaration, then you might want to keep your existing declaration (make sure it is in every form) and when "passing" it around, you would have to remember that every form has it's own copy of the value.

    Which suits you best is over to you and what you intend to do. This example might demonstrate the difference for you.

    Create three forms called Form1, Form2, Form3
    On each form place two buttons called Command1, Command2
    Paste the following code into the appropriate form's code window

    Code:
    ' this is form1
    Option Explicit
    Public myDate As Date
    
    Private Sub Command1_Click()
      Dim myForm As New Form2
      myForm.myDate = Now
      myForm.Show
    End Sub
    
    Private Sub Command2_Click()
      MsgBox myDate
    End Sub
    
    ' this is form2
    Option Explicit
    Public myDate As Date
    
    Private Sub Command1_Click()
      Dim myForm As New Form3
      myForm.myDate = Now
      myForm.Show
    End Sub
    
    Private Sub Command2_Click()
      MsgBox myDate
    End Sub
    
    ' this is form3
    Option Explicit
    Public myDate As Date
    
    Private Sub Command1_Click()
      Dim myForm As New Form1
      myForm.myDate = Now
      myForm.Show
    End Sub
    
    Private Sub Command2_Click()
      MsgBox myDate
    End Sub
    Note that this example will create a new form and display it, initialising the new form's myDate public variable to the current time.

    The following alternative demonstrates the use of the public global variable myDate.

    Just add a module and paste the following code

    Code:
    Option Explicit
    Public myDate As Date
    Then, add a third button to each form called Command3 and paste the following code into each form's code window

    Code:
    Private Sub Command3_Click()
      MsgBox "local myDate = " & myDate & Chr(10) & "global myDate = " & Module1.myDate
      ' another syntax is Me.myDate for local myDate
    End Sub
    You would be best to avoid naming local and global variables the same thing. It can make troubleshooting a problem.

    I hope this helps

    Paul Lewis

  4. #4

    Thread Starter
    Fanatic Member mikeycorn's Avatar
    Join Date
    Jun 2000
    Location
    Aliso Viejo, CA, USA
    Posts
    526

    Smile thanks very much

    I am simply amazed at how generous both of you are (as well as quite a few others on this site) in your willingness to take some of your own time to help educate a newbie! Both your replies have shed some light and so thanks again.


    With plenty to chew on,

    mikeycorn

  5. #5
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Talking no worries

    I only respond because when I first logged on with problems of my own to have solved, I got good and fast answers. Although I cannot vouch for how good myu answers are , I feel it's only fair to put back into the system what I got out of it...

    Plus lately I'm "putting in" a bit more so I have some credit

    Cheers

    Paul Lewis

  6. #6
    Guest

    Talking A Module will do the job :)

    Try stating the variable in the exact same way in a Module.
    Declaring it public on a form makes it public only on the specific form.

    Hope this help

  7. #7
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800

    Thumbs up

    Couldn't you just also declare a global variable?
    Code:
    Global Correct
    ?

  8. #8

    Thread Starter
    Fanatic Member mikeycorn's Avatar
    Join Date
    Jun 2000
    Location
    Aliso Viejo, CA, USA
    Posts
    526

    that reply begs another question

    Well, if you could do it with Global as well as with Public then what would be the difference between the two approaches?

    mikeycorn

  9. #9
    Guest
    The use of Global is obsolete. It's only added for backwards compatibility.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width