473,320 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Problem in getting public variable data

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
6 1729
iburyak
1,017 Expert 512MB
Try this line instead:


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


Good Luck.
Apr 1 '07 #2
devonknows
137 100+
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
Killer42
8,435 Expert 8TB
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
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
Killer42
8,435 Expert 8TB
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
579 512MB
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

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

Similar topics

2
by: Vinay Aggarwal | last post by:
I have been thinking about the lazy initialization and double checked locking problem. This problem is explain in detail here http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html...
6
by: Stephen Cook | last post by:
Having worked through the problems around enabling the document function using an XmlUrlResolver I started work on building a useful class to hide the intricacies. Trying to generalise the process...
3
by: Mark | last post by:
Hi all, i'm trying to serialize a class. Using the constructor of XmlSerializer i get these (odd?) errors: "File or assembly name goseij9w.dll, or one of its dependencies, was not found"....
1
by: Rhy Mednick | last post by:
I'm creating a custom control (inherited from UserControl) that is displayed by other controls on the form. I would like for the control to disappear when the user clicks outside my control the...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
3
by: Philip Tepedino | last post by:
I'm having an odd problem. My website's session state is getting shared between users! This problem only happens when a user tries to access the site from inside our corporate LAN. The user,...
7
by: garyusenet | last post by:
This is the first time i've worked with openfile dialog. I'm getting a couple of errors with my very basic code. Can someone point out the errors in what i've done please....
5
by: tshad | last post by:
In VS 2003, I am setting up an abstract class that is setting up classes for each datatype of VB.Net (as well as C#). I am trying to set it up so that most of the work is done in the Abstract...
7
by: edsunder | last post by:
I'm making a "wrapper" for an unmanaged DLL (written, I'm fairly certain in C). I have a c++ "wrapper" that I'm trying to convert to VB.net. I've got most of the program working, but I've hit a brick...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.