|
-
Nov 15th, 2005, 11:54 AM
#1
Thread Starter
Hyperactive Member
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:
Sub Example(Variable as string...) ' only allows 40 arguments here
Cheers
GTJ
-
Nov 15th, 2005, 11:55 AM
#2
Re: More arguments in a module sub routine??
-
Nov 15th, 2005, 12:33 PM
#3
Re: More arguments in a module sub routine??
-
Nov 15th, 2005, 12:44 PM
#4
Re: More arguments in a module sub routine??
Or a class.
VB Code:
' Class "code"
Option Explicit
Public mystr As String
Public myint As Integer
Public myetc As Double
' Form code
Option Explicit
Public Sub MySub(SomeData As Class1)
MsgBox SomeData.mystr
End Sub
Private Sub Form_Load()
Dim c As New Class1
c.myetc = 123.45
c.myint = 66
c.mystr = "test"
MySub c
End Sub
-
Nov 15th, 2005, 12:58 PM
#5
Re: More arguments in a module sub routine??
Use PARAMARRAY
VB Code:
Sub MySub(ParamArray args())
For i = 0 To UBound(args)
Debug.Print args(i)
Next
End Sub
Private Sub Command1_Click()
MySub "abc", "def", "ghi", "123"
End Sub
Pradeep
-
Nov 15th, 2005, 07:10 PM
#6
Thread Starter
Hyperactive Member
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??
-
Nov 15th, 2005, 07:13 PM
#7
Re: More arguments in a module sub routine??
yes you would, you do something like:
VB Code:
Dim i as long
For i = 0 to ubound(MyParamArr)
If MyParamArr(i) <> vbnullstring then msgbox "AGDAG"
Next i
-
Nov 15th, 2005, 07:14 PM
#8
Re: More arguments in a module sub routine??
I'm not certain what you mean by "empty" but you could certainly look for a default value in either that paramarray or class that would indicate "empty".
-
Nov 15th, 2005, 07:23 PM
#9
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.
-
Nov 15th, 2005, 07:39 PM
#10
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.
-
Nov 15th, 2005, 08:04 PM
#11
Re: More arguments in a module sub routine??
Unfortunately I don't think you can pass a UDT to a sub.
-
Nov 15th, 2005, 08:06 PM
#12
Re: More arguments in a module sub routine??
VB Code:
Option Explicit
Private Type MyType
x As String
End Type
Private Sub Form_Load()
Dim y As MyType
y.x = "yyy"
ASub y
End Sub
Public Sub ASub(z As MyType) 'Compile error here
End Sub
VB Code:
Option Explicit
Private Type MyType
x As String
End Type
Private Sub Form_Load()
Dim y As MyType
y.x = "yyy"
ASub y 'Compile error here
End Sub
Public Sub ASub(z)
End Sub
-
Nov 15th, 2005, 08:17 PM
#13
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:
'This works just fine
Private Type MyType
i As Integer
s As String
End Type
Private Sub MySub(arg As MyType)
Debug.Print arg.i, arg.s
End Sub
Private Sub Command1_Click()
Dim t As MyType
t.i = 1
t.s = "Hello world"
MySub t
End Sub
-
Nov 15th, 2005, 08:21 PM
#14
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
-
Nov 15th, 2005, 08:22 PM
#15
Re: More arguments in a module sub routine??
That will only work if both the Type and the sub are in a module and I guess there is no reason he can't do it that way.
-
Nov 15th, 2005, 08:23 PM
#16
Re: More arguments in a module sub routine??
 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.
-
Nov 15th, 2005, 08:27 PM
#17
Re: More arguments in a module sub routine??
Sorry, I take that back. You are both right.
-
Nov 15th, 2005, 08:27 PM
#18
Re: More arguments in a module sub routine??
 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:
Option Explicit
Private Type Point3D
X As Single
Y As Single
Z As Single
End Type
Private Sub Test(P As Point3D)
P.X = 10
P.Y = 20
P.Z = 30
End Sub
Private Sub Form_Activate()
Dim Vertex As Point3D
Test Vertex
Print Vertex.X, Vertex.Y, Vertex.Z
End Sub
-
Nov 15th, 2005, 08:30 PM
#19
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:
'in a module:
Public Type MyType
i As Integer
s As String
End Type
VB Code:
'in a Form
Public Sub MySub(arg As MyType)
Debug.Print arg.i, arg.s
End Sub
Private Sub Command1_Click()
Dim t As MyType
t.i = 1
t.s = "Hello world"
MySub t
End Sub
Private Sub's can also accept Public UDT's, but only a Private Sub can accept a Private UDT.
-
Nov 15th, 2005, 08:42 PM
#20
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:
Option Explicit
Private Type MyType
i As Integer
s As String
End Type
Public Sub MySub(arg As MyType) 'Compile error here
Debug.Print arg.i, arg.s
End Sub
Private Sub Command1_Click()
Dim t As MyType
t.i = 1
t.s = "Hello World"
MySub t
End Sub
'Private Type MyType
' i As Integer
' s As String
'End Type
'
'Private Sub MySub(arg As MyType)
' Debug.Print arg.i, arg.s
'End Sub
'
'Private Sub Command1_Click()
' Dim t As MyType
' t.i = 1
' t.s = "Hello world"
' MySub t
'End Sub
-
Nov 15th, 2005, 08:47 PM
#21
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..?
-
Nov 15th, 2005, 08:52 PM
#22
Re: More arguments in a module sub routine??
 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).
-
Nov 15th, 2005, 08:56 PM
#23
Re: More arguments in a module sub routine??
 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.
-
Nov 15th, 2005, 08:57 PM
#24
Re: More arguments in a module sub routine??
 Originally Posted by |2eM!x
Code:
Public Sub MySub(arg As MyType) 'Compile error here
Debug.Print arg.i, arg.s
End Sub
Ah, I see said the blind man.
-
Nov 15th, 2005, 09:18 PM
#25
Member
Re: More arguments in a module sub routine??
 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
-
Nov 16th, 2005, 10:26 AM
#26
Thread Starter
Hyperactive Member
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...
-
Nov 16th, 2005, 10:33 AM
#27
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.
-
Nov 16th, 2005, 10:34 AM
#28
Thread Starter
Hyperactive Member
Re: More arguments in a module sub routine??
whats the maximum number of arguments allowed for a class module???
-
Nov 16th, 2005, 10:35 AM
#29
Re: More arguments in a module sub routine??
 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.
-
Nov 16th, 2005, 10:39 AM
#30
Re: More arguments in a module sub routine??
 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.
-
Nov 16th, 2005, 10:40 AM
#31
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"
-
Nov 16th, 2005, 10:41 AM
#32
Thread Starter
Hyperactive Member
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:
if not textbox = "" then
if not searchvariable = "" then
searchvariable = "(blahblahblah
else
searchvariable = searchvariable & " AND (blahvbblahblahuiohysd
end if
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...
-
Nov 16th, 2005, 10:42 AM
#33
Thread Starter
Hyperactive Member
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...
-
Nov 16th, 2005, 10:55 AM
#34
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:
Private Function WHERE() as String
Dim ctl As Control
Dim SQL As String
For Each ctl In Me.Controls
If Left(ctl.Tag, 2) = "F_" Then
If ctl.Text <> "" Then
Select Case ctl.Tag
Case "F_T" 'Text field
SQL = SQL & ctl.Name & " Like '" & ctl.Text & "' AND "
Case "F_N" 'Number
SQL = SQL & ctl.Name & " = " & ctl.Text & " AND "
Case "F_D" 'Date
SQL = SQL & ctl.Name & " = #" & ctl.Text & "# AND "
End Select
End If
End If
Next
WHERE = "WHERE " & Left(SQL, Len(SQL) - 5)
End Function
Private Sub Command1_Click()
MsgBox "SELECT * FROM TABLE " & WHERE
End Sub
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Nov 16th, 2005, 10:58 AM
#35
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"
-
Nov 16th, 2005, 11:01 AM
#36
Thread Starter
Hyperactive Member
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??
-
Nov 16th, 2005, 11:02 AM
#37
Thread Starter
Hyperactive Member
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
-
Nov 16th, 2005, 11:04 AM
#38
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|