Solved

ACCESS 2010 pass calling textbox field name to function

Posted on 2013-10-30
7
1,282 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

762 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

23 Experts available now in Live!

Get 1:1 Help Now