Using Access 2000, OS = Win2KPro
The answer to this is probably simple, but I'm stuck on figuring this out . I have a form with several textbox controls in it that allow a user to input values. Prior to allowing a user to submit their entries in the form to a table, I want to check that they have entered something in at least one of the textboxes. So I wrote the following to loop through all of the textbox controls to check their values. I'm testing tihs code with all of the textbox fields empty on the form.
But I'm getting, "Invalid use of Null", and, "Object Required", errors when I attempt to get the control's value. Does anyone know what's wrong here? I have 'ctl2.Value' as a watch expression and the value in the Watches windows shows as Null when the textbox is empty.
Code: (showing only the part to check for empty entries)
---------------------------------------------------------------------------------
Option Compare Database
Private Sub cmd_AddNewRecord_Click()
On Error GoTo Err_cmd_AddNewRecord_Click
Dim mydb As Database, myrset As Recordset, Numworked, i As Integer
Dim ctl As Control, currentform As Form, ctlvalue As String
Dim ctl2 As TextBox
Set currentform = Forms!MyCurrentForm
i = 0 ' i is counter for number of controls, used to check other stuff later
For Each ctl2 In currentform
MsgBox ctl2.ControlName
MsgBox ctl2.Value 'Get invalid use of Null at this line
'Get Object Required error in IF statement
If Trim(ctl2.Value) = "" Or ctl2.Value Is Null Then
i = i + 1
End If
Next
MsgBox i
Exit_cmd_AddNewRecord_Click:
Exit Sub
Err_cmd_AddNewRecord_Click:
MsgBox err.Description
Resume Exit_cmd_AddNewRecord_Click
End Sub
The answer to this is probably simple, but I'm stuck on figuring this out . I have a form with several textbox controls in it that allow a user to input values. Prior to allowing a user to submit their entries in the form to a table, I want to check that they have entered something in at least one of the textboxes. So I wrote the following to loop through all of the textbox controls to check their values. I'm testing tihs code with all of the textbox fields empty on the form.
But I'm getting, "Invalid use of Null", and, "Object Required", errors when I attempt to get the control's value. Does anyone know what's wrong here? I have 'ctl2.Value' as a watch expression and the value in the Watches windows shows as Null when the textbox is empty.
Code: (showing only the part to check for empty entries)
---------------------------------------------------------------------------------
Option Compare Database
Private Sub cmd_AddNewRecord_Click()
On Error GoTo Err_cmd_AddNewRecord_Click
Dim mydb As Database, myrset As Recordset, Numworked, i As Integer
Dim ctl As Control, currentform As Form, ctlvalue As String
Dim ctl2 As TextBox
Set currentform = Forms!MyCurrentForm
i = 0 ' i is counter for number of controls, used to check other stuff later
For Each ctl2 In currentform
MsgBox ctl2.ControlName
MsgBox ctl2.Value 'Get invalid use of Null at this line
'Get Object Required error in IF statement
If Trim(ctl2.Value) = "" Or ctl2.Value Is Null Then
i = i + 1
End If
Next
MsgBox i
Exit_cmd_AddNewRecord_Click:
Exit Sub
Err_cmd_AddNewRecord_Click:
MsgBox err.Description
Resume Exit_cmd_AddNewRecord_Click
End Sub