|
-
Jul 5th, 2000, 11:35 PM
#1
Thread Starter
Fanatic Member
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
-
Jul 6th, 2000, 12:14 AM
#2
Addicted Member
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:
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...
-
Jul 6th, 2000, 12:35 AM
#3
Hyperactive Member
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
-
Jul 6th, 2000, 12:50 AM
#4
Thread Starter
Fanatic Member
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
-
Jul 6th, 2000, 06:00 PM
#5
Hyperactive Member
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
-
Jul 6th, 2000, 07:07 PM
#6
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
-
Jul 6th, 2000, 07:38 PM
#7
Frenzied Member
Couldn't you just also declare a global variable?
?
-
Jul 7th, 2000, 02:04 PM
#8
Thread Starter
Fanatic Member
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
-
Jul 7th, 2000, 06:11 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|