Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 7th, 2007, 10:18 AM
ftm ftm is offline
Registered User
 
Join Date: Nov 2006
Location: , , United Kingdom.
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 ?:(

Reply With Quote
  #2 (permalink)  
Old March 7th, 2007, 03:54 PM
Friend of Wrox
Points: 513, Level: 8
Points: 513, Level: 8 Points: 513, Level: 8 Points: 513, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2007
Location: Davenport, IA, USA.
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.

Reply With Quote
  #3 (permalink)  
Old March 8th, 2007, 07:39 AM
ftm ftm is offline
Registered User
 
Join Date: Nov 2006
Location: , , United Kingdom.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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


Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 01:44 AM.


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