Results 1 to 38 of 38

Thread: More arguments in a module sub routine??

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Location
    England
    Posts
    283

    Angry More arguments in a module sub routine??

    in defining one of my routines in a module i need to define 41 arguments, however it only allows 40, is there a way to get around this, or change this number or something!!!????

    eg.

    VB Code:
    1. Sub Example(Variable as string...) ' only allows 40 arguments here

    Cheers

    GTJ

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: More arguments in a module sub routine??

    I don't believe so, no.

  3. #3

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: More arguments in a module sub routine??

    Or a class.

    VB Code:
    1. ' Class "code"
    2. Option Explicit
    3.  
    4. Public mystr As String
    5. Public myint As Integer
    6. Public myetc As Double
    7.  
    8. ' Form code
    9.  
    10. Option Explicit
    11. Public Sub MySub(SomeData As Class1)
    12.     MsgBox SomeData.mystr
    13. End Sub
    14.  
    15. Private Sub Form_Load()
    16. Dim c As New Class1
    17. c.myetc = 123.45
    18. c.myint = 66
    19. c.mystr = "test"
    20.  
    21. MySub c
    22. End Sub

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: More arguments in a module sub routine??

    Use PARAMARRAY

    VB Code:
    1. Sub MySub(ParamArray args())
    2.     For i = 0 To UBound(args)
    3.         Debug.Print args(i)
    4.     Next
    5. End Sub
    6.  
    7. Private Sub Command1_Click()
    8.     MySub "abc", "def", "ghi", "123"
    9. End Sub

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Location
    England
    Posts
    283

    Re: More arguments in a module sub routine??

    i need to be able to check to see if each argument is empty or not and be able to run code off each seperate argument, i wouldn't be able to do this with paramarray would i??

  7. #7
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: More arguments in a module sub routine??

    yes you would, you do something like:
    VB Code:
    1. Dim i as long
    2. For i = 0 to ubound(MyParamArr)
    3. If MyParamArr(i) <> vbnullstring then msgbox "AGDAG"
    4. Next i

  8. #8

  9. #9
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: More arguments in a module sub routine??

    Yeah I was gonna suggest ParamArray, but you beat me to it. It supports 1000's if not millions of arguments. Or what memory will allow I think.

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: More arguments in a module sub routine??

    Could you explain why in goods name you want to pass so many values? It might be some other solution if we could understand what you're trying to do. In other cases ParamArray could work (the limit for that is memory and we're talking stack memory now so you may reach that limit earlier then you think depending on the current stack size and the type of arguments you want to pass). Another option that already have been mentioned is to use a class, but in your case maybe it might be better to pass a UDT. That's a very common practice in C for example.

  11. #11

  12. #12
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: More arguments in a module sub routine??

    VB Code:
    1. Option Explicit
    2. Private Type MyType
    3.     x As String
    4. End Type
    5.  
    6. Private Sub Form_Load()
    7. Dim y As MyType
    8. y.x = "yyy"
    9.  
    10. ASub y
    11. End Sub
    12.  
    13. Public Sub ASub(z As MyType) 'Compile error here
    14.  
    15. End Sub

    VB Code:
    1. Option Explicit
    2. Private Type MyType
    3.     x As String
    4. End Type
    5.  
    6. Private Sub Form_Load()
    7. Dim y As MyType
    8. y.x = "yyy"
    9.  
    10. ASub y 'Compile error here
    11. End Sub
    12.  
    13. Public Sub ASub(z)
    14.  
    15. End Sub

  13. #13
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: More arguments in a module sub routine??

    Actually Marty, you can pass a UDT to a sub however there are some rules to follow. If the type is private, which it must be if you declare it in a Form or in a private class module (a class module is private in a standard exe project and can optionally be so in other project types) then the Sub must be private. However if the UDT is public (declared in a regular module or in a public class) the sub can be either public or private.

    It actually makes sense if you think about it. A Public Sub can be reach from any part of your project and if it requires a UDT, the UDT must also be publicly available or otherwise you will not be able to pass the correct values to it in some cases.
    VB Code:
    1. 'This works just fine
    2. Private Type MyType
    3.     i As Integer
    4.     s As String
    5. End Type
    6.  
    7. Private Sub MySub(arg As MyType)
    8.     Debug.Print arg.i, arg.s
    9. End Sub
    10.  
    11. Private Sub Command1_Click()
    12.     Dim t As MyType
    13.     t.i = 1
    14.     t.s = "Hello world"
    15.     MySub t
    16. End Sub

  14. #14
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: More arguments in a module sub routine??

    UDT's can be used in subs and functions, MartinLiss. I do it all the time in my 3D engines I make in DirectX. As JA pointed out, there are rules

  15. #15

  16. #16
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: More arguments in a module sub routine??

    Quote Originally Posted by Jacob Roman
    UDT's can be used in subs and functions, MartinLiss. I do it all the time in my 3D engines I make in DirectX. As JA pointed out, there are rules
    I don't believe you can do it in a form. I'm talking about passing the UDT.

  17. #17

  18. #18
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: More arguments in a module sub routine??

    Quote Originally Posted by MartinLiss
    I don't believe you can do it in a form. I'm talking about passing the UDT.
    I know you are talking about that. And I dunno what's wrong with your example, but mine worked like a charm:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Type Point3D
    4.  
    5.     X As Single
    6.     Y As Single
    7.     Z As Single
    8.  
    9. End Type
    10.  
    11. Private Sub Test(P As Point3D)
    12.  
    13.     P.X = 10
    14.     P.Y = 20
    15.     P.Z = 30
    16.  
    17. End Sub
    18.  
    19. Private Sub Form_Activate()
    20.  
    21.     Dim Vertex As Point3D
    22.    
    23.     Test Vertex
    24.    
    25.     Print Vertex.X, Vertex.Y, Vertex.Z
    26.  
    27. End Sub

  19. #19
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: More arguments in a module sub routine??

    @Marty: As I mentioned above, if the Sub is declared as Public the type must also be public and in a Standard EXE that means you must declare it in a regular module. But that doesn't mean the Sub must that takes the UDT as an argument must reside in the same module. The Public Sub can be in a Form if you like. This works fine:
    VB Code:
    1. 'in a module:
    2. Public Type MyType
    3.     i As Integer
    4.     s As String
    5. End Type
    VB Code:
    1. 'in a Form
    2. Public Sub MySub(arg As MyType)
    3.     Debug.Print arg.i, arg.s
    4. End Sub
    5.  
    6. Private Sub Command1_Click()
    7.     Dim t As MyType
    8.     t.i = 1
    9.     t.s = "Hello world"
    10.     MySub t
    11. End Sub
    Private Sub's can also accept Public UDT's, but only a Private Sub can accept a Private UDT.

  20. #20
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: More arguments in a module sub routine??

    I apologize for hijacking this thread but this is really interesting. When I copy the following code into a new form and try to run it, a compile error results. However when I reverse the commenting, it works. The code looks identical. What am I missing???

    VB Code:
    1. Option Explicit
    2. Private Type MyType
    3.     i As Integer
    4.     s As String
    5. End Type
    6.  
    7. Public Sub MySub(arg As MyType) 'Compile error here
    8.     Debug.Print arg.i, arg.s
    9. End Sub
    10.  
    11. Private Sub Command1_Click()
    12. Dim t As MyType
    13. t.i = 1
    14. t.s = "Hello World"
    15. MySub t
    16. End Sub
    17. 'Private Type MyType
    18. '    i As Integer
    19. '    s As String
    20. 'End Type
    21. '
    22. 'Private Sub MySub(arg As MyType)
    23. '    Debug.Print arg.i, arg.s
    24. 'End Sub
    25. '
    26. 'Private Sub Command1_Click()
    27. '    Dim t As MyType
    28. '    t.i = 1
    29. '    t.s = "Hello world"
    30. '    MySub t
    31. 'End Sub

  21. #21
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: More arguments in a module sub routine??

    Code:
    Public Sub MySub(arg As MyType) 'Compile error here
        Debug.Print arg.i, arg.s
    End Sub
    edit*

    im confused as to what the question is now..?

  22. #22
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: More arguments in a module sub routine??

    Quote Originally Posted by MartinLiss
    Sorry, I take that back. You are both right.
    Sorry, I missed that reply

    But while we are on the subject I will take some time to expand on this just a little bit further if I may.

    In a component, like an ActiveX DLL project for example, you can declare Public UDT's inside a class which Instancing property is set to either PublicNotCreatable or MultiUse (in an ActiveX EXE it's also allowed in a GlobalSingleUse and GlobalMultiUse class). The only place it's not allowed is in a class which Instancing property is set to Private (which all Form objects always are and also all classes in a Standard EXE project).

    Now, as a VB developer you will probably see this UDT as a member of the particular class you declare it in but that is actually not the case. A Public UDT declared in a class module is not a member of that class but rather apart of the type library for that component. After you have compiled your component and you have set a reference to it in another project the UDT is instantly available to you. You do not need to create an object of the particular class you declared it in before you can use it.

    So any class inside your component can now accept this UDT as an argument and a function can even return such an UDT. You can also use it inside your own procedures inside the Standard EXE project without creating any objects, you only need to set the reference to the component library.

    This fact may also cause problems, since if any methods in your component relies on getting arguments of this UDT type how would you be able to do so if you're using late binding? Using late bindings means you haven't set any reference to the component.

    The simple answer is that you can't. This is because a late bound object doesn't load the type library of the component, it only creates the object.

    I make a clear distinction here between a component and an object. An object is an instance of a class that may or may not reside inside a component.

    So that is why you shouldn't use public UDT's inside components. But regarding the question in this thread I have no idea what he's trying to do or where this code reside or even the scope of the code. So that's why I asked for a little better explanation while at the same time I also throw out the UDT as one possible solution (because in a Standard EXE project it doesn't really matters).

  23. #23
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: More arguments in a module sub routine??

    Quote Originally Posted by MartinLiss
    I apologize for hijacking this thread but this is really interesting. When I copy the following code into a new form and try to run it, a compile error results. However when I reverse the commenting, it works. The code looks identical. What am I missing???
    That a Public Sub can not accept a Private UDT. Please read this once more.

  24. #24

  25. #25
    Member
    Join Date
    Jul 2005
    Posts
    45

    Re: More arguments in a module sub routine??

    Quote Originally Posted by Joacim Andersson
    But while we are on the subject I will take some time to expand on this just a little bit further if I may.

    In a component, like an ActiveX DLL .......
    Hello. I've been using this forum on a regular basis since I became a user a while ago. I don't have a huge post count since I mainly use the search feature and have almost always found what I was looking for. While doing so I've seen several of simular long replies from you Jocaim Anderson that are more like short articles, and I just wanted to say that I'm very impressed by your in-depth knowledge and your ability to explain complicated issue in an easy to understand and very interesting manner. I only wished my teacher had had the same abilities .

    Keep up the good work

  26. #26

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Location
    England
    Posts
    283

    Re: More arguments in a module sub routine??

    Could you explain why in goods name you want to pass so many values?
    I am making a module that takes search values from textboxes/checkboxes and creates the search string and runs the search.

    I need so many values to give the user the option to search from the value as a number, a string, a number with wildcard or a string with wildcard.

    I also need the data control name(s) to run the search on, and the grid to display the reuslts.... etc..... etc...

    so you see that i need a lot more than 40...

  27. #27
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: More arguments in a module sub routine??

    That sounds like a job for a class module instead of one function/sub in a module.

  28. #28

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Location
    England
    Posts
    283

    Re: More arguments in a module sub routine??

    whats the maximum number of arguments allowed for a class module???

  29. #29
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: More arguments in a module sub routine??

    Quote Originally Posted by greythej
    I am making a module that takes search values from textboxes/checkboxes and creates the search string and runs the search.

    I need so many values to give the user the option to search from the value as a number, a string, a number with wildcard or a string with wildcard.

    I also need the data control name(s) to run the search on, and the grid to display the reuslts.... etc..... etc...

    so you see that i need a lot more than 40...
    You don't need to pass the names of textboxes or combo boxes or checkboxs or anything else to a sub to build an SQL SELECT clause. You simply build it based on what, if anything is in the textbox, or if the checkbox is checked, or if anything is selected from the combo.

    There is no reason to write a sub/function with that many parameters.

  30. #30
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: More arguments in a module sub routine??

    Quote Originally Posted by greythej
    whats the maximum number of arguments allowed for a class module???
    I wasn't talking about arguments here. You can build properties that you set before you call the method that does the job.

    On the other hand, if you want to specify if the search is numberic, text, with or without wildcards and so on still doesn't need more then two arguments. What to search for and a value that tells you how to do the search.

  31. #31
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: More arguments in a module sub routine??

    search..? are u creating a SELECT QUERY by testing all the controls etc and building the where clause depending on whats in each?

    I have a way to do this without the need for any arguments
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  32. #32

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Location
    England
    Posts
    283

    Re: More arguments in a module sub routine??

    yer but it takes tons of time to write the code if you have lots of fields:

    eg.

    VB Code:
    1. if not textbox = "" then
    2. if not searchvariable = "" then
    3. searchvariable = "(blahblahblah
    4. else
    5. searchvariable = searchvariable & " AND (blahvbblahblahuiohysd
    6. end if
    7. end if

    wrting that tons of times really pisses me off!!!! so i thought i would make it easier for me to write a module (class module you have now suggested) to do it...

  33. #33

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Location
    England
    Posts
    283

    Re: More arguments in a module sub routine??

    ah send to me static PLEASE.

    will give my email address if you don't wish to post it on this forum...

  34. #34
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: More arguments in a module sub routine??

    happy to post the code / explanation of how
    ok.. name each of your textboxes the exact name of the corresponding field in your database...
    in the TAG of each one mark it for Text / Num / date etc..
    In my example I use "F_T", "F_N", "F_D"
    The F_ is just to mark the control as being part of the Filter...
    the T,N,D is for Text,Number,Date

    now you'll need to play with this im sure but here is the basics to get u started
    VB Code:
    1. Private Function WHERE() as String
    2. Dim ctl As Control
    3. Dim SQL As String
    4. For Each ctl In Me.Controls
    5.     If Left(ctl.Tag, 2) = "F_" Then
    6.         If ctl.Text <> "" Then
    7.         Select Case ctl.Tag
    8.             Case "F_T" 'Text field
    9.                 SQL = SQL & ctl.Name & " Like '" & ctl.Text & "' AND "
    10.             Case "F_N" 'Number
    11.                 SQL = SQL & ctl.Name & " = " & ctl.Text & " AND "
    12.             Case "F_D" 'Date
    13.                 SQL = SQL & ctl.Name & " = #" & ctl.Text & "# AND "
    14.         End Select
    15.         End If
    16.     End If
    17. Next
    18. WHERE = "WHERE " & Left(SQL, Len(SQL) - 5)
    19. End Function
    20.  
    21. Private Sub Command1_Click()
    22.     MsgBox "SELECT * FROM TABLE " & WHERE
    23. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  35. #35
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: More arguments in a module sub routine??

    one more thing:

    ANY text field will allow wildcards.. since I used LIKE
    if there are no wildcards used.. it will act like an =

    let me know if u understand it
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  36. #36

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Location
    England
    Posts
    283

    Re: More arguments in a module sub routine??

    thats quality static, you define ctl as control, would that not try and create the string for other controls such as labels pictures etc..??

    would there be a way to make it only textboxes it looped through??

  37. #37

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Location
    England
    Posts
    283

    Re: More arguments in a module sub routine??

    o wait the fact you have only filled the tag property for textboxes gets round that..

    sorry...

    GTJ

  38. #38
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: More arguments in a module sub routine??

    yes

    you could test if its a textbox...
    If TypeOf ctl is Textbox Then

    but using the tag make sure that if u are using OTHER textboxes.. it wont mess with them and add them to the WHERE
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

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