Wednesday 18 March 2009

Cool formy things

So, here's a form.

You might want to open the image in a new window and have it handy. This might get a little complex (those easily scared might want to look away now...)

It's for a newspaper delivery database (perhaps more of which at some point). Specifically it lets the user update when a customer will be on holiday. It's only actually updating some of the data in the record by the way, which is entirely possible but might not have occurred to you.

First off, top right there's a combo box to skip straight to a record based on the surname - although the drop down will also show other key data. That's easy (it's just a combo box which you Find a record on the form... using).

Then there's a calendar control to enter the date the holiday starts. This is just a standard click the calendar thing - the juice covered textbook covers it on pages 228-229.

But, and here's where it gets clever, I added some code.

You'll remember (or be about to look it up...) that there's some code you need to add in the Calendar control VBA editing scary looking bit: something along the lines of Me.HolsBegin.Value = Calendar3.Value?

Well, what I did was add some code below that to simply make the HolsEnd box be equal to the calendar click + 7 in the first instance. It's kinda like using a default value. Honest.

Look, here's the screenshot...Then, to avoid having two confusing calendars on the same form I added a spinner next to the HolsEnd box to allow that to be adjusted manually but without having to do the annoying typing thing. The code for this is on pages 229-230 of the juice covered textbook.

Now, the clever bit.

I wanted a button to enable me to clear both dates easily and without any fuss.

Could I find a way to do this? Could I heck. Nothing from the standard stuff seemed to work.

So, I experimented. And, guess what, it worked. Like, first time dude! Must be my lucky day...

Anyway, here's what to do:
  1. draw a button using the grey toolbar button thing
  2. choose any of the options and go for the text - you can change it later
  3. click finish to add it to the form (although you may want to give it a sensible name as well)
  4. make sure the button is selected
  5. hit the Properties button on the toolbar (or right click...)
  6. find the On Click bit, click in it and then click the three dots next to it
  7. choose Code Builder to go to the VB editor
  8. now, delete all the code apart from the first and last lines in the section related to your button - it'll be called Private Sub Name_Click()
  9. then add the code from the screenshot, using the names of your fields you want to clear
Heck, the code's easy isn't it? All it's doing is clearing both fields by making their value equal to nothing.

Easy. Lemon. Whatever.

And, yes, I do feel quite proud of that even though it's probably not that complex. Took 39 years and 350-odd days to get to that...

Which just goes to prove the foolishness of trying to teach new tricks to old dogs

No comments: