Author Topic: Passing Variables  (Read 2222 times)

0 Members and 1 Guest are viewing this topic.

krampaul82

  • Guest
Passing Variables
« on: November 29, 2007, 03:55:24 PM »
Hello
I am fairly new at VBA and I am having troubble passing Variables I want the value obtained in gtc_proj module (see Attached) to the other modules that will require this Job # Pre-fix any help would be appreciated.
Sincerely,  Mark Paulus

Guest

  • Guest
Re: Passing Variables
« Reply #1 on: November 29, 2007, 04:05:16 PM »
Which variable(s)??

The one thing that jumped out at me was this (from the module called 'Module1_new_gtc_proj').

Quote
Sub gtc_2007_proj_start()

'declare the variables....
   
  Public gtcprojnum As String

That should be declared OUTSIDE of the sub.  In other words, just put it at the top of the module.  Or, another idea (this is what I do) is to put all of your PUBLICly declared variables in a separate module.

krampaul82

  • Guest
Re: Passing Variables
« Reply #2 on: November 29, 2007, 04:47:43 PM »
That is not used... The variable i want is from the input box in module1

Atook

  • Swamp Rat
  • Posts: 1026
  • AKA Tim
Re: Passing Variables
« Reply #3 on: November 29, 2007, 05:10:49 PM »
Krampaul, to expound on Matts post, you can great a module and fill it with global variables, which are seen from any module. I usually put these in a module called mod_Globals and declare them with something like

    Public gbDebug As Boolean
    Public gstrInputBox as String

Then I can set and read any of these variables from anywhere in the project. I haven't looked at  your code, but also note that if you hide a form without unloading it, you can still access the values in the form from any module by using frmInput.Textbox4.Value. or something like that.

JohnK

  • Administrator
  • Seagull
  • Posts: 10547
Re: Passing Variables
« Reply #4 on: November 29, 2007, 05:28:48 PM »
I dont fully understand your question either but...
Code: [Select]
Sub main()
  Dim myStrVar As String
    myStrVar = InputBox("Enter something")
    MsgBox "You entered: " + myStrVar
End Sub
doesnt work for you?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

krampaul82

  • Guest
Re: Passing Variables
« Reply #5 on: November 30, 2007, 03:45:53 PM »
Yes, It does but now, I want that value to be passed to other modules as a pre-fix number for some files that will be moved upon the creation of a new project

deegeecees

  • Guest
Re: Passing Variables
« Reply #6 on: November 30, 2007, 03:58:10 PM »
Code: [Select]
Option Explicit
Public [color=red]*your var here*[/color] As String

...is what I use, also...

Code: [Select]
Const DATABASE_DIR = "a string"

krampaul82

  • Guest
Re: Passing Variables
« Reply #7 on: November 30, 2007, 04:02:13 PM »
Krampaul, to expound on Matts post, you can great a module and fill it with global variables, which are seen from any module. I usually put these in a module called mod_Globals and declare them with something like

    Public gbDebug As Boolean
    Public gstrInputBox as String

Then I can set and read any of these variables from anywhere in the project. I haven't looked at  your code, but also note that if you hide a form without unloading it, you can still access the values in the form from any module by using frmInput.Textbox4.Value. or something like that.

This Worked... Thank you so much...

ML

  • Guest
Re: Passing Variables
« Reply #8 on: November 30, 2007, 04:15:21 PM »

This is a little off topic but I do totally understand why people create a module for all "Public" variables so that can be used throughout the entire project but I tend to shy away from that. This is only a personal preference.
At best, I will put all variables being used by a particular "module" at the top in the declarations section; however, I sometimes declare my variables abovethe code as I am writing the code in order to keep the variables necessary for that portion of code with the code.
I just like to be able to see the variables right in front of me as I am doing the programming. In this case, those variables are only necessary for a small section in the program.

Of course, if you need to declare like 30, 40 or whatever variables that are being used across a project then you definitely need to do something global.

I guess there is really no write way, I just hate having to go looking for what variable I need when I need it.

In the case of a userform related project, then I guess that a public variable module really does make a lot of sense.

Mark