Access MVP (2010-2015)

oOo.oOo.oOo.oOo.oOo.oOo

Call Me…

Calling an Event Procedure from one Form to another…

Every once in a while I need to use the same code I have in the Event Procedure of a one Control in another Forms Event Procedure, whether it be the On_Click event of a Command Button or the After_Update of a Combo Box.  99% of the time I would just turn that into in Function or a Sub and drop it into a Module and done, I can now reuse it over and over with no issues.  So why would I or anyone need to do this?  Simple… the Users makes an update on a Subform and I need to refresh the Main Form and that code is specific to the Controls on that Form/Subform, what easier way than to just reuse the code already available in an Event Procedure in another Form.  (This happens about 1% of the time, if not less.  It is not something I would recommend to do regularly.)  Now, I could just copy/paste it to the Subform but if I ever have to make a change I have to remember to hit both places!  Leaving it where it is makes sense, now, I just need to change it in one place.

So now, the specifics…

First and foremost, you must either remove the Private from in front of the Sub and/or change it to Public but it’s not necessary.  I tend to change it to Public, only because if I need to find it I can use Find > Public Sub as a parameter finding it quickly.  (Using Find > Sub will cause me to stop at every Event in every Module!)

Example…
Was Private Sub cboSearch_AfterUpdate changed to Public Sub cboSearch_AfterUpdate()

And the difference between Public Sub and Private Sub?  Ken Sheridan explains it best…

Declaring a function in a standard module Public exposes it throughout the database, so it can be called from queries, forms, code in other modules, etc.  Declaring a function in a standard module Private exposes it only within the module, so should be done where a function is called by another function or procedure in the same module, but not by anything outside the module.   The same name can be used for Private functions in different modules, but a Public function’s name must be distinct within the whole database.

From: Private or Pubic Functions?

Then you will need to make sure the Form that you are referencing is open.  That said, you can open it minimized or hidden but it must be open or you cannot access the Public Function.

From Subform (Child) to Main Form (Parent)
Function or Sub

Call Forms.frmYourForm.pubYourPublicFunction

OR…

Call Forms("frmYourForm").pubYourPublicFunction

Combo Box

Call Forms("frmYourForm").cmdYourComboBox_AfterUpdate

OR…

Me.Parent.cmdYourComboBox_AfterUpdate

Command Button

Forms("frmYourForm").cmdYourCommandButton_Click

OR…

Me.Parent.cmdYourCommandButton_Click

 

From Main Form (Parent) to Subform (Child) Form
Function or Sub

Me.sfrYourSubform.Form.pubYourPublicFunction

Combo Box

Me.sfrYourSubform.Form.cmdYourComboBox_AfterUpdate

Command Button

Me.sfrYourSubform.Form.cmdYourCommanButton_Click

 

From another Control on the same Form (Parent or Child)
Function or Sub (The same way you would call a normal Module)

Call pubYourPublicFunction

Combo Box

Call cboYourComboBox_AfterUpdate

Command Box

Call cmdYourCommanButton_Click

 

From Subform (Child) to Main Form (Parent) of Main Form (Parent) Form
Function or Sub

Me.Parent.Parent.pubYourPublicFunction

Combo Box

Me.Parent.Parent.cmdYourComboBox_AfterUpdate

Command Button

Me.Parent.Parent.cmdYourCommandButton_Click

 

From a Public Module to Subform (Child) via Main Form (Parent)
Function or Sub

Forms("frmMain")("sfrYourSubform").pubYourPublicFunction

Combo Box

Forms("frmMain")("sfrYourSubform").cmdYourComboBox_AfterUpdate

Command Button

Forms("frmMain")("sfrYourSubform").cmdYourCommandButton_Click

 624 total views

Controlling Scrollbars in Subforms…

Subforms are a little peculiar when used as Continuous Forms… when the Subform produces 3 records which, in this example, calls for the Scrollbar and you then switch, from the Main Form, and you only have two records or less showing you have this shadow area where the Scrollbar used to be (Figure2)… slightly annoying and not very nice looking on your Form.

To avoid this put the below in the On_Current event of the Subform…

Note: In this example the Subform is sized to allow three records to show.  If you have adjusted to show more (or less) records then adjust the RecordCount accordingly.

If Me.RecordsetClone.RecordCount > 1 Then
   Me.ScrollBars = 2
Else
   Me.ScrollBars = 0
End If

Form.ScrollBars Property

Setting Visual Basic Description
Neither (forms) None (text boxes) 0 (Default for text boxes) No scroll bars appear on the form or text box.
Horizontal Only (forms) 1 Horizontal scroll bar appears on the form. Not applicable to text boxes.
Vertical Only (forms) Vertical (text boxes) 2 Vertical scroll bar appears on the form or text box.
Both (forms) 3 (Default for forms) Vertical and horizontal scroll bars appear on the form. Not applicable to text boxes.

From: https://msdn.microsoft.com/en-us/library/office/ff834790.aspx?f=255&MSPPError=-2147217396

(Though the page says Office 2013 or later this property has been available since Access 97, so, feel free to use in earlier versions of Access.)

 1,176 total views

Highlight Current Record…

Highlighting the current record is an often asked question.  You could use Conditional Formatting but then you have to do it for each Control in the Detail section of your Form.  I prefer this way…

Step 1

Make sure all visible controls have been set for a Transparent background

Step 2

Put an invisible Control in the Form’s Header, in this example it’s named txtHighlightID and put the Primary Key in the Detail section and make that Control invisible also, in this example it’s named txtID.

Step 3

In the Forms (yes, also works on Subforms) On_Current event put…

Me.txtHighlightID = Me.txtID

Step 4

Put a Control, it’s named txtHighlight in this example, that spans the full width of the Form…

=IIf([txtID]=[txtHighlightID],"█████████████████████████████████████████████████████████████████████████████████████████████████████████████","")

Set the Font Size to insure it goes the Height of the Row and select the color of the Font for your Control (this is what determines the color of the highlighting).  In this example I used #F6FBDD.  Make sure to go the Arrange tab, click on txtHighlight and click Send to Back.  For how to get the Full Block that is the highlight, see Conditional Row Shading on a Continuous Form.

Neaten up your Form and then preview… when you click on a row it will now highlight.  You can download a sample here.

 2,074 total views

Access 2016 Runtime now available…

“The Microsoft Access 2016 Runtime enables you to distribute Access 2016 applications to Users who do not have the full version of Access 2016 installed on their computers.”

To download click here

 786 total views

Move Selections Up/Down in a List Box…

Moving selections up and down a List Box, sounds simple right?  Well, not so much… unless your name is Dale Fry.  I wanted a way for Users to be able to reorder items in a List Box, i.e. tasks, to do lists, activities and documents.  With this you can move items up or down on the list in order of importance.

Download the cool demo here.

 1,926 total views