Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need code to be activated e/ time user clicks on form 1

Status
Not open for further replies.

tziviak2

MIS
Jul 20, 2004
53
0
0
US
I have a form with textboxes, list boxes... I need code to be run e/t s/t is clicked on the form - I tried putting the code in the form's onCurrent, beforeUpdate,afterUpdate,onClick,onActivate,afterInsert
but none seemed to work -I even tried "form.refresh" in the onclick of every field

any suggestions would be appreciated
 
First suggestion. Please explain clearly what you want to do, not what does not work. This is a pretty cryptic message you have posted. Second, please explain what is e/t s/t. Probably a common abbreviation, but I do not know it.
Are you saying, that you would like every list box and text box on a form to fire the same sub routine whenever it is clicked. If that is the case, then you could simply put onclick events for each control and have the code on click code call a subroutine. Now if you have a lot of controls and do not want to do that, then it will require building both a custom class and custom collection. Need more details.
 
No idea if this is what your asking, but...

Here is an example using a custom class and custom collection. I have about 50 text boxes on this form. With just the following I can click on any of them and get a message box with name, value, and variable type.

Code:
Option Compare Database
Option Explicit

Public clickControls As clsClickControls
Public clickControl As clsClickControl
Private Sub Form_Open(Cancel As Integer)
  Dim myCntrl As Object
  Dim myTextBox As TextBox
  Set clickControls = New clsClickControls
  For Each myCntrl In Me.Controls
    If myCntrl.ControlType = acTextBox Then
      Set myTextBox = myCntrl
      Set clickControl = clickControls.Add(myTextBox)
      Set clickControl = Nothing
    End If
  Next myCntrl
End Sub

These are the custom classes to allow this to work

clsClickControl
Code:
Option Compare Database
Option Explicit

Private WithEvents mClickTextBox As TextBox
Private mName As String

Public Property Set ClickTextBox(theClickTextBox As TextBox)
  Set mClickTextBox = theClickTextBox
  mClickTextBox.OnClick = "[Event Procedure]"
End Property

Private Sub mClickTextBox_Click()
  MsgBox Me.Name & "  " & "Value: " & mClickTextBox.value & "  " & "Type: " & VarType(mClickTextBox.value)
End Sub

Public Property Get Name() As String
  Name = mName
End Property

Public Property Let Name(ByVal theName As String)
  mName = theName
End Property

collection class clsClickControls
Code:
Option Compare Database
Option Explicit

Private mClickControls As New Collection
Public Property Get Count() As Long
  Count = mClickControls.Count
End Property
Public Function Add(theClickControl As TextBox) As clsClickControl
  Dim newClickControl As New clsClickControl
  Set newClickControl.ClickTextBox = theClickControl
  newClickControl.Name = theClickControl.Name
  mClickControls.Add newClickControl
  Set Add = newClickControl
End Function
Public Function Item(index As Variant) As clsClickControl
  Set Item = mClickControls.Item(index)
End Function

Public Sub Delete(index As Variant)
 mClickControls.Remove index
End Sub

One event reacts to the 50 controls.
 
thank you -the first response was helpful-I was just trying to get away with writing code in one place-but I guess I have to call the code from every listbox...
btw (by the way) e/t -(everything) and s/t -(something)

my next question is that I have a textbox -andthe onclick function calls the pickDate() -now I want everytime the date is changed-code should run-I tried putting it into the afterUpdate of the textbox but the code never runs-any suggestions?
thank you
 
The update events and some other events do not work if the control is manipulated by code. Only work if manually manipulated. So do the code after you call the pickDate. Example

....
me.txtBoxDate = pickDate()
call subRoutineThatDoesTheCode
....

I was just trying to get away with writing code in one place-but I guess I have to call the code from every listbox.

Did you not want to try the custom classes? This solves the problem of "one event for all controls". It is really worth trying because this concept of custom classes, custome events, and "with events" variables, will really expand your code writing, once you understand the principles.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top