In life, there are things that happen to us because we've invested in making them happen, and some things that happen randomly and that we had no control over.
Some people will invest a lot of effort and get little in return, some people will invest nothing and get all the luck. Most people fall somewhere in between.
What you need to decide is how much you want to invest in the hope of a return and more importantly, how long you are prepared to invest until you give up if you don't get the returns.
I went through a phase of feeling low when all my efforts seemed to yield failure and wondering what was the point and then of course, feeling sorry for myself focusing on those who seemed to get everything handed to them on a plate, wallowing in the despair of 'it's not fair'.
Then I woke up and asked myself how I would feel on my death bed looking back at my life and I realised that I would feel much happier about my life thinking that I did do everything I could to get what I wanted in life, even if it didn't lead to the expected outcomes rather than thinking that I passed by opportunities because I didn't bother trying.
I am lucky to have children by 31, but that wasn't with the man of my dream. Despite only meeting at 27, I had never had a committed relationship before I met my ex. When we separated, I was determined to find my Mr right and confident that it would happen. Reality set in. After spending time on dating site, joining group, going to the gym, and doing everything I could to meet Mr Perfect, I started to give up. It wasn't just me, single friends were going through the same process, losing hope and giving up. I didn't though, even when I stopped believing it would happen, I continued to try, and one day, and yes, when I really had given up, he came into my life just like that. I was 38.
In the end, we don't control everything and despite the desire on both side, we didn't add a child to our family. I also learned that even with Mr Perfect, life and marriage isn't always so, but I am happy.
OP, have you tried counselling? As said, you can't predict what the future will bring your way, but at 40, there is still plenty of time to fulfill your life and take control over it rather than letting the years go by with the routine of it. I wish you all the best.