Solved

ACCESS 2010 pass calling textbox field name to function

Posted on 2013-10-30
7
1,322 Views
Last Modified: 2013-10-30
I have to add the code below to ~100 textboxes On Double Click Event:

Private Sub Ctl04_01_PC_DblClick(Cancel As Integer)
   If IsNull(Me.Ctl04_01_PC) Then Me.Ctl04_01_PC = "X"
End Sub

Is is possible to write a function similar to this:
Public Function DblClick( objectname as ??)
    If IsNull(objectname) Then objectname= "X"
End Function

and then change the On Double Click Event for the 100 fields to:
DblClick(objectname)

i guess what i am asking, is there a way to pass the field name to a function?
0
Comment
Question by:BFanguy
7 Comments
 
LVL 30

Assisted Solution

by:hnasr
hnasr earned 200 total points
ID: 39612445
If declared as a sub:
Private Sub Text1_DblClick(Cancel As Integer)
      DblClick(Screen.ActiveControl.Name)
End Sub

As function:
Private Sub Text1_DblClick(Cancel As Integer)
      var=DblClick(Screen.ActiveControl.Name)
End Sub

Declare control as string in function or sub.
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 300 total points
ID: 39612450
place this codes in a regular module


Function fSetvalue()
If IsNull(Screen.ActiveControl) Then
     fSetvalue = "X"
     Screen.ActiveControl = fSetvalue
End If
End Function


select all the textboxes in your form and in the On Dbl Click event property place

On Dbl Click =fsetValue()
0
 

Author Closing Comment

by:BFanguy
ID: 39612557
Both work, thank you guys.  capricorn1 i used your logic - easy to follow!
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 39612558
I think there is some confusion here between controls and their bound fields.  If you use a standard naming convention, so combo boxes have the prefix "cbo" followed by the field name, you can easily extract the field name from the control name using this expression:

Mid(strControlName, 4)

Then use one of the code samples above to work with the field value.

If the controls have the same names as their bound fields, this should be fixed, as it can lead to reference errors (Microsoft does this automatically when fields are placed on a form -- a very bad design practice).
0
 

Author Comment

by:BFanguy
ID: 39612575
Helen,

It is interesting you say "If the controls have the same names as their bound fields, this should be fixed, as it can lead to reference errors (Microsoft does this automatically when fields are placed on a form -- a very bad design practice)."  I have seen these comments before and I have never seen a problem with the control having the same name as its bound field and I have written well over 1,000 apps in access dating back to version 2.0.

Can you give me an example where this causes problems?
0
 

Author Comment

by:BFanguy
ID: 39612777
capricorn1, what can i add to the function to perform a tab after setting the field to X?

thank you in advance.
0
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 39612865
Here is an example:  Code that uses the BackColor property might fail because that is a control property, not a field property.  I have been developing in Access since the beta of 1.0, and I have seen these errors a few times, in code by other people that I have taken over.  It doesn't happen often, but it is a good idea to use a naming convention, for this and other reasons.  When I see a reference in code to txtStartDate, I know that is a control, while dteStart is a variable, and StartDate is a field.

I developed my LNC Rename add-in after dealing with a nightmare of a database that had a Sales table, Sales query, Sales form, Sales Report, and about five Sales variables of different (but undeclared) data types.  Now I just use the add-in to semi-automatically apply the appropriate prefix to database objects and controls.  Several versions for different versions of Access can be downloaded from this page on my Website:

http://www.helenfeddema.com/CodeSamples.htm
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question