Can I attach a 40 spoke hub to a 36 spoke rim? I just discovered a crack in the back rim of my mother's bike and I'd like to replace it. The trouble is that the hub is 40 spoke and I cannot find a 40 hole rim that would fit the bike and would be less expensive than buying a new bike.

However I can find plenty of 36 hole rims that are perfect (other than the fact they have 36 holes).

I was wondering if there are lacings that are safe for connecting a 40 hole gear hub to a 36 hole rim.

I would rather not replace the entire wheel because it would, due to the rather antique internal hub currently in use, likely mean adding a derailleur, replacing the gear-line and replacing the chain, which just seems a little overkill.
"Perverse" is what Sheldon Brown calls this sort of wheelbuilding, but it apparently can be done. There are even video tutorials for a 32-36 mismatch, which might help you. Unless you really need a sturdy rim, you could look into using a 32-hole rim as a 4:5 pattern might be simpler than 9:10. You need several sizes of spoke for such a build. 

Another option worth a look is trying to get undrilled rims, though they're probably expensive.

I have heard of people building wheels ignoring the spoke hole offset from the centreline, or filing out the holes if the offset is slight. Doing that on a 20-hole rim would mean you'd have to drill every other hole - but be careful as the valve hole could easily be a problem (most but not all of my wheels have it halfway between spokes). I'd be wary of such an approach personally.

I don't suppose the front is 40h is it? Building a new front wheel and putting the old front rim on the back would be a neat solution if it is, but 40h front wheels aren't common.