Carl
Carl

Reputation: 5779

VBA function that dynamically appends year name

I have a form in Access with check boxes for each year. The check boxes are named ch+(whatever year) I want to write run a query that depends on the check boxes, but I don't want to rewrite it for each box so I am writing a function that takes the year as input, checks if that year's box is checked, then runs the query if it is checked.

I tried something like this:

public function check(year as integer)
dim boo as string
boo="me.ch" & year
if boo then
...
end if
end function

It appears that setting a string variable equal to the name of the boolean variable in the form I want checked doesn't work.

How do I write a function the inputs the year, and then tests if the check box in the form related to that year is true?

Thanks

Upvotes: 0

Views: 56

Answers (1)

Gustav
Gustav

Reputation: 55816

Try this:

Public Function Check(Year As Integer)

    If Me("ch" & CStr(Year)).Value = True Then
        ' Do stuff.
    End If

End Function

Upvotes: 1

Related Questions