MS Access Database Forms: Using The SelStart And SelLength Properties

MS Access Database Forms: Using The SelStart And SelLength Properties

Have you noticed when you navigate between control fields in your MS Access database forms that it automatically selects the entire content (if not empty) instead of placing the cursor to the end of the last character?

ms access database design form

Using some simple Access VBA code, you can now control the placement for the cursor to mimic and automate placing the insertion point to the last character position ready for further editing.

MS Access Database Forms: Using The SelStart And SelLength Properties

The main two attributes or properties as they are generally referred as are called the SelStart and SelLength properties.

As the MSDN Access help file describes it: The SelStart property specifies or determines the starting point of the selected text or the position of the insertion point if no text is selected. 

Which means, it places the cursor in the first character position and changes the selection of characters with a length of 0 (zero) to it.

The SelLength property specifies or determines the number of characters selected in a text box or control field which can be edited.

This property will count and return the number of characters found in a text box control and act as the starting position for the first property, hence the Access VBA code line as below:

Me.[Your Control Name].SelStart = Me.[Your Control Name].SelLength

Make sure if you are not manually going to navigate to this field control that you also include the SetFocus method call otherwise it will cause an error since the control needs to have focus (be active).

You could improve this simple line of code to a more generic function and call it into any selected control by adding something like:

ms access database design form function code

Using the ‘On Enter‘ event for the text box control will call this procedure and place the cursor at the end after the last character helping to improve your MS Access database form.

Learning to build Access forms and adding code in your database can be really easy to apply which requires very little programming skill – it’s really not that difficult! 

I have created utilities for your Access database which expands on using VBA code and is completely open for you to utilise and expand on. Why not check it out now?

This entry was posted in Forms, MS Access, VBA and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Confirm you are human.