View Full Version : Visual Basic, anyone?
Raven
08-01-2005, 02:49 PM
I've done most of my programming in C++, but lately I've been working on a project in VB 6. I ordered a reference book, but it hasn't arrived yet. In the mean time, I've been googling everything, and have had some trouble finding answers to my questions. So maybe someone here is experience enough to help me out.
My latest question is with accessing variables between forms.
Let's say I have Form1 and Form2. Form1 has a button which opens Form2. When Form2 is loaded, variable x (from Form1) will need to be used on Form2. Since Form_Load() won't take any variables, how can I pass x on to be used in Form2?
For example, let's say I have numPeople on Form1. Then a label on Form2 would read the value of numPeople from Form1. I've tried the following, but it doesn't work. Label1.Caption = Form1.numPeople
Any suggestions?
Try the Public statement. Not 100% on it, but I think it will do that.
gottimd
08-01-2005, 02:59 PM
Ask that question here. (http://www.mrexcel.com/board2/)
Castlerock
08-01-2005, 02:59 PM
Private MyVar As String
Private Sub Command1_Click()
Dim f2 As Form2
Set f2 = New Form2
f2.Label1.Caption = MyVar
f2.Show
End Sub
Private Sub Form_Load()
MyVar = "Hello"
End Sub
Or, instead of Label1, you could have some public variable on Form2.
sabotai
08-01-2005, 03:01 PM
Who did you piss off to be put on a Visual Basic project? ;)
Raven
08-01-2005, 03:09 PM
Who did you piss off to be put on a Visual Basic project? ;)
Actually, it's a personal project. I am working on a baseball sim. A lofty goal, but something I've been wanting to do for a couple years now.
I started writing it in C++, but laying out the GUI in OpenGL was a little too time consuming.
We'll see how it goes.
PackerFanatic
08-01-2005, 03:23 PM
I have used little VB (will be taking a class this next semester) but I know that in C#, I used delegates to pass variables back and forth between forms. Maybe do some research on that and see if it is doable in VB.
Mr. Wednesday
08-01-2005, 03:44 PM
I've done most of my programming in C++, but lately I've been working on a project in VB 6. I ordered a reference book, but it hasn't arrived yet. In the mean time, I've been googling everything, and have had some trouble finding answers to my questions. So maybe someone here is experience enough to help me out.
My latest question is with accessing variables between forms.
Let's say I have Form1 and Form2. Form1 has a button which opens Form2. When Form2 is loaded, variable x (from Form1) will need to be used on Form2. Since Form_Load() won't take any variables, how can I pass x on to be used in Form2?
For example, let's say I have numPeople on Form1. Then a label on Form2 would read the value of numPeople from Form1. I've tried the following, but it doesn't work. Label1.Caption = Form1.numPeople
Any suggestions? Any control on a form is accessible as a public variable.
You can add public variables, properties, or methods to a form to provide additional avenues for access.
If you want to get advanced, you can have a form Implement an interface, and then you can access properties or methods of that interface via an object reference.
What happens when you try to read Form1.numPeople? Are you running into problems with order of initialization? Are you initializing Form1.numPeople in the right place?
Mr. Wednesday
08-01-2005, 03:44 PM
Dola, any reference to Form1 should automatically load it.
Greyroofoo
08-01-2005, 03:57 PM
I haven't played with VB much recently so the syntax is fuzzy in my head.
I know you should be able to just reference the variable on Form A directly from Form B.
Or you can just use a module (I think that what its called) to make a global variable than can be used by everything.
Mr. Wednesday
08-01-2005, 03:58 PM
Be very careful about using a global variable in a module. That way lies bad coding practice and difficult-to-maintain code. I'm not saying there's never a place for it (I've done it myself), but you should be very careful about doing it.
Castlerock
08-01-2005, 04:08 PM
Actually, it's a personal project. I am working on a baseball sim. A lofty goal, but something I've been wanting to do for a couple years now.
I started writing it in C++, but laying out the GUI in OpenGL was a little too time consuming.
We'll see how it goes.
If VB6 is not a requirement, then I'd take a look at C#. It is way more powerful then VB6 and GUI layout is even easier.
Buccaneer
08-01-2005, 07:21 PM
Dim f2 As Form2
Set f2 = New Form2
Yes, just declare a New Form within the scope of where it's being loaded. Don't use global declarations for something like this.
Mr. Wednesday
08-01-2005, 07:26 PM
Keep in mind -- that syntax will declare a new instance of the Form2 template. By default, you get a global variable named Form2 that also refers to an instance of it. If you don't need multiple instances, it will often be cleaner to simply use the default variable for it.
Logan
08-01-2005, 07:31 PM
reminded of CS111 from sophomore year
Raven
08-01-2005, 10:39 PM
What happens when you try to read Form1.numPeople? Are you running into problems with order of initialization? Are you initializing Form1.numPeople in the right place?
Initialization was fine, the problem was the variables weren't declared public. Now that I changed them all to public, I have no problem with accessing Form1.numPeople, or any variable from any form.
Thanks guys.
Mr. Wednesday
08-01-2005, 10:44 PM
Oh, I see. I was thinking you were talking about a control. :)
Neon_Chaos
08-01-2005, 10:45 PM
Initialization was fine, the problem was the variables weren't declared public. Now that I changed them all to public, I have no problem with accessing Form1.numPeople, or any variable from any form.
Thanks guys.As a VB programmer, I'm going to give you a tip...
Never declare anything in Forms. Use the Form as the User-Interface, not as the program holder.
Place all your variables, codes, subs, and functions inside Modules.
So, Let's say you have a program that uses Form 1 and Form 2. Create a module Module1.
Declare all your variables as public in Module 1, and place all your subs and functions (with their own private variables declared within the subs/functions... e.g. counters et al) in module 1, using the forms only to reference the code. this way, any form can access any variable, sub or function without you havign to pass it back and forth.
Castlerock
08-02-2005, 07:43 AM
As a VB programmer, I'm going to give you a tip...
Never declare anything in Forms. Use the Form as the User-Interface, not as the program holder.
Place all your variables, codes, subs, and functions inside Modules.
So, Let's say you have a program that uses Form 1 and Form 2. Create a module Module1.
Declare all your variables as public in Module 1, and place all your subs and functions (with their own private variables declared within the subs/functions... e.g. counters et al) in module 1, using the forms only to reference the code. this way, any form can access any variable, sub or function without you havign to pass it back and forth.
I have to agree with Mr.Wednesday on this one...
Be very careful about using a global variable in a module. That way lies bad coding practice and difficult-to-maintain code. I'm not saying there's never a place for it (I've done it myself), but you should be very careful about doing it.
Neon_Chaos
08-02-2005, 08:00 AM
I have to agree with Mr.Wednesday on this one...
Well, if you're just going to pass the variable around and change it between forms, might as well use a Global Variable to be accessed by forms.
Of course, if you have data that is deemed sensitive and should not be changed other than by certain subs/functions, you can just pass it using ByVal referrencing between Subs and Functions, and just use the declared variables within as placeholders.
I specifically have multiple modules. A Variable module where i declare all my global variables, a Function module and a Sub module. Within the Sub module, I have multiple subroutines for whatever processes I need (including assigning initial data to variable) Most of my subroutines use ByVal referrencing, so as not to 'accidentally' change the global variables, so do my functions. My forms serve as a GUI, calling the subs and functions as I see fit.
I specifically use Global Variables because I know what Variables I want to be Global and I know when I want to change them or not. Rather than declare them within forms and keep on passing data around, it tends to clutter my code a lot.
Raven
08-10-2005, 12:39 AM
Another question.
If I have a database called Universe. What's the syntax to insert in to that database?
Mr. Wednesday
08-10-2005, 12:52 AM
It depends on how you're accessing it (most likely, DAO vs. ADO, but also do you want to use SQL or object manipulation) and what the backend is (could be Access, MSDE/SQL Server, etc.).
Generally, you can accomplish it using an "INSERT INTO" SQL statement. The details of using that may vary with the backend.
Raven
08-10-2005, 01:01 AM
Using ADO and Access. I've taken a db class this semester, so I am a somewhat familiar with SQL. Once I figure out how to trigger the statements, I'll be able to write them. It's just the triggering that I need to figure out.
Mr. Wednesday
08-10-2005, 06:44 AM
How do you mean by "triggering" the statement?
Mr. Wednesday
08-10-2005, 06:46 AM
Dola, if by "trigger" you mean, how to go about executing it, you can use the Execute method of the Connection object.
Raven
08-10-2005, 01:26 PM
Well, what I meant was - I won't actually insert the row in to the table until something happens (until something triggers the insertion).
In this case, it won't be entered in to the table until you hit a button (sort of like a finalize button). Now inside the button_Click() event, I need to see the syntax for actual execution of the Insert Into statement.
Mr. Wednesday
08-10-2005, 01:34 PM
<connection object>.Execute "INSERT INTO <remainder of command>", <remaining parameters>
(I don't recall, offhand, what else there is for additional parameters beyond an optional argument to return the number of records affected.)
Raven
08-24-2005, 11:49 PM
If I have a menu that I want to use on several forms, what are some options I have to implement this without rewriting the same menu for every single form?
Mr. Wednesday
08-24-2005, 11:54 PM
In VB5/6, I think you would need to copy the menu structure to every form. You might be able to simplify it by copying the definition statements from the .frm file (which is plain text), but AFAIK that's the extent of what you can do.
Coder
08-25-2005, 03:10 AM
If I have a menu that I want to use on several forms, what are some options I have to implement this without rewriting the same menu for every single form?
I'd go with an MDI-app. Place the main menu in the MDI-parent and then make all your other screens MDI-children.
Graeme Kelly
08-25-2005, 01:02 PM
I'm pretty sure there should be a way of setting up the menus in code. You might have to create the menu headers (File, Edit etc) but you should be able to add meni items to them dynamically. It's been a few years (man, i'm getting old) since I messed with VB though.
I'a have to agree with Neon_Chaos on the little disagreement above - I'd implement such a thing (that is, a variable that multiple forms need access to) by having it inside a class and creating a global instance of that class.
vBulletin v3.6.0, Copyright ©2000-2026, Jelsoft Enterprises Ltd.