Sally
Sally

Reputation: 1789

Access VBA 2007 passing a form as a parameter

We are trying to reduce duplicate code in our access database. We have severalforms what we use the following sort of code on.

strSQL = "Some SQL"
Me!cboClientName.RowSource = strSQL
Me!cboClientName.BoundColumn = 2
Me!cboClientName.ColumnCount = 2
Me!cboClientName.ColumnWidths = "0cm ; 2 cm"

What I wanted to do is create a sub routine and pass the form into it and then replace the Me with the object.

So I now have something like this:

Public Sub subName(myForm as Form)
    strSQL = "SELECT [tblClients].[ID], [tblClients].[ClientName] FROM [tblClients] ORDER BY [ClientName]"
    myForm !cboClientName.RowSource = strSQL
    myForm !cboClientName.BoundColumn = 2
    myForm !cboClientName.ColumnCount = 2
    myForm !cboClientName.ColumnWidths = "0cm ; 2 cm"

cboClientName is on every form that I am calling it on. I then am calling the sub routine using.

subName(Me)

I am however getting the error:

Type mis match ... 13

What am I doing wrong? Is there a better way todo this?

Upvotes: 0

Views: 2843

Answers (1)

Fionnuala
Fionnuala

Reputation: 91316

That should be

 subName Me

No brackets.

Further information: http://msdn.microsoft.com/en-us/library/gg251710.aspx

Upvotes: 1

Related Questions