Link to home
Start Free TrialLog in
Avatar of jadaiber
jadaiber

asked on

Set field as variable

Can someone please show me how to set the field as a variable in the following code so that I can easily use it on multiple fields on my form?  Thanks.

Private Sub HoldForFinals_AfterUpdate()
Dim OldV, NewV As String
If HoldForFinals.OldValue = True Then
OldV = "YES"
Else
OldV = "NO"
End If

If HoldForFinals.Value = True Then
NewV = "YES"
Else
NewV = "NO"
End If

Me.Updates = Now & " - " & HoldForFinals.Name & " was changed by " & Environ("USERNAME") & " from [" & OldV & "] to [" & NewV & "]" & vbCrLf & Updates.Value

End Sub
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Not real sure what you're trying to do, but a couple of thoughts:
(1)  In your code, OldV will be a Variant, because you didn't explicitly state it as a string.  
I believe VB.NET would declare it as a String the way you wrote it, but Access would not.

(2)  Changed string variable names to sOldValue and sNewValue.
(3)  You'll want to make sure AfterUpdate event can interpret .OldValue and .Value correctly.
___

Dim sOldValue as String, sNewValue as String
If HoldForFinals.OldValue = True Then
   sOldValue = "YES"
Else
   sOldValue = "NO"
End If

If HoldForFinals.Value = True Then
   sNewValue = "YES"
Else
   sNewValue  = "NO"
End If
Avatar of jadaiber
jadaiber

ASKER

Sorry for not being clear.  I would like to set the field (in this case "HoldForFinals") as a varrible in the code so that when I use the code on a different field ( ReleasedForEngineering) I won't have to change the field throughout the code.   I'm really new to this so I'm sure I'm not framing this question correctly.
>so that when I use the code on a different field
In that case, you'll need to make it a form-level variable, and not a subroutine-level variable, so that you can use it the entire scope of your form being open.

Option Compare Database
Option Explicit

Dim sOldValue as String, sNewValue as String

Private Sub HoldForFinals_AfterUpdate()
If HoldForFinals.OldValue = True Then
sOldValue = "YES"
Else
sOldValue = "NO"
End If

If HoldForFinals.Value = True Then
sNewValue = "YES"
Else
sNewValue = "NO"
End If

End Sub
Is it possible to set HoldForFinals as a varible?

maybe like....

Dim myField as String
myField = me.HoldForFinals

and  then....

If myField.oldvalue = true then ..... etc

I've try this, but get an error.  What am I missing???

ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help