Passing an Object (VBA Question)

M

Matt

Hi All,

I have the current Sub Procedure in a VBA Module.

-----------------------------------------------------------------------------------------------------------------------------
Sub openForm(formName As String, Optional varToSend As Object)
If varToSend Is Missing Then
DoCmd.openForm formName
Else
DoCmd.openForm formName, , , , , , varToSend
End If
End Sub
----------------------------------------------------------------------------------------------------------------------------
From a button on "frmDash" I call: openForm("frmEmployeeAdjust") -
This Works Great

But when I call: openForm("frmEmployeeAdjust",empNo) - I get an Error
"Expected: = "

I know I could just write the code in the button and everything would
work, but I would like to write this simple function that I could call
anytime I needed to open a form. I always thought Object was generic
enough to contain any data type that exists, so I should be ok weather
I pass an Integer, String, Etc. Am I doing something blantantly wrong
here?


As always, Thanks in advance for all of your help!
 
A

Albert D. Kallal

OpenArgs MUST be a string, it can't be object....

so, you need:

Sub openForm(formName As String, Optional varToSend As string)


varToSend since it is to be passed a arugment, MUST BE simple string
var....

if varToSend is a number, or string, then ms-access can/will cast the number
as a string for you..but, it not the acutal object, but simple string
var...
 
S

storrboy

Hi All,

I have the current Sub Procedure in a VBA Module.

-----------------------------------------------------------------------------------------------------------------------------
Sub openForm(formName As String, Optional varToSend As Object)
If varToSend Is Missing Then
DoCmd.openForm formName
Else
DoCmd.openForm formName, , , , , , varToSend
End If
End Sub
----------------------------------------------------------------------------------------------------------------------------


This Works Great

But when I call: openForm("frmEmployeeAdjust",empNo) - I get an Error
"Expected: = "

I know I could just write the code in the button and everything would
work, but I would like to write this simple function that I could call
anytime I needed to open a form. I always thought Object was generic
enough to contain any data type that exists, so I should be ok weather
I pass an Integer, String, Etc. Am I doing something blantantly wrong
here?

As always, Thanks in advance for all of your help!

Object is rather generic but it's not the best thing to use for known
objects.
If the variable represents a control use As Control, if it's just data
but of an unknown type use Variant. Basically always use the most
appropriate variable type.
Second the manner in which you check if the variable was passed is
incorrect. It should read...

If IsMissing(varToSend) Then
 

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