Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a Variable

Status
Not open for further replies.

smartrider

Programmer
Apr 18, 2003
21
US
Hello ,

I have two modules in two different Templates and I am trying to get a value from one template to another. Can anyone plz please tell me what am I doing wrong.

I have the code here :

In templare 1 Module the code is :
Public Sub LoadAuthor()

frmAuthorDB.cmdAdd.Visible = False
frmAuthorDB.cmdDelete.Visible = False
frmAuthorDB.cmdEdit.Visible = False
frmAuthorDB.cmdOk.Visible = False
frmAuthorDB.cmdSelect.Visible = True
DisableChkBoxes
frmAuthorDB.Show
fillfrom = fillfromname
Unload frmAuthorDB

End Sub

in Template Two Module :

Public Sub cmdNameAuthor_Click()
Application.Run "LoadAuthor"

FillFrom = txtFromName.Text
End Sub


I am trying to get the value of FillFrom from Template one to Template 2


CAN ANYONE HELP ME OUT HERE - PLZ

THANKS
 
A little more information might be of help.

What is the application? Excel? Access? Word? ???

Why are you using "Application.Run" instead of just invoking the other sub?

What does "LoadAuthor" do?

What do you mean by "Template"?

In any case, it probably doesn't make sense to have
Code:
  fillfrom = fillfromname
in one sub and
Code:
  FillFrom = txtFromName.Text
in the other. If you declare "fillfrom" as a global (public) variable, then the contents would just get overwritten. As it is, each use of the variable is local to the sub where it is used and only has value within that sub.

The cleanest way to pass data from one sub to another is with parameters. Here is a simple demo:
[blue]
Code:
Option Explicit

Sub Primary()
Dim sAuthorName As String
  GetAuthorName sAuthorName
  MsgBox "Author: " & sAuthorName
End Sub

Sub GetAuthorName(AuthorName As String)
  AuthorName = "Dickens, Charles"
End Sub
[/color]


An alternate, messier way is to use a global variable. (But that should be a last resort.):
Code:
Option Explicit
[green]
Code:
' Demo using a global string variable (not recommended)
[/color]
Code:
Public gsAuthorName As String

Sub Primary()
  GetAuthorName
  MsgBox "Author: " & gsAuthorName
End Sub

Sub GetAuthorName()
  gsAuthorName = "Dickens, Charles"
End Sub

 
This application is in Word.

There are two different Templates so I used Add-in option to join them.

LoadAuthor - invokes the application located in Template1 so that text from that form will be pasted in Template Two.

I tried you suggestion but go "Sub not defined Error"

Any other suggestions - please ?
 
Afraid Word is not my strong suit (or I would have recognized the use of the term "template").

I don't have an answer for you. Hopefully, someone with experience with Word macros can jump in here and help out.

If you don't get any response in the next day or so, start a new thread. Make it clear what you are trying to do (i.e. communicate betwen two separate documents) and you should have more luck.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top