Set a variable

G

Guest

Hi.
Is it possible to define a variable = value of the field on focus?
E.g.: in a field called «Var» I enter the value «1», and if I have the mouse
cursor there, a click on a specified button would make appear a Msgbox with
the sentence "The value in field 'Var' is 1".
Is possible to do it?
Thank you.
Acores
 
G

Guest

Dim varMyVariable As Variant
varMyVariable = Me.var
Msgbox "The value in the field Var = '" & CStr(varMyVariable)

If you want to generalize this so it picks up the value regardless of the
texbox it's in, create a routine like this:

Private Sub DisplayVariable(ByVal strControl As String, ByVal strValue As
String)
MsgBox "The value of '" & strControl & "' is '" & strValue & "'"
End Sub

Then you have to determine which control you want to report on. If you want
to see the value of the last textbox you updated before pressing the button,
do something like this in the button's click event:

DisplayVariable Me.PreviousControl.Name, Me.PreviousControl

Because the command button now has the focus, you need to know which control
it grabbed focus from. PreviousControl will do this for you.

Barry
 
G

Guest

Here is what I think you are wanting to do:

Private Sub Command27_Click()
Dim ctl As Control
Dim varCtlValue As Variant
Dim strCtlName As String

On Error GoTo Command27_Click_Error

Set ctl = Screen.PreviousControl
Select Case ctl.ControlType
Case acCommandButton
varCtlValue = "No Value"
strCtlName = ctl.Name
Case acOptionButton
varCtlValue = ctl.Parent
strCtlName = ctl.Parent.Name
Case acToggleButton
varCtlValue = ctl.Parent
strCtlName = ctl.Parent.Name
Case Else
varCtlValue = ctl
strCtlName = ctl.Name
End Select

varCtlValue = Nz(varCtlValue, "Null")
MsgBox strCtlName & " = " & varCtlValue

Set ctl = Nothing

Command27_Click_Exit:

On Error Resume Next
Exit Sub

Command27_Click_Error:

'Trap for No Previous Control Error
If Err <> 2843 Then
MsgBox "Error " & Err.Number & " (" & Err.DESCRIPTION & _
") in procedure Command27_Click of VBA Document Form_Ztester"
GoTo Command27_Click_Exit
End If

End Sub

I did not test every type of control, but this should give you the basic idea.
 
G

Guest

You are on the right track, Barry, but there are some controls that give
misleading info or thow an error. Also, controls contained in other controls
(option groups) will give the name and value of the button, not the option
group. List Boxes would have to be handled differently as well, but I
haven't tested them. See my previous post.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Variable value 1
Subform sintaxe 2
Buttons enabled/disabled 4
Store a variable 1
Option Group Dilemma 3
Form Automation 7
Table update form a subform 3
Checkbox error 4

Top