Array Handling

Hi,

Hi ,
  I just want to ask how to pass an array into a procedure:
 
Dim strText() As String
 
ReDim strText(FIELD_NUM)
For i = 0 To FIELD_NUM - 1
    strText(i) = Text(i).Text  ' Text(i).Text is the text box in the form
Next

loadOptionCatalogs (strText)   <------------Typ mismatched occurs

where

Sub loadOptionCatalogs(strText() As String)
....
....
End sub

Could anyone tell me the correct method and give some explanation ?

Thanks!
cyyamAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
JacamarConnect With a Mentor Commented:
Call loadOptionCatalogs(strText())

then

Private sub loadOptionCatalogs(strText() as String)

Then you can use it in the sub procedure.
0
 
GERTJANCommented:
I think it must be :

Sub loadOptionCatalogs(strText As String)


0
 
Éric MoreauSenior .Net ConsultantCommented:
use
call loadOptionCatalogs (strText)
or
loadOptionCatalogs strText
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
junkeiraCommented:
it should be:

Sub loadOptionCatalogs(strText As Variant)
0
 
QJohnsonCommented:
Only Jacamar's is correct.  The others will only handle single values - not an array variable.
0
 
supunrCommented:
your code worked fine in my machine.  

Private sub Command1_Click()
Dim i as long
Dim strText() As String

ReDim strText(FIELD_NUM)
For i = 0 To FIELD_NUM - 1
   strText(i) = Text(i).Text  ' Text(i).Text is the text box in the form
Next

loadOptionCatalogs  strText  ' <------------Typ mismatched occurs
End Sub


Sub loadOptionCatalogs(strText() As String)
....
....
End sub

actually get rid of the brackets around strText in the marked line....

Good Luck!
0
 
supunrCommented:
you only put brackets around a function call, not for a call to a subroutine (i.e. functions that returns nothing)

e.g.

retVal = FunctionName(parameter)

SubRoutName parameter
' or
call SubRoutName(parameter)
0
 
QJohnsonCommented:
It looks as if the overriding concern is the use of the brackets in the called routine's declaration.  Actually calling the routine can be done with or without brackets.

I was surprised to see this, but this code convinced me.  Just drop in in the code window of a new standard exe project:

Private Sub Form_Load()
   Dim astr(2) As String

   astr(0) = "a"
   astr(1) = "b"
   astr(2) = "c"
   MsgBox "Present order is:" & vbCrLf & _
       astr(0) & vbCrLf & astr(1) & vbCrLf & astr(2)
   
   Call SwapAB(astr)     '  <<---- key line of code here
   
   MsgBox "New order is: " & vbCrLf & _
       astr(0) & vbCrLf & astr(1) & vbCrLf & astr(2)
   
End Sub

Private Sub SwapAB(astr() As String)
   Dim stemp As String
   stemp = astr(0)
   astr(0) = astr(2)
   astr(2) = stemp
End Sub

you'll see that you can change that "key line of code" to either use astr() or astr and the code works the same.  I had always believed you had to use the parens in the call and in the routine declararation.  Always happy to learn that ancient suppositions are wrong.  (Maybe this was right back in VB3??? <g>)

QJ

0
 
cyyamAuthor Commented:
Thanks for all of your responds, and esp. QJohnson and Jacamar.

May I further a little bit in Array handling.
How can I assign an array to another array?

Dim arstr1 () as string
Dim arstr2() as string

Redim arstr2 ( n )
... do operation in arstr2

arstr1 = arstr2  <---- It does not work

How can I assign it without assigning the array elements one by one ?

Thanks!
0
 
QJohnsonCommented:
I pasted your code into VB6 and it worked fine as soon as I change 'n' to a number.  I used 3.  After the assignment statement executes, ubound(astr1) and ubound(astr2) are both 3.
0
 
CleanupPingCommented:
Hi cyyam,
This old question (QID 20552510) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.
0
 
GPrentice00Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

-->Split between QJohnson and Jacamar

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

GPrentice00
EE Cleanup Volunteer
0
 
GPrentice00Commented:
sorry - 20 pts, cant be split.

--> accept answer by Jacamar
0
All Courses

From novice to tech pro — start learning today.