Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 7th, 2007, 10:18 AM
ftm ftm is offline
Registered User
 
Join Date: Nov 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Passing an array from module to form

I have been struggling with a problem passing a small array to be used in a form and I hope that somebody can help me. I cannot get the array into the form.

In the main module code I have an array containing information on a book reference:

Code:
Private Function PresentForm1(ByRef iID() As String, sBookRef() As String) As Boolean
Dim iCounter As Integer
For iCounter = LBound(iID) To UBound(iID)
    Form1.ListBox1.AddItem iID(iCounter)
Next Form1.ListBox1.Selected(0) = True
For iCounter = 0 To 2
MsgBox sBookRef(iCounter) ' This bit works fine
Next iCounter
Form1.Show
End Function
I need to use the sBookRef() array in a form that is called from the function so that a caption on the form changes depending on the item selected in the list box. Something along the lines of:

Code:
Private Sub ListBox1_Click() 
Label1.Caption = sBookRef(ListBox1.ListIndex)
End Sub
When I run the code I get a subscript out of range error.
I added the extra bit of code with the MsgBox to show that there are 3 valid values in the sBookRef() array within the lifetime of the original function. The problem I am having is in passing that array into the form so I can use it there.
I tried listing the array at the top of the module But it didn't seem to work either:

Code:
Public sBookRed() As String
Can anybody please help ?:(

 
Old March 7th, 2007, 03:54 PM
Friend of Wrox
 
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Is the label declaration being called PRIOR to the initialization of the array? That would cause this error.

Also If the code gets called before an item is selected in the listbox, the listbox will return a -1 value and that would also cause this error.

I suggest a slight modification to the line that sets the caption:
If ListBox1.ListIndex < 0 Then Label1.Caption = "<No Selection>" Else Label1.Caption=sBookRef(ListBox1.ListIndex)

It would also be prudent to make sure that ListItem isn't greater than the array.

Hope this helps.

 
Old March 8th, 2007, 07:39 AM
ftm ftm is offline
Registered User
 
Join Date: Nov 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Many thanks for replying. I shall try out your suggestions.







Similar Threads
Thread Thread Starter Forum Replies Last Post
passing Multidimensional Array Values back to form stitch Classic ASP Databases 2 June 21st, 2013 01:59 PM
Addressing form from module MKri VB.NET 2002/2003 Basics 18 December 23rd, 2005 10:22 AM
Array & Collections, Option Private Module ramk_1978 Beginning VB 6 1 January 19th, 2005 01:06 PM
Passing php array values to javascript array gkrishna Pro PHP 0 November 6th, 2004 03:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.