Solved

ACCESS 2010 pass calling textbox field name to function

Posted on 2013-10-30
7
1,293 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 119

Accepted Solution

by:
Rey Obrero 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now