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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Current Field Name

Status
Not open for further replies.

andy4929

IS-IT--Management
Feb 24, 2003
1
GB
Does anyone know how to obtain the current field name within a form,

I am trying to log events on a database, i can get the users name and the form, what i am now after is to get the field that has been updated, ie running code after an update has been detected.
 
In the past I give a little try (it wasn't necc., so never ended it) but maybe u can use it as basic for ur problem

Public Sub gewijzigd(frm As Form)
On Error GoTo errHandler
Dim cnn As New ADODB.Connection
Dim ctl As Control, strSQL As String

Set cnn = CurrentProject.Connection
For Each ctl In frm.Controls
If ctl.ControlType = acTextBox Then
With ctl
If Nz(.OldValue, &quot;&quot;) <> Nz(.Value, &quot;&quot;) Then
strSQL = &quot;INSERT INTO tblRecordGewijzigd([W_datum],[W_user],[W_fldname],&quot; & _
&quot;[W_form],[W_oudewaarde],[W_nieuwewaarde]) VALUES ('&quot; & _
Date & &quot;','&quot; & HaalGebruiker & &quot;','&quot; & .Name & &quot;','&quot; & _
frm.Name & &quot;','&quot; & _
IIf(Nz(.OldValue, &quot;&quot;) = &quot;&quot;, &quot;x&quot;, Nz(.OldValue, &quot;&quot;)) & &quot;','&quot; & _
IIf(Nz(.Value, &quot;&quot;) = &quot;&quot;, &quot;x&quot;, Nz(.Value, &quot;&quot;)) & &quot;')&quot;
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
End If
End With
End If
Next ctl
Exit_proc:
Exit Sub
errHandler:
MsgBox Err.Description & Err.Number
Resume Exit_proc
End Sub
 
This will always give the correct Field Name.

Paste this into a Global Module:

Function Get_CurrentFieldName()
Get_CurrentFieldName = Screen.ActiveControl.ControlSource
End Function


To see how the Function works, paste the following into the Got Focus event of a Control on a Form:

msgbox Get_CurrentFieldName
 
Look at the VB help on the Screen object.

Screen.ActiveControl -and-
Screen.ActiveForm
 
see faq181-291

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top