?
Solved

ACCESS 2010 pass calling textbox field name to function

Posted on 2013-10-30
7
Medium Priority
?
1,359 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 30

Assisted Solution

by:hnasr
hnasr earned 800 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 1200 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
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
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.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

765 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