Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Global Variables in VB

  • 20-03-2005 7:02pm
    #1
    Registered Users, Registered Users 2 Posts: 542 ✭✭✭


    Please help me!!

    Right, i have a form where i get an employee number for the user. i then search my employee database for the number and when i find it i load a new form and display all the employees details.

    i have a global variable set in the first form and i set it to equal what ever employee number is inputted by the user.

    but how do i transfer this over to the second form??

    any help is much appreciated!


Comments

  • Registered Users, Registered Users 2 Posts: 4,276 ✭✭✭damnyanks


    formName.variableName

    I think is what you use in VB to access variables from other forms.


  • Registered Users, Registered Users 2 Posts: 542 ✭✭✭Hoochiemama


    hmm, ok. heres what i did in the second form...

    Dim fnum1 As String


    Private Sub Form_Load()
    fnum1 = TD_EmpSearch.Fnum
    Print fnum1
    End Sub

    TD_EmpSearch is the name of the first form.

    although it recognised the syntax you gave me... (cheers for that, btw)
    its still not printing anything on the form


  • Registered Users, Registered Users 2 Posts: 542 ✭✭✭Hoochiemama


    sorry, i realised my error.

    i put a button on the second form and set it to print the variable on a click and that worked!! thanks a million for your help


  • Registered Users, Registered Users 2 Posts: 541 ✭✭✭Vorrtexx


    it's probably best to pass the value over to the 2nd form, rather than the 2nd form directly referencing the variable in the first form.

    To pass the employee number to the 2nd form maybe something like this in Form2:

    Property Let EmployeeNumber(NewValue As String)
    fnum1 = NewValue
    End Property


    so when you go to open the 2nd form, set the property and then show it
    the form_load

    Form2.EmployeeNumber = TD_EmpSearch.Fnum
    Form2.Show


    Then in Form2's Load:

    Private Sub Form_Load()
    Print fnum1
    End Sub


  • Registered Users, Registered Users 2 Posts: 1,345 ✭✭✭Squall


    You could also use a module to store all the variables.

    Just create a module class and declare all the variables there. You can reference them form anywhere in the project then. Possibly not as efficient but it causes fewer headaches.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    i have a global variable
    ...

    but how do i transfer this...

    While it looks like you've got your problem sorted, I'd suggest that you either need to rethink your terminology, or that you're confused about something.

    Global variables, by definition, are globally accessible within an application. There is no notion of transferring them to somewhere else, but rather of accessing them from somewhere else.

    I know this might seem pedantic in the extreme, but it has been my experience that sloppy terminology is the root of so many problems in programming.

    The more precisely you can describe a problem, the easier it is (generally speaking) to resolve.

    jc


Advertisement