One of the Ab Melakhoth (primary forbidden Sabbath labors) on Shabbath, is the Melakha of צד (Sad - trapping). There is a dispute in the Gemara as to whether or not the Biblical prohibition is limited to animals which are במינו צד (animals which are normally trapped for use, such as eating their meat or for their fur and so on).
The Shulkhan Arukh decides the Halakha in accordance with the opinion that only a case of במינו צד (Bemino Sad) is a prohibition from the Torah. Nevertheless, even according to this opinion there is still a Rabbinical prohibition of trapping animals that do not fall into this category (of במינו צד). Therefore, one may not trap a fly or other such creature, on Shabbath.
The Rema says, based on this, that if one has a small drawer with a fly in it, he may not close it on Shabbath. He quotes, however, an opinion that permits it. The Beth Yosef rules in accordance with the stringent opinion. The Ba"H, the Maghen Abraham, the Ben Ish Hai and the Mishna Berura, are also stringent. This is very common when one a wants to close a window that has a screen, and by closing the screen the moths, flies or other insects have no way of exiting. One may not close the window, but must first send away the insects.
(Shulkhan Arukh siman 316:3. Ben Ish Hai, 2nd year, Waera, Oth 6. Mishna Berura Oth 16. Menuhath Ahaba, Heleq 2 ch.17, Oth 12)
Tzad, Tsad, Sad, trapping, hunting on Shabbath, Rabbi Menashe Menashe