PC Review


Reply
Thread Tools Rate Thread

Cannot pass argument from control to sub and back

 
 
Revolvr
Guest
Posts: n/a
 
      18th Sep 2008
I'm at a loss here...

I have code associated with controls on a worksheet, which call
subroutines in other modules, and I am passing an argument, a string,
indicating the success or failure of the operations in that
subroutine. Then the string gets appended to a TextBox. But I am not
able to pass the string argument.

In Sheet1 I have
Private Sub dothings_Click()
'Dim smsg As String

Call messagetest(smsg)
‘ Here if I check the value of smsg, it is empty.
NewMsg1 (smsg) ‘ writes to a text box

End Sub

The subroutine messagetest is in another module. Messagetest sets the
smsg string to a value, however when returning the value becomes an
empty string.

Sub messagetest(smsg As String)
smsg = "message test set this string"
End Sub

If this sub is in the same module as the sheet code, it works. I have
tried declaring smsg as public, but still doesn't work. I've tried
passign byval, still doesn't work.

Why is this and is it possible to pass arguments between code
associated with a worksheet, and modules?


Thanks!
 
Reply With Quote
 
 
 
 
Gary''s Student
Guest
Posts: n/a
 
      18th Sep 2008
Dim the string in a standard module above any subs for functions.

This will make the string a global variable that all subs can "see"
--
Gary''s Student - gsnu2007k


"Revolvr" wrote:

> I'm at a loss here...
>
> I have code associated with controls on a worksheet, which call
> subroutines in other modules, and I am passing an argument, a string,
> indicating the success or failure of the operations in that
> subroutine. Then the string gets appended to a TextBox. But I am not
> able to pass the string argument.
>
> In Sheet1 I have
> Private Sub dothings_Click()
> 'Dim smsg As String
>
> Call messagetest(smsg)
> ‘ Here if I check the value of smsg, it is empty.
> NewMsg1 (smsg) ‘ writes to a text box
>
> End Sub
>
> The subroutine messagetest is in another module. Messagetest sets the
> smsg string to a value, however when returning the value becomes an
> empty string.
>
> Sub messagetest(smsg As String)
> smsg = "message test set this string"
> End Sub
>
> If this sub is in the same module as the sheet code, it works. I have
> tried declaring smsg as public, but still doesn't work. I've tried
> passign byval, still doesn't work.
>
> Why is this and is it possible to pass arguments between code
> associated with a worksheet, and modules?
>
>
> Thanks!
>

 
Reply With Quote
 
Nigel
Guest
Posts: n/a
 
      18th Sep 2008
You cannot pass values back from a sub other than in public variables.

You can pass a value back from a function, the value assumes the value
assigned in the function and is referred to by the function name.

Private Sub dothings_Click

NewMsg1 = messagetest(smsg)

End Sub

Function messagetest(myMessage as string) as String
' set default reply message
messagetest = "Function Failed"

' refer to myMessage in this code
' your code here which also sets the return message if the code worked
messagetest = "Function Worked OK"

End Function

--

Regards,
Nigel
(E-Mail Removed)



"Revolvr" <(E-Mail Removed)> wrote in message
news:288abdf4-2057-49f3-a655-(E-Mail Removed)...
I'm at a loss here...

I have code associated with controls on a worksheet, which call
subroutines in other modules, and I am passing an argument, a string,
indicating the success or failure of the operations in that
subroutine. Then the string gets appended to a TextBox. But I am not
able to pass the string argument.

In Sheet1 I have
Private Sub dothings_Click()
'Dim smsg As String

Call messagetest(smsg)
‘ Here if I check the value of smsg, it is empty.
NewMsg1 (smsg) ‘ writes to a text box

End Sub

The subroutine messagetest is in another module. Messagetest sets the
smsg string to a value, however when returning the value becomes an
empty string.

Sub messagetest(smsg As String)
smsg = "message test set this string"
End Sub

If this sub is in the same module as the sheet code, it works. I have
tried declaring smsg as public, but still doesn't work. I've tried
passign byval, still doesn't work.

Why is this and is it possible to pass arguments between code
associated with a worksheet, and modules?


Thanks!

 
Reply With Quote
 
Revolvr
Guest
Posts: n/a
 
      18th Sep 2008

OK so I added

Dim smsg as string

at the top of the module. Also, in a couple of places I had a line
line:

messagetext (smsg)

instead of:

Call messagetext(smsg)

I had to change everything to a "Call".

So it seems to work, but I'm not sure I understand why, which seems to
be wrapped up in the differences between code in a module, and code
associated with a worksheet.

Thanks a lot!


 
Reply With Quote
 
Jim Thomlinson
Guest
Posts: n/a
 
      18th Sep 2008
There are 2 ways to pass a variable. One is byref and the other is byval.
ByVal passes a copy of the variable to the sub and so any changes to the
variable are lost when the sub ends. By ref passes the acual variable so any
changes to the variable are kept in the calling procedure after the called
procedure ends.

While global variables can be handy be very judisious in their useage. They
are a pain to debug because if at any point the value is wrong it can be very
difficult to know which procedure modified it last...
--
HTH...

Jim Thomlinson


"Revolvr" wrote:

> I'm at a loss here...
>
> I have code associated with controls on a worksheet, which call
> subroutines in other modules, and I am passing an argument, a string,
> indicating the success or failure of the operations in that
> subroutine. Then the string gets appended to a TextBox. But I am not
> able to pass the string argument.
>
> In Sheet1 I have
> Private Sub dothings_Click()
> 'Dim smsg As String
>
> Call messagetest(smsg)
> ‘ Here if I check the value of smsg, it is empty.
> NewMsg1 (smsg) ‘ writes to a text box
>
> End Sub
>
> The subroutine messagetest is in another module. Messagetest sets the
> smsg string to a value, however when returning the value becomes an
> empty string.
>
> Sub messagetest(smsg As String)
> smsg = "message test set this string"
> End Sub
>
> If this sub is in the same module as the sheet code, it works. I have
> tried declaring smsg as public, but still doesn't work. I've tried
> passign byval, still doesn't work.
>
> Why is this and is it possible to pass arguments between code
> associated with a worksheet, and modules?
>
>
> Thanks!
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass Control as Argument Clyde Microsoft Access VBA Modules 3 25th Feb 2010 05:41 AM
Pass control back to Excel window Hendy88@gmail.com Microsoft Excel Worksheet Functions 1 29th Dec 2006 07:24 PM
Pass control back from HTTPModule or HTTPHandler Max Microsoft ASP .NET 1 27th May 2006 09:56 PM
Trying to pass a variable back from a user control Phil Certain Microsoft ASP .NET 11 1st Feb 2005 10:55 AM
How to overcome the limitation: Cannot pass 'argument' as ref or out, because ' argument ' is a marshal-by-reference class Mountain Bikn' Guy Microsoft C# .NET 2 15th Nov 2003 07:45 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:39 AM.