A form, which has a subform, to edit donations from church donors.
In a form, the user can enter a number in a text box. If the number is valid, the subform is populated with data.
What I want to do is have a procedure so that if the number entered by the user is not valid, a message box comes up.
Below is the entire code.
If the number the user enters in the text box is valid, the subform populates with no problem.
If the number entered in the text box does not exist in sql2, see the DLookup line, the result is Error code 94, invalid use of Null.
Can you suggest a correction to the code? Or a better method?
Thanks.
Tom
In a form, the user can enter a number in a text box. If the number is valid, the subform is populated with data.
What I want to do is have a procedure so that if the number entered by the user is not valid, a message box comes up.
Below is the entire code.
If the number the user enters in the text box is valid, the subform populates with no problem.
If the number entered in the text box does not exist in sql2, see the DLookup line, the result is Error code 94, invalid use of Null.
Code:
Private Sub txtEnvNbr_AfterUpdate()
On Error GoTo Err_txtEnvNbr_AfterUpdate_Error
Dim sql1 As String
Dim sql2 As String
sql1 = "SELECT EnvNbr, [Date Given], Local, [M and S], Building, Memorial, Other, InMemoryOf, ToFund " _
& " FROM tblNewGivings " _
& " WHERE EnvNbr = " & Forms!frmEditNewGivings!txtEnvNbr _
& " ORDER BY EnvNbr, [Date Given]"
sql2 = DLookup("EnvNbr", "qryNewGivings", "EnvNbr = Forms!frmEditNewGivings!txtEnvNbr")
If sql2 <> Me.txtEnvNbr Then
Call MsgBox("Envelope # " & Forms!frmEditNewGivings!txtEnvNbr & " is not assigned." _
& vbCrLf & "Please re-enter another number." _
, vbExclamation)
Exit Sub
Me.txtEnvNbr.SetFocus
Else
Me.fsubNewGivings.Visible = True
Me.fsubNewGivings.Form.RecordSource = sql1
End If
On Error GoTo 0
Exit Sub
Err_txtEnvNbr_AfterUpdate_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure txtEnvNbr_AfterUpdate of VBA Document Form_frmEditNewGivings"
End Sub
Can you suggest a correction to the code? Or a better method?
Thanks.
Tom