VBA Passing in Textbox into a function Help

J

Jerry8989

I have many date fields that I want to validate a range for. I created this
function to pass in the text so that it will verify the date and then display
a message if it is out of range then cancel the change so it isn't committed.

Private Sub txtInDate_BeforeUpdate(Cancel As Integer)
ValidationRFQDates (txtInDate)
End Sub

Private Sub ValidationRFQDates(txtField As TextBox)
If (DateValidation(txtField) = 0) Then
MsgBox "Date Must be Greater Than 1/1/1999 and Less Than " & DateAdd("yyyy",
10, Date)
Cancel = True
txtField.Undo
End If
End Sub

I keeps telling me "Object Required" on ValidationRFQDates (txtInDate)

Thank You
 
M

Marshall Barton

Jerry8989 said:
I have many date fields that I want to validate a range for. I created this
function to pass in the text so that it will verify the date and then display
a message if it is out of range then cancel the change so it isn't committed.

Private Sub txtInDate_BeforeUpdate(Cancel As Integer)
ValidationRFQDates (txtInDate)
End Sub

Private Sub ValidationRFQDates(txtField As TextBox)
If (DateValidation(txtField) = 0) Then
MsgBox "Date Must be Greater Than 1/1/1999 and Less Than " & DateAdd("yyyy",
10, Date)
Cancel = True
txtField.Undo
End If
End Sub

I keeps telling me "Object Required" on ValidationRFQDates (txtInDate)


That's because you enclosed the text box name in ( ) which
amkes it an expression that returns the text box's value
instead of an object reference. If you feel the need to use
( ) when calling a Sub procedure, then you must use:
Call ValidationRFQDates(txtInDate)

Even if you fix that, it still won't work because Cancel is
unknown in your procedure. You should make the procedure a
Function so it can return the cancel indicator. E.g.

Private Function ValidationRFQDates(txtField As TextBox) As
Boolean
. . .
ValidationRFQDates = True
. . .
End Function

and then use it this way:
Private Sub txtInDate_BeforeUpdate(Cancel As Integer)
Cancel = ValidationRFQDates(txtInDate)
End Sub
 
K

Klatuu

What is DateValidation?
You have it coded as if it is a function.
If so, where is the function. If it is in a standard module, it shoud be a
Public Function.

Also your Cancel = True is in the wrong place. It has to be in the Before
Update event code.
 
J

Jerry8989

Klatuu thank you for your reply.
DateValidation is a function I have in a module. All it does is take a take
and make sure it's within a range. What i'm trying to do it make 1 function
that I can pass the text box too and have the date checked and if it errors
out to undo the changes and cancel the action. I have 30 different date
fields so I thought passing in the actual text box would allow me to check
the date and do all the other stuff in one place. I didn't want to copy the
"Cancel = True" and the textbox.undo for every BeforeUpdate for ever text
box.

Thanks again for any help
Jerry
 
J

Jerry8989

Marsh Thank you for your tips. That makes sense. My only question is that
if Cancel is set to true will it know which text box i'm cancelling or will
it just cancel whatever the current change was?

Thank You again
 
M

Marshall Barton

Cancel applies to the BeforeUpdate event and if the event is
cancelled, then the event's control (txtInDate) can not be
updated.
 
K

Klatuu

No, the Cancel argument can't be seen outside of the procedure it is in.
What you can do is modify your code so it return the correct cancel value.

Cancel = DateValidation(Me.txtSomeDateControl)

Or you could use:
Cancel = DateValidation()

Then in the DateValidation code, you can reference which text box it is
being called from with:

Screen.ActiveControl
 
J

Jerry8989

Marshall & Klatuu,
Thank you both for your replies I followed what you said and I figured it
out. I used the function and the cancel is within each beforeupdate event.

Thanks again
Jerry

Marshall Barton said:
Cancel applies to the BeforeUpdate event and if the event is
cancelled, then the event's control (txtInDate) can not be
updated.
--
Marsh
MVP [MS Access]

Marsh Thank you for your tips. That makes sense. My only question is that
if Cancel is set to true will it know which text box i'm cancelling or will
it just cancel whatever the current change was?

Thank You again
 

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

Top