By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,101 Members | 1,387 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,101 IT Pros & Developers. It's quick & easy.

Problem in getting public variable data

P: 6
Hi,

I am having problem to retrieve the data value of a public variable. Below is my code:

Form1
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Public selected_entry_id As String
  3.  
  4. Private Sub btnEdit_Click()
  5.   selected_entry_id = DataGrid1.Columns(0)
  6.   Form2.Show
  7. End Sub
  8.  
  9. Private Sub DataGrid1_Click()
  10.   selected_entry_id = DataGrid1.Columns(0)
  11. End Sub

Form2
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.  
  3.   If selected_entry_id = "" Then
  4.     MsgBox "Empty"
  5.   Else
  6.    MsgBox "Value exists"
  7.  End If
  8.  
  9. lblselectedid.Caption = selected_entry_id
  10.  
  11. End Sub
I keep reveceiving empty value for the public variable (selected_entry_id).

May i know what is the mistake from my code above?
Mar 31 '07 #1
Share this Question
Share on Google+
6 Replies


iburyak
Expert 100+
P: 1,017
Try this line instead:


[PHP]lblselectedid.Caption = Form1.selected_entry_id[/PHP]


Good Luck.
Apr 1 '07 #2

devonknows
100+
P: 137
Try this line instead:


[PHP]lblselectedid.Caption = Form1.selected_entry_id[/PHP]


Good Luck.
That or make the actual variable global, for example put the variable into the a Module (.bas) that way the variable is there when the Application is launched and is created in runtime, were as for example if you unloaded form1 and loaded form2 you would have lost any data in that variable, until you reloaded form1 and put data back into it, but thats only if you are trying to pass data between a lot of forms, it can make things easier than trying to go Form1.Label1.Caption for about 10 different captions.

But hope thats a bit more insight
Kind Regards
Devon.
Apr 1 '07 #3

Expert 5K+
P: 8,434
A variable declared as Public within a Form only exists within that form (as you would have seen if you had specified Option Explicit in Form2). You can access it by specifically referencing the form, as iburyak has shown.

If you want global variables that you can use anywhere in your project, declare them as Public in a code module.

I always recommend the use of Option Explicit, as it prevents exactly this sort of problem by allowing you to use only those variables you have explicitly created. In this case, since you referenced a non-existent variable selected_entry_id in Form2, VB helpfully created it for you. But of course, it didn't contain anything.
Apr 1 '07 #4

P: 6
A variable declared as Public within a Form only exists within that form (as you would have seen if you had specified Option Explicit in Form2). You can access it by specifically referencing the form, as iburyak has shown.

If you want global variables that you can use anywhere in your project, declare them as Public in a code module.

I always recommend the use of Option Explicit, as it prevents exactly this sort of problem by allowing you to use only those variables you have explicitly created. In this case, since you referenced a non-existent variable selected_entry_id in Form2, VB helpfully created it for you. But of course, it didn't contain anything.

Hi,
How to declare the variable in a code module?
Apr 2 '07 #5

Expert 5K+
P: 8,434
How to declare the variable in a code module?
Create a code module, and put your Public statement in it.

If you don't know how to create a code module, you need to read the documentation.
Apr 2 '07 #6

vijaydiwakar
100+
P: 579
Hi,

I am having problem to retrieve the data value of a public variable. Below is my code:

Form1
======
Option Explicit
Public selected_entry_id As String

Private Sub btnEdit_Click()
selected_entry_id = DataGrid1.Columns(0)
Form2.Show
End Sub

Private Sub DataGrid1_Click()
selected_entry_id = DataGrid1.Columns(0)
End Sub


Form2
=====
Private Sub Form_Load()

If selected_entry_id = "" Then
MsgBox "Empty"
Else
MsgBox "Value exists"
End If

lblselectedid.Caption = selected_entry_id

End Sub

************************************************** ********************************

I keep reveceiving empty value for the public variable (selected_entry_id).

May i know what is the mistake from my code above?
pls explain ur flow of execution
Apr 2 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.