P
Peg via AccessMonster.com
I have read the posts on passing a control to a function. As long as my
control has some data in it, the function works fine. If my control is null
I get a message "You tried to assign a null value to a variable that is not a
variant data type".
I have tried to set the value of the text control to "" before passing it,
but I still get the error. Any suggestions?
Here is the code:
Private Sub txtAddr1_BeforeUpdate(Cancel As Integer)
If Nz(Me.txtAddr1, "") = "" Then
Me.txtAddr1 = ""
End If
Cancel = Check_Length(Me.txtAddr1, mlngAddr1_Lngth, True)
End Sub
Function Check_Length(txtCtrl As TextBox, lngLngth As Long, _
bln0Lngth_allowed As Boolean) As Boolean
'*****************************************************************************
'* Function: Check_Length
*
'* Purpose: Displays a message if the field length is too long or 0.
*
'* Returns true if error found.
*
'*****************************************************************************
Check_Length = False
If Len(Nz(txtCtrl.Text, "")) > lngLngth Then
MsgBox "Only " & lngLngth & " characters are allowed." & vbCrLf _
& "Remove " & (Len(txtCtrl.Text) - lngLngth) & " characters." _
, vbExclamation + vbApplicationModal _
, "Too Many Characters"
Check_Length = True
txtCtrl.BackColor = vbRed
ElseIf Len(Nz(txtCtrl.Text, "")) = 0 And bln0Lngth_allowed = False Then
MsgBox "Field cannot be blank." & vbCrLf _
, vbExclamation + vbApplicationModal _
, "Blank Field Invalid"
Check_Length = True
txtCtrl.BackColor = vbRed
Else
txtCtrl.BackColor = vbWhite
End If
End Function
control has some data in it, the function works fine. If my control is null
I get a message "You tried to assign a null value to a variable that is not a
variant data type".
I have tried to set the value of the text control to "" before passing it,
but I still get the error. Any suggestions?
Here is the code:
Private Sub txtAddr1_BeforeUpdate(Cancel As Integer)
If Nz(Me.txtAddr1, "") = "" Then
Me.txtAddr1 = ""
End If
Cancel = Check_Length(Me.txtAddr1, mlngAddr1_Lngth, True)
End Sub
Function Check_Length(txtCtrl As TextBox, lngLngth As Long, _
bln0Lngth_allowed As Boolean) As Boolean
'*****************************************************************************
'* Function: Check_Length
*
'* Purpose: Displays a message if the field length is too long or 0.
*
'* Returns true if error found.
*
'*****************************************************************************
Check_Length = False
If Len(Nz(txtCtrl.Text, "")) > lngLngth Then
MsgBox "Only " & lngLngth & " characters are allowed." & vbCrLf _
& "Remove " & (Len(txtCtrl.Text) - lngLngth) & " characters." _
, vbExclamation + vbApplicationModal _
, "Too Many Characters"
Check_Length = True
txtCtrl.BackColor = vbRed
ElseIf Len(Nz(txtCtrl.Text, "")) = 0 And bln0Lngth_allowed = False Then
MsgBox "Field cannot be blank." & vbCrLf _
, vbExclamation + vbApplicationModal _
, "Blank Field Invalid"
Check_Length = True
txtCtrl.BackColor = vbRed
Else
txtCtrl.BackColor = vbWhite
End If
End Function