Results 1 to 29 of 29

Thread: would like to use the same code for a textbox in different forms ! possible ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    france
    Posts
    71

    Arrow would like to use the same code for a textbox in different forms ! possible ?

    like i said i would like to use the same code for different the same textbox in different forms

    because i don't like to paste code, it make changes really longer

    is it possible how can i do

    another thing, can i call some procedure and use the value of my textbox during this procedure, actually i have to save them in a string
    Last edited by writelearner; Sep 23rd, 2004 at 05:32 AM.

  2. #2
    New Member
    Join Date
    Oct 2003
    Location
    germany
    Posts
    10
    That' s all possible.
    1. You can add a module where you put procedures into and call them from your form code. So you can avoid pasting code in all the forms. Just add a procedure with your code and when you make changes just correct this procedure.
    2. You can do with your TextBox values whatever you want.
    Alex.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    france
    Posts
    71
    problem is that textbox values are lost when you quit the private sub of the form and i don't know how to declare them as public

    in fact it is why i cannot use the same code for different form i always lose the link with the objects on the form

    maybe it is because i use the show modal = false or maybe it has nothing to look with it

  4. #4
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657
    Add a module to you project, and then declare your Variables in it like this:

    PUBLIC MyVar as string

    This will now be available to the whole project even when a form closes !

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    france
    Posts
    71
    ok this is for the variant but what i need is to set the textbox.visibility property and some others

    and like i said before i don't want to past my code too much because it is very heavy to handle

    so what i want want is to make a public textbox, not a public variant

    thanks anyway

  6. #6
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657
    Fogot to add, obviously you will need to set you Variable equal to your textbox value before you quit you form.

    e.g. - myvar = txtbox1

    This is the easiest way of passing Values between forms, but for better performance & memory usage you should use - Public Properties.

    As in

    Public Property Get

    Public Propert Let

  7. #7
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657
    oops posted that second post before your last one appeared !


    You can also have a Public function that can change all Textboxes

    as in

    VB Code:
    1. Public sub ChangeTextbox(txt as textbox)
    2.  
    3. txt.visibility = True
    4.  
    5. e.t.c
    6.  
    7.  
    8. end sub

    Then all you have to do is call the sub from anywhere in the program!

    ChangeTextbox (mytextbox)

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    france
    Posts
    71
    okay lets get in deep

    typeunite is a combobox
    longueur, largeur and epaisseur are textbox
    labels are labels
    here is the sub i want to externelizze

    and when i try to play it it says missing objects

    VB Code:
    1. Sub longlargepvisibility()
    2.         If typeunite = "M3" Then
    3.             longueur.Visible = True
    4.             largeur.Visible = True
    5.             epaisseur.Visible = True
    6.             Label4.Visible = True
    7.             Label5.Visible = True
    8.             Label6.Visible = True
    9.             pertes.Value = 1.35
    10.         End If
    11.         If typeunite = "M2" Then
    12.             longueur.Visible = True
    13.             largeur.Visible = True
    14.             Label4.Visible = True
    15.             Label5.Visible = True
    16.             pertes.Value = 1.2
    17.         End If
    18.         If typeunite = "ML" Then
    19.             longueur.Visible = True
    20.             Label4.Visible = True
    21.             pertes.Value = 1.2
    22.         End If
    23.         If typeunite <> "M3" And typeunite <> "M2" And typeunite <> "ML" Then
    24.             longueur.Visible = False
    25.             largeur.Visible = False
    26.             epaisseur.Visible = False
    27.             Label4.Visible = False
    28.             Label5.Visible = False
    29.             Label6.Visible = False
    30.             pertes.Value = 1
    31.         End If
    32. End Sub

  9. #9
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657
    You can't just put that code in a public module and expect it to work!

    The textboxes & Lables & Comboboxes MUST be passed in as arguments to the sub !

    Let me try to understand exactley what you are trying to do, are you saying that you have the same controls as in the sub on several forms, and therefore want to be able to use the same code to set all the controls ???

    If so then you need to do something like

    VB Code:
    1. Public Sub longlargepvisibility(txtlong as textbox,txtlarg as textbox,cbo1 as combobox lbl1 as label) 'e.t.c
    2.  
    3. 'Pass in all your controls to the sub and then set them
    4.  
    5. txtlong.visible = true
    6. cbo1.visible = true
    7.  
    8. 'e.t.c
    9.  
    10. end sub

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    france
    Posts
    71
    in fact all the forms are not shown at the same moment but i use the same type of information on many forms so i try to use the same control for the same information

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    france
    Posts
    71
    don't work the call method ask for an argument, never used this before so i'am a little locked here is the call property i use and the title of the sub

    VB Code:
    1. Call longlargepvisibility
    2.  
    3.  
    4. Sub longlargepvisibility(longueur As TextBox, largeur As TextBox, epaisseur As TextBox, combounite As ComboBox, Label4 As Label, Label5 As Label, Label6 As Label, pertes As TextBox)

  12. #12
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657
    When you call the sub :

    VB Code:
    1. Call longlargepvisibility

    you need to pass the txtboxes e.t.c into it, if you put on open bracket "(" after longlargepvisibility you will get a list of arguements that the sub expects.

    so it shold look something like

    VB Code:
    1. Call longlargepvisibility(longuer,larger, e.t.c )

    Now on every form you can just use the Call longlargepvisibility sub and pass in the controls !

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    france
    Posts
    71
    i tried that but it is not working
    it says incompatibility of type

    VB Code:
    1. Call longlargepvisibility(longueur, largeur, epaisseur, combounite, Label4, Label5, Label6, pertes)

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    france
    Posts
    71
    VB Code:
    1. Call longlargepvisibility(longueur As TextBox, largeur As TextBox, epaisseur As TextBox, combounite As ComboBox, Label4 As Label, Label5 As Label, Label6 As Label, pertes As TextBox)

    this tells me that syntax is wrong
    i think it is witing for some kind of value but don't know what

  15. #15
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657
    Hi again Writelearner, i don't think i am explaining myself very well so i have attached an example project for you to have a look at.

    Hope it helps !
    Attached Files Attached Files

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    france
    Posts
    71
    got troubles to open your files
    module is ok but forms cannot be imported in VBA i use office 2003

  17. #17
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657
    Sorry about that, wasn't thinking and made a VB app for you

    If you import the attached forms now they should work !
    Attached Files Attached Files

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    france
    Posts
    71
    sorry, but it not working it says incompatibility of type

    after all you typed the same code so we have the same result

    maybe it doesn't work in VBA

  19. #19
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657
    It Definatley Works, i created those forms in VBA, but i have just realised i have office 2002, which maybe a problem !

    Do the forms load properly ??

    Where do you get the error ?

  20. #20
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657
    Just so you know i have had that demo working in front of me, in VBA, so there is no doubt that it works. If you could tell me where you are getting this error in the code maybe i could help you more !!!

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    france
    Posts
    71
    the error occurs when i hit the call property

    i did import the module 2 forms and the frm form but there is two more files maybe it is in it

  22. #22
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657
    Ah, I see what i have sent you write learner is a demo, and was intended to give you an EXAMPLE of how To make a Public Sub to Change all txtboxes work !

    TO RUN THE DEMO

    what you need to do is open a brand new project, seperate from what you are doing and import the 3 forms & the module i have sent you into it.

    Then try running frmMenu, this demo should show you how you need to change your code to work correctly!

    USING THE MODULE IN YOUR PROJECT !!!

    If you want you can just use the Module1.bas that i sent you also, on its own. First add it YOUR project

    To call the sub inside it though you shold use the following syntax

    Module1.longlargepvisibility longueur, Largeur, epaisseur, ComboUnite, Label4, Label5, Label6, pertes

    don't bother with using 'Call', and don't use brackets !

    By setting bvisible equal to either TRUE or FALSE you should get different results.


    To get the module to work with your project you will need to remove my code which just makes the Control Visible or Invisible
    and put your own in.

    Also you will need to pass "typeunite" into the sub

    so put TypeUnite as the first Variable e.g :-

    Public Sub longlargepvisibility(typeunite as string, longuer as textbox e.t.c)


    and then call the sub like so

    Module1.longlargepvisibility typeunite,longueur, Largeur, epaisseur, ComboUnite, Label4, Label5, Label6, pertes

    Hope this makes sense if you don't understand post me back !

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    france
    Posts
    71
    like i said before your code is not working.

    i launch the frm form, and then click on any button to have an incompatibility of type (execution error 13)

    the wrong line is here

    VB Code:
    1. Module1.longlargepvisibility longueur, Largeur, epaisseur, ComboUnite, Label4, Label5, Label6, pertes

    so where is the secret

  24. #24
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657
    Well, i really don't understand why thats happening, I have the form in front of me and it is working Fine, unless Microsoft have made some changes in WORD 2003 to Types then i am stuck.

    BUT Trust me this does work in WORD 2002, as i have it in front of me now working!.

    Does anybody else out their have any idea of why this sub is working for me but not for writelearner ???

  25. #25

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    france
    Posts
    71
    i trust but i am working with excel this may be the big difference

  26. #26

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    france
    Posts
    71
    maybe you can try to send the excel file

  27. #27
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657
    Right, that makes sense

    i didn't know you where using Excel, i just tried importing all my forms into Excel, and now i have the same error as you !

    Give a bit and i will see if i can resolve the problem !

  28. #28

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    france
    Posts
    71
    why things have to be complecated

  29. #29
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657
    Right i believe i have solved the problem,

    apparently Excel has two lots of Type Library's for Textbox's, ComboBoxes and the like so when you are declaring a Textbox for instance you need to make sure you declare the right one.

    instead of

    Dim txtbox1 as textbox

    put

    dim txtbox1 as msforms.textbox

    in terms of the problem with the sub all i had to do was change:

    VB Code:
    1. Public Sub longlargepvisibility(longueur As TextBox, Largeur As TextBox, epaisseur As TextBox, ComboUnite As ComboBox, Label4 As Label, Label5 As Label, Label6 As Label, pertes As TextBox)

    to
    VB Code:
    1. Public Sub longlargepvisibility(longueur As MSForms.TextBox, Largeur As MSForms.TextBox, epaisseur As MSForms.TextBox, ComboUnite As MSForms.ComboBox, Label4 As MSForms.Label, Label5 As MSForms.Label, Label6 As MSForms.Label, pertes As MSForms.TextBox)


    Anyway i have altered my Module to take this into account. If you download this module and use it with the 3 forms i sent it should work now!
    Attached Files Attached Files
    Last edited by NeedSomeAnswers; Sep 23rd, 2004 at 08:48 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width