Solved

MS-Word Visual Basic Array ?

Posted on 2000-03-13
14
174 Views
Last Modified: 2013-11-25
Keep this in mind as you read:  I do not have the full version of visual basic; I'm working with the MS-Word version.  

My question is this:  How can I bring an array's information from one form to another?  I've tried to declare it under DELCARATIONS but it still won't come through.

Here is a bit of the code I'm trying.

For fn = 1 To frmFatherNumberBar.scrlFatherNumber.Value
Selection.InsertAfter fathername(fn) + ";"
Next fn

frmfathernumberbar is a seperate form from the one I'm on, and scrlfathernumber.value is a scroll bar.
I need the current form to pull "fathername(1)", etc.. from the form frmFatherNumberBar.

Any ideas how to do this?

-rick detroit
0
Comment
Question by:saksafon2
14 Comments
 
LVL 4

Expert Comment

by:wesleystewart
Comment Utility
Are you sure the syntax is correct:

Selection.InsertAfter fathername(fn) + ";"

It might be:

Selection.InsertAfter fathername(fn) ";"

without the "+" operator.  I'll have to tinker with it a little bit and see.

Wes
0
 

Author Comment

by:saksafon2
Comment Utility
I've used the + ";" syntax a few times.  For this example we could even leave the + ";" out.
The problem it's having is just getting the array over.  Meh!  It's frustrating.

lol
rick
0
 
LVL 32

Expert Comment

by:bhess1
Comment Utility
First, when you declare fathername(), make it a PUBLIC variable, e.g.:

PUBLIC fathername() as String

Then, when you reference the variable, make sure that you reference the form as well:

Selection.InsertAfter frmFatherNumberBar.fathername(fn) + ";"

That should do it.
0
 
LVL 4

Expert Comment

by:wesleystewart
Comment Utility
If you put a stop on this line:

For fn = 1 To frmFatherNumberBar.scrlFatherNumber.Value

(click on te gray bar to the left of this line of code) then the code will stop before it executes that line.  When it does, hit ctrl+G and the immediate window will pop up.  When it does, paste this in and hit return:

? frmFatherNumberBar.scrlFatherNumber.Value

The value in your scroll bar should pop up.  If it doesn't, then we need to start somewhere else.

Wes
0
 

Author Comment

by:saksafon2
Comment Utility
Meh!  

Bhess1, the idea is perfect and it's exactly what I'm looking for except the computer is giving me two different errors:

First, when I "PUBLIC" the variable in general declerations, it states that "Constants, fixed-length strings, arrays, and DECLARE statements not allowed as public members of object modules".

I placed the declerations actually in the code and it compiled, but when I got to the 2nd form that needs to call it, it states that it still can't find the array from the other form.  Any ideas?

rick
0
 
LVL 4

Expert Comment

by:wesleystewart
Comment Utility
Can you move this code into a general module and get it out of the form module?  Then you can use all those neat words like constant, public, etc.

You might have to add some syntax to take care of object references(like your form and document) but I think that would be the best way.

Wes
0
 

Author Comment

by:saksafon2
Comment Utility
How do I do that and how would I be able to call it up from the form?

rick
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 4

Expert Comment

by:wesleystewart
Comment Utility
Absolutely.

From the Word IDE, click >>Insert>>Module and a new module window will pop up.  You can create a new subprocedure by typing Sub MyProc() and hitting Enter.

Then your code will go in MyProc.  Use a button on your form or another event (form close, etc) and call MyProc from within the event.

How are you calling the code now?  It shouldn't be much different.

Wes
0
 
LVL 15

Expert Comment

by:cquinn
Comment Utility
Refer to the array (which I assume is declared in frmfathernumberbar) using the explicit reference as below:
 
frmFatherNumberbar.fathername(fn)


P.S.

Use the ampersand (&) to concatenate strings - if you concatenate with the plus sign (+) and any of the things you are concatenating is null, the whole string ends up as null!

0
 

Author Comment

by:saksafon2
Comment Utility
It says "PUBLIC" is an invalid attribute inside a sub-procedure.
I made one and copied the info in and it says it doesn't want to work.

Go fig.  MEH!

rick
0
 

Author Comment

by:saksafon2
Comment Utility
Adjusted points to 60
0
 
LVL 4

Accepted Solution

by:
wesleystewart earned 60 total points
Comment Utility
public is invalid in a subprocedure.  It goes in the declarations section at the top of the module, NOT inside the procedure.

This makes it available to the whole project.

Wes
0
 

Author Comment

by:saksafon2
Comment Utility
The module idea was great.  I placed both sets of procedures that need the variables in a seperate module with different procedures and it's going to call up and work fine.  Thanks for the help again.
Your savin' my life here.

-rick, detroit
0
 
LVL 4

Expert Comment

by:wesleystewart
Comment Utility
I'm happy to help.  If you're going to be doing a lot of programming in Word, I suggest the following two books:

http://www.oreilly.com/catalog/writewordmacro/
http://www.oreilly.com/catalog/pwomodel/noframes.html

If you get them from Amazon I think they are about $20 each.  I don't work for O'Reilly or anything like that, I just think their books are the best no-nonsense reference texts on the market.  They manage to get as much good stuff into 500 or 600 pages as Sybex and the others get into 1200 to 1500 pages.

VBA, as it is implemented in Word, is the exact same language engine (VBA332.dll) found in VB, so programming in word is a great step towards progarmming in VB.

Wes
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now