Passing variable question

M

Mike

I have a if then statement in a form calling a public sub in a module, that
section works fine, but affter doing calculations I want to pass the
variable back the the form. How can I go by doing this.

'This is in the Form:
Private Sub btnsubmit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnsubmit.Click

Dim txtuserid As String

Dim acctstat As String

txtuserid = "testuser"

If chkuseracct.CheckState = CheckState.Checked Then Call
test(txtuserid, acctstat) Else acctstat = "No Account Created"

MsgBox(acctstat)

End Sub





'This is in the Modue:

Public Sub test(ByVal txtuserid As String, ByVal acctstat As String)

acctstat = "Account is created"



End Sub
 
A

Armin Zingler

Mike said:
I have a if then statement in a form calling a public sub in a
module, that section works fine, but affter doing calculations I
want to pass the variable back the the form. How can I go by doing
this.

'This is in the Form:
Private Sub btnsubmit_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnsubmit.Click

Dim txtuserid As String

Dim acctstat As String

txtuserid = "testuser"

If chkuseracct.CheckState = CheckState.Checked Then Call
test(txtuserid, acctstat) Else acctstat = "No Account Created"

MsgBox(acctstat)

End Sub





'This is in the Modue:

Public Sub test(ByVal txtuserid As String, ByVal acctstat As String)

acctstat = "Account is created"



End Sub




http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconArgPassMechanism.asp

http://msdn.microsoft.com/library/en-us/vbcn7/html/vbconFunctionProcedures.asp


Armin
 
G

Guest

Try the following code:
Private Sub btnsubmit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnsubmit.Click
Dim txtuserid As String
Dim acctstat As String
txtuserid = "testuser"
If chkuseracct.CheckState = CheckState.Checked Then acctstat =
test(txtuserid, acctstat) Else acctstat = "No Account Created"
MsgBox(acctstat)
End Sub

'This is in the Modue:
Public Function test (ByVal txtuserid As String) as string
Return = "Account is created"
End Function
 
P

pvdg42

Mike said:
I have a if then statement in a form calling a public sub in a module, that
section works fine, but affter doing calculations I want to pass the
variable back the the form. How can I go by doing this.

'This is in the Form:
Private Sub btnsubmit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnsubmit.Click

Dim txtuserid As String

Dim acctstat As String

txtuserid = "testuser"

If chkuseracct.CheckState = CheckState.Checked Then Call
test(txtuserid, acctstat) Else acctstat = "No Account Created"

MsgBox(acctstat)

End Sub





'This is in the Modue:

Public Sub test(ByVal txtuserid As String, ByVal acctstat As String)

acctstat = "Account is created"



End Sub
Assuming you mean that you will produce a numeric value in test() and want
that value to be accessible in btnsubmit_click(), try redefining test() as a
function procedure with a return type to match your calculation result, then
modify your call to capture the returned value.
Using your example and assuming a variable type Double, something like this
(code untested):

Public Function test(ByVal txtuserid As String, ByVal acctstat As String)
As Double

acctstat = "Account is created"

Dim retVal as Double

'code to create numeric result in retVal

Return retVal

End Function

Change the call to something like this:

Dim receiver as Double

If chkuseracct.CheckState = CheckState.Checked Then

receiver = test(txtuserid, acctstat)
'the local variable receiver will get the value returned from test()


Else acctstat = "No Account Created"
 

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