[AccessD] Request for help with Function

jwcolby jwcolby at colbyconsulting.com
Wed Jan 14 07:49:54 CST 2009


It sounds like your problem is the "activecontrol" part.  I 
am guessing that the focus leaves the control and is placed 
on the command button BEFORE the BeforeUpdate runs and thus 
the command button is being validated, which would not be 
correct.

Pass in the control itself, then inside of the function look 
at the control.value.

function Validate(ctl as control)
	if ctl.value = XXX then
		Do something
	else
		so something else
	endif
end function

Call that from the Before update.

	if Validate(ThisCtl) then
	else
	endif

Doing it this way leaves no ambiguity as to what control is 
being validated.

John W. Colby
www.ColbyConsulting.com


Bob Gajewski wrote:
> Hello Friends
>  
> I have a form that contains 184 one-character text fields (EventSeatA001,
> EventSeatA002, etc) - seats in an auditorium. All fields are defaulted to
> "A".
>  
> I need to check the value that the user inputs, and if an invalid character
> was entered, I want to display an error message and return the field to the
> value from before it was changed.
>  
> I have the following code in the _BeforeUpdate CBF for each of the 184
> fields, and it works just fine.
>  
> Code Sample 1
> ===============================
> If Me.ActiveControl <> "A" And Me.ActiveControl <> "R" And Me.ActiveControl
> <> "C" And Me.ActiveControl <> "P" And Me.ActiveControl <> "S" And
> Me.ActiveControl <> "N" Then
>     MsgBox "The valid choices are 'A' (available), 'R' (reserved), 'C'
> (chorus/cast), 'P' (pre-sale), 'S' (sold), and 'N' (not available)."
>     Cancel = True
>     Me.ActiveControl.Undo
> End If
> 
> ===============================
>  
> However, I would prefer to put this validation into a Function and just call
> it in each _BeforeUpdate. This would make maintenance SO much easier. This
> is where I am struggling. I wrote the Function code and made the call in the
> _BeforeUpdate ...
>  
> Code Sample 2
> ===============================
> Private Function isValidStatus()
> Dim Cancel As Integer
> If Me.ActiveControl <> "A" And Me.ActiveControl <> "R" And Me.ActiveControl
> <> "C" And Me.ActiveControl <> "P" And Me.ActiveControl <> "S" And
> Me.ActiveControl <> "N" Then
>     MsgBox "The valid choices are 'A' (available), 'R' (reserved), 'C'
> (chorus/cast), 'P' (pre-sale), 'S' (sold), and 'N' (not available)."
>     Cancel = True
>     Me.ActiveControl.Undo
> End If
> End Function
> ===============================
>  
> Code Sample 3
> ===============================
> Private Sub EventSeatA001_BeforeUpdate(Cancel As Integer)
> isValidStatus (Me.ActiveControl.Value)
> End Sub
> ===============================
>  
> ... and the validation works fine, but as soon as I click on [OK] on the
> message box, I get a system error message:
>  
> Code Sample 4
> ===============================
> Run-time error 13:
>  
> Type mismatch
> ===============================
>  
> If anyone can point me in the right direction, I would very appreciative.
>  
> Thanks,
> Bob Gajewski
>  
>  
> P Please consider the environment before printing this e-mail 
>  



More information about the AccessD mailing list