Solved

ACCESS 2010 pass calling textbox field name to function

Posted on 2013-10-30
7
1,314 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

808 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