Call Subroutine as variable (Access 2003)

W

WC Justice

I would like to be able to call a particular subroutine based on recordset
data.

For example, if rs.fields("FormID") = 29, I would like to be able to call
PreviewDocument29 by doing something like this:

strSubName = "PreviewDocument" & rs.fields("FormID")
call strSubName

I am currently doing it the crude way:

if rs.fields("FormID") = 1 then
call PreviewDocument1
elseif rs.fields("FormID") = 2 then
call PreviewDocument2
....
end if

If strSubName needs to be an object of some sort, I don't know what it needs
to be. Any help would be appreciated.
 
J

Jezebel

You can use CallByName(), although even the MS developers warn that's a
horribly slow method.
 
W

WC Justice

Speed is not enough of an issue to keep me from using it to address my issue.
However, from looking into the CallByName function, I don't see how to use
it to call a subroutine. I also failed to mention that the subroutine
contains an argument.
 
T

Tony Jollans

I wouldn't recommend this in this circumstance (what is wrong with a simple
Select Case statement?) but one relatively simple way to do it is ...

In ThisDocument put ...

Sub PreviewDocument1(Argument)
' Do whatever
End Sub


Sub PreviewDocument2(Argument)
' Do whatever
End Sub

' etc.

Wherever you want it, code

Callbyname ThisDocument, "PreviewDocument" & rs.fields("FormID"),
vbMethod, Parameter
 
C

Cindy M.

Hi =?Utf-8?B?V0MgSnVzdGljZQ==?=,
would like to be able to call a particular subroutine based on recordset
data.

For example, if rs.fields("FormID") = 29, I would like to be able to call
PreviewDocument29 by doing something like this:

strSubName = "PreviewDocument" & rs.fields("FormID")
call strSubName
You might try Application.Run strSubName

(BTW, this group is specific for Microsoft WORD, not Access.)

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top