screen.activeform feild value

swingline

Member
Local time
Today, 18:21
Joined
Feb 18, 2020
Messages
51
Is it possible to use Screen.Activeform to reference a specific field value? something like?

Dim SN As String

SN = Screen.ActiveForm.Name.EquipSN.Value
 
Implied in your question is that you are trying to open several different forms with basically the same structure.

In other words, you have a form which you have duplicated for different purposes. On these duplicate forms you have a particular control that provides you information you want to take into further processing.

If this is the case, then the usual approach is not to have duplicate forms, but use code to alter the characteristics of the primary form so that you don't end up with lots of duplicates.

If that's not the souce of this issue, then please explain more about your problem.
 
Last edited:
you can use screen.activeform.nameofcontrol

but there are usually better ways of doing this - passing the form as a parameter to a function for example
 
Implied in your question is that you are trying to open several different forms with basically the same structure.

In other words, you have a form which you have duplicated for different purposes. On these duplicate forms you have a particular control that provides you information you want to take into further processing.

If this is the case, then the usual approach is not to have duplicate forms, but use code to alter the characteristics of the primary form so that you don't end up with lots of duplicates.

If that's not the souce of this issue, then please explain more about your problem.
Basically I only have two forms, one for single equipment edit allowing for notes to be added and one continuous form for bulk edits. Same table just different form names. Im trying to adapt an audit/changes bit of code to include the SN when changes are made on either form.

more than likely you can do this as well:
Code:
screen.activeform.controls("name_of_control_here").value
Thanks this works as intended. But didn't solve my larger problem so I'll need to do a bit more searching.



This is the code I am working with the issue I have now is getting this to work with the continuous form seems to break down at this line

If IsNull(c.OldValue) And Not IsNull(c.Value) Then

Code:
Function WriteChanges()

Dim f As Form
Dim c As Control
Dim frm As String
Dim SN As String
Dim user As String
Dim sql As String
Dim changes As String
Dim db As DAO.Database

Set f = Screen.ActiveForm
Set db = CurrentDb

frm = Screen.ActiveForm.Name
user = GetUserName
changes = ""
SN = Screen.ActiveForm.Controls("EquipSN").Value

   sql = "INSERT INTO Audit " & _
         "([FormName], [SN], [User], [ChangesMade]) " & _
         "VALUES ('" & frm & "', '" & SN & "','" & user & "', "

    For Each c In f.Controls

      Select Case c.ControlType
          Case acTextBox, acComboBox, acListBox, acOptionGroup
               If IsNull(c.OldValue) And Not IsNull(c.Value) Then
                  changes = changes & _
                  c.Name & "--" & "BLANK" & "--" & c.Value & _
                  vbCrLf
               ElseIf IsNull(c.Value) And Not IsNull(c.OldValue) Then
                  changes = changes & _
                  c.Name & "--" & c.OldValue & "--" & "BLANK" & _
                  vbCrLf
               ElseIf c.Value <> c.OldValue Then
                  changes = changes & _
                  c.Name & "--" & c.OldValue & "--" & c.Value & _
                  vbCrLf
               End If
      End Select

    Next c

   sql = sql & "'" & changes & "');"

db.Execute sql, dbFailOnError

Set f = Nothing
Set db = Nothing

End Function
 
Thanks this works as intended. But didn't solve my larger problem so I'll need to do a bit more searching.
no problem. there are literally an endless number of ways to reference controls like this, with all different kinds of syntax and differentiating statements that can be used. FYI.
 
If you know the correct name of the form that contains this control, you would do better to use syntax similar to Forms("FormName").Controls("ControlName").Value and .OldValue in order to access the particular controls.

Or, since you have a FOR EACH loop, use

Code:
FOR EACH c IN Forms("FormName").Controls

Trying to go all around the mulberry bush (through Screen.ActiveForm) to get to a form when you already know its name seems like trying too hard. You said you had only two forms, so you should be able to figure that out by process of elimination. Or is there something you didn't clarify there?
 

Users who are viewing this thread

Back
Top Bottom