Consulting

Results 1 to 20 of 20

Thread: Sleeper: Userform transfer variables?

  1. #1
    VBAX Regular
    Joined
    Aug 2005
    Posts
    56
    Location

    Sleeper: Userform transfer variables?

    hey ppl,
    this seems like a silly qestion to me but i cant find out what the problem is....
    i open up a userform through a macro. when all the information is entered and submit is hit, i want all the info in the text boxes to be transfered to variables that the rest of the macro can use and manipulate...
    how do ya do that?

    i have this in the code of the submit button, it doesnt work:
     
    If (txtstate = "") Then
       MsgBox "You didn't enter a state, Try Again."
       Exit Sub
    End If
    state = txtstate.Text
    the if statement is cool, but the variable transfer isn't.
    thnx fer any help.
    Last edited by Aussiebear; 03-31-2023 at 06:29 AM. Reason: Adjusted the code tags

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hi there,

    Where exactly are you trying to transfer this variable to? Can you zip/upload a sample file?

  3. #3
    VBAX Regular
    Joined
    Aug 2005
    Posts
    56
    Location
    eh, its a bit of a pain to get to send only part of the file becuase it contains alot of bussines "passwords" so im gonna have to start deleting stuff. heres the jist of the code:

    the form has lets say one text field with the name: "shmuck"
    and the form name is: "bfform"
    i call the useform through a macro:

    'Check if the bookfair already has a School/org name and info
        Worksheets("Sheet1").Select
        yesno = Range("M2").Value
    'if yesno = 1 then the information is already in
        If (yesno = 0) Then
    'Load Form for bookFair Information
        Load bfform
        bfform.Show

    Now... the text field has been filled out. i want to call it with the current macro, and, for example:

    MsgBox (shmuck)
    if i were to do that right after the form is closed/hidden, it would return an empty msgbox becuase that variable doesnt have any information...
    am i making sense?

  4. #4
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    268
    Location
    Your current problem is that the textbox does not exist once you unload the form.

    The easiest way to code it (there are several other possibilities) would be to declare a Public variable in the parent module, and set that variable to the textbox contents before unloading the form.

  5. #5
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Or call the routine before the form is unloaded, passing the variable through to the next routine. BUT, I agree with BC here, use a Public variable.

  6. #6
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    BlueCactus,

    I read what you wrote: "there are several possibilities".

    A part of declaring the variable as public or writing its value in an object (range of a sheet, another textbox,etc.), can you tell me which are the other possibilities, please?

    Thank you very much

  7. #7
    VBAX Regular
    Joined
    Aug 2005
    Posts
    56
    Location
    ahhh, didnt think that it would clear the variables if the form was unloaded... very cool thanks!

  8. #8
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    firefytr, other methods?

  9. #9
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Yes, using a blank worksheet and storing variables in this location is a definite possibility. Especially when using add-ins. The sheets are not visible, but you can still have as many sheets as you want in them, and they can store data as any other worksheet and be queried in the same fashion (although only via code).

    There are public variables, static variables, passing through routines, storing in the windows registry, etc. Many ways to skin the cat here.

  10. #10
    VBAX Regular
    Joined
    Aug 2005
    Posts
    56
    Location
    ehm, nope, doesnt work.

    i declared "shmuck" as public string.
    in the code of the submit button on the form i have:

     shmuck = txtshmuck.Text
    and it gives me an error.
    or do i need to call "txtshmuck" as public variable and use THAT in the macro?
    Last edited by Aussiebear; 03-31-2023 at 06:56 AM. Reason: Adjusted the code tags

  11. #11
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    Thank you very much!

  12. #12
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    268
    Location
    Quote Originally Posted by ALe
    BlueCactus,

    I read what you wrote: "there are several possibilities".

    A part of declaring the variable as public or writing its value in an object (range of a sheet, another textbox,etc.), can you tell me which are the other possibilities, please?

    Thank you very much
    Other possibilities are (trying to be brief):
    i) like you mention, writing it out to a sheet
    ii) writing in a non-Private Sub into the userform code which you call after Load Userform and before Userform.Show. You call that to pass a variable to the form: e.g., Call MyForm.PassingVars(myvar1, myvar2) etc... Then at some point you set these variables to the control values, and finally....
    ii-a) If the variable were passed ByRef (default method), their parent variables in the calling module are already set to what you want, or
    ii-b) before unloading the form, call a Sub in the parent module with the variable values
    iii) Don't pass any variables to the userform. Just pass the control values back to the parent module with a Call ParentModule.PassingVars(TextBox1.Text, Listbox1.ListIndex) etc before unloading the form.

  13. #13
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    268
    Location
    Quote Originally Posted by russkie
    ehm, nope, doesnt work.

    i declared "shmuck" as public string.
    in the code of the submit button on the form i have:

     shmuck = txtshmuck.Text
    and it gives me an error.
    or do i need to call "txtshmuck" as public variable and use THAT in the macro?
    You want something like:
    Public schmuck as String
    Sub testSchmuck()
      schmuckForm.Show
      MsgBox schmuck
    End Sub
    Exit code in UserForm:
    schmuck = txtschmuck.Text
    Unload Me
    You could also set schmuck in txtschmuck_Click() or txtschmuck_Change() rather than at the end.
    Last edited by Aussiebear; 03-31-2023 at 06:57 AM. Reason: Adjusted the code tags

  14. #14
    VBAX Regular
    Joined
    Aug 2005
    Posts
    56
    Location
    ... muchos graciuos amingo compadre`!... no really, thanks

  15. #15
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    The Public variables need to be declared in a Standard Module, btw.

  16. #16
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Rather than using public variables, why not just hide the form rather than unload it? All values in the form remain available to you, and you leave the unload until the process is completed.

         MsgBox bfform.shmuck
    Last edited by Aussiebear; 03-31-2023 at 06:57 AM. Reason: Adjusted the code tags

  17. #17
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Zack,
    I've never saved information in xla files. Is the data stored available to any workbook which incorporates that xla?
    Regards
    Malcolm
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  18. #18
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    MD: Yes. As long as that xla file is installed in the current session, it can be accessed anywhere via VBA. The only exception is if the xla file is password protected.

  19. #19
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Thanks Zack,
    Could be useful.
    Regards
    Malcolm
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  20. #20
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    I find it quite useful, I use it daily.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •