Passing a control variable to another sub

  • Thread starter Thread starter Bharat Odedra
  • Start date Start date
B

Bharat Odedra

Hello

In a textbox on a form in access if an incorrect value is
entered a message appears asking for the correct value to
be entered. I would like this textbox to have focus and
all characters in this box to be Selected so that the user
has to type in a new value which will over write the
existing value.

The selection of the existing text needs to be done by a
function/sub so that it can be resued for other textboxs.
How do I declare a control variable in both the calling
sub and pass it on to the sub doing the selection. In the
example below I have used xxx and yyy for where the
variable needs to be declared.

In this example the calling sub is named after the
control. Is there another way of referring to the control
without using the whole control's name e.g. me.TotalValue
to refer to the control in the sub TotalValue_Afterupdate()

Thanks in advance for your help


Private Sub TotalValue_AfterUpdate()
Dim strvalue As Integer
strvalue = Len(CStr(Me.TotalValue))
If Me.TotalValue.Value <> Me.Text36.Value Then
MsgBox "The Total Value is NOT equal to the amount
ordered" & vbCrLf & "Plese re-enter the total value or
change value in denominations ordered."
SelectTBConts strvalue,XXX
Else
Me.Command10.Enabled = True
End If
End Sub

Public Sub SelectTBConts(SrtLen As Integer, XXX as YYY)
With Me!TotalValue
.SetFocus
.SelStart = 0
.SelLength = SrtLen
End With
End Sub
 
Hi Bharat,

You might want to consider moving the validation code to the BeforeUpdate
event of the control, then use Cancel=True to prevent focus from leaving the
control which is in error.

You can pass a reference to the control itself -

SelectCtl Me.TotalValue

Public Sub SelectCtl(ctlIn As Control)
With ctlIn
.SetFocus
.SelStart = 0
.SelLength = Len(ctlIn.Value)
End With
End Sub
 
Sandra

Thanks for the reply. Your suggestion to use BeforeUpdate
has solved the problem of the control losing focus. I now
understand the difference to Afterupdate. I had to remove
the ".setfocus" statement as it resulted in an error. I
guess this was because the control already had focus from
the calling sub.

Bharat
-----Original Message-----
Hi Bharat,

You might want to consider moving the validation code to the BeforeUpdate
event of the control, then use Cancel=True to prevent focus from leaving the
control which is in error.

You can pass a reference to the control itself -

SelectCtl Me.TotalValue

Public Sub SelectCtl(ctlIn As Control)
With ctlIn
.SetFocus
.SelStart = 0
.SelLength = Len(ctlIn.Value)
End With
End Sub

--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

Bharat said:
Hello

In a textbox on a form in access if an incorrect value is
entered a message appears asking for the correct value to
be entered. I would like this textbox to have focus and
all characters in this box to be Selected so that the user
has to type in a new value which will over write the
existing value.

The selection of the existing text needs to be done by a
function/sub so that it can be resued for other textboxs.
How do I declare a control variable in both the calling
sub and pass it on to the sub doing the selection. In the
example below I have used xxx and yyy for where the
variable needs to be declared.

In this example the calling sub is named after the
control. Is there another way of referring to the control
without using the whole control's name e.g. me.TotalValue
to refer to the control in the sub TotalValue_Afterupdate()

Thanks in advance for your help


Private Sub TotalValue_AfterUpdate()
Dim strvalue As Integer
strvalue = Len(CStr(Me.TotalValue))
If Me.TotalValue.Value <> Me.Text36.Value Then
MsgBox "The Total Value is NOT equal to the amount
ordered" & vbCrLf & "Plese re-enter the total value or
change value in denominations ordered."
SelectTBConts strvalue,XXX
Else
Me.Command10.Enabled = True
End If
End Sub

Public Sub SelectTBConts(SrtLen As Integer, XXX as YYY)
With Me!TotalValue
.SetFocus
.SelStart = 0
.SelLength = SrtLen
End With
End Sub

.
 
Back
Top