Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing data 1

Status
Not open for further replies.

drewdaman

Programmer
Aug 5, 2003
302
0
0
CA
hello

i have yet another question! i am quite a vb newb!

i get some user input from a form. now if i have, say a module, where i implement what to do with the data, how can i pass the data from the "form code module" to the other module? i guess one way would be to use global vars in the other module.. but that kinda sucks!

i'm building a sort of a "wizard". so i have a bunch of forms with "next" buttons.. so eventually, i would have to get the data and pass it to another module.

thanks!
 
You may consider passing arguments (aka parameters) to the Sub.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
well.. yes and no..

i have to get a lot of data from the user. that's why i have the next, next, next thing...

on the final form, i would like to make some sort of a call to a sub that does soemthing. so, once i click on "next" i don't want to lose the data.. i want to pass all of the data i gathered to a sub, but i don't want to call the sub until all of the forms have been filled by teh user.

thanks.
 
Why not storing your data in an hidden sheet ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
well.. because i didn't know you could do that! lol..

how do you do that?

thanks!
 
The simplest way (in my opinion) is to play with named ranges and the ControlSource property of most UserForm's controls.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
hey,

could you please expand on this?

thanks a lot!
 
I don't yet know how to do this on a userform, but why not use one userform with several tabs? Just like the Microsoft Wizards, would not be possible to make each of your current userforms into a separate tab on a userform (in a tabstrip or multipage), and then use a Next button to cycle through them. It might be a bit difficult, but then again, it might solve your problems with passing data.

Just a thought,
Joseph
 
Yeah, I really should read these things before I post...

The line should have been Just like the Microsoft Wizards, would it not be possible to make each of your current userforms into a separate tab on a userform (in a tabstrip or multipage), and then use a Next button to cycle through them?

Sorry,
Joseph
 
Here's another approach you may want to try.

Create a User Defined Type (Public) in your code module. Example:
Code:
Public Type UserInfoType
LastName As String
FirstName As String
Department As String
Salary As Single
YearsOfService As Integer
End Type


In your code module place the procedure to act on this data:
Code:
Sub DoSomethingWithData(byVal UserData As UserInfoType)
  'Code to store, manipulate, etc. data passed in UserData
End Sub

In your Userform class module, declare a module-level variable:
Code:
Dim UserInput As UserInfoType

When the user clicks OK to complete the Userform (wizard), populate this variable with the applicable info then pass it to the procedure:
Code:
With UserInput
  .LastName = txtLastName.Text
  .FirstName = txtFirstName.Text
  .Department = txtDept.Text
  .Salary = CSng(txtSalary.Text)
  .YearsOfService = CInt(txtService.Text)
End With

DoSomethingWithData UserInput


Regards,
Mike
 
Personally, I cannot see what the issue is with using Global variables.......

Rgds, Geoff

Three things are certain. Death, taxes and lost data. DPlank is to blame

Please read FAQ222-2244 before you ask a question
 
thanks for all the replies guys..

xlbo.... using global vars (especially the number that i would need) is considered terrible programming practice.

amccoder.. thanks for the idea, but i don't know how to make tabs and stuff. plus the boss wants it this way!

mike, your solution seems really really good! i didnt' know you could make a user defined type in vba. i'm a c++ person who has been asked to do a small project in vba.. but this idea really appeals to me.. since i'm used to classes and structs and whatnot! i will try this otu and get back to you!

thanks a lot for all the replies!
 
drewdaman said:
using global vars (especially the number that i would need) is considered terrible programming practice

Why ?

Rgds, Geoff

Three things are certain. Death, taxes and lost data. DPlank is to blame

Please read FAQ222-2244 before you ask a question
 
I have to second Geoff on this .... what's wrong with Global vars??????

Cheers, Glenn.

Did you hear about the literalist show-jumper? He broke his nose jumping against the clock.
 
well.. its considered bad in Object oriented programming, which i'm used to.. i'm not sure if its considered bad in vba. glob vars do not lend themselves to modularization and OO principles in general. but since it is considered bad in OO, and that is what i do most of the time, i dont' want my boss to see the code and yell at me ! :p
 
Mike's idea is a good one though ;-) Not trying to downgrade that - just asking whether you can back up that statement about global vars with any facts ?

Rgds, Geoff

Three things are certain. Death, taxes and lost data. DPlank is to blame

Please read FAQ222-2244 before you ask a question
 
Aren't Global vars named Class (or Object) in OO ? ;-)
 
LOL - nice timing !!

Thanks for the explanation but I believe you are wrong in this case.

Global variables are used commonly to transfer variables between subs - only difference between them and normal variables is that they are declared at the top of the module and are available to all subs rather than just 1.

Rgds, Geoff

Three things are certain. Death, taxes and lost data. DPlank is to blame

Please read FAQ222-2244 before you ask a question
 
hehe.. well.. we could carry on this discussion for months!

the bottom line is that you avoid glob vars whenever you can!

phv, no.. they are not. you create an instance of a class (much like you create an instance of a user defined type in vba). the class is only accessible within a certain scope- where it is declared.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top