Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Click a Button to check a form

Status
Not open for further replies.

Bill4tektips

Technical User
Aug 5, 2005
175
0
0
GB
I have a Form and I would like to check if certain criteria is filled in before it can be closed.
The coding I have put in is:
------------------------------------------------
Private Sub CloseFinding_Click()
On Error GoTo Err_CloseFinding_Click
Dim strUserName As String
strUserName = Environ("UserName")

If ([NCRRD] = "") Then MsgBox "Complete Response Required by Date", vbCritical, "Error Message - '" & strUserName & "'"
Else
If ([ResponseReceived] = "") Then MsgBox "Complete Response Received Date", vbCritical, "Error Message - '" & strUserName & "'"
Else
If ([ActionCompleteDate] = "") Then MsgBox "Complete Action Completed Date", vbCritical, "Error Message - '" & strUserName & "'"
Else
If ([LatestPCD] = "") Then MsgBox "Complete Latest PCD", vbCritical, "Error Message - '" & strUserName & "'"
Else
If ([PCD] = "") Then MsgBox "Complete PCD", vbCritical, "Error Message - '" & strUserName & "'"
Else
If ([RootCauseCode] = "") Then MsgBox "Complete Root Cause Code", vbCritical, "Error Message - '" & strUserName & "'"
Else
If ([Function] = "") Then MsgBox "Complete Function", vbCritical, "Error Message - '" & strUserName & "'"
End If

If ([NCRRD] <> "" And [ResponseReceived] <> "" And [ActionCompleteDate] <> "" And [LatestPCD] <> "" And [PCD] <> "" And [RootCauseCode] <> "" And [Function] <> "") Then
[Status] = "C"
End If


If ([NCRRD] <> "" And [ResponseReceived] <> "" And [ActionCompleteDate] <> "" And [LatestPCD] <> "" And [PCD] <> "" And [RootCauseCode] <> "" And [Function] <> "") Then
MsgBox "Thank you for updating the database!"
End If

Exit_CloseFinding_Click:
Exit Sub

Err_CloseFinding_Click:
MsgBox Err.Description
Resume Exit_CloseFinding_Click
End Sub
-----------------------------------
but when I run it I get an error message "Compile Error Else without If" and the first "Else" is highlighted. Can anyone see what I am doing wrong please?
 
If ([NCRRD] = "") Then MsgBox "Complete Response Required by Date", vbCritical, "Error Message - '" & strUserName & "'"
ElseIf ([ResponseReceived] = "") Then MsgBox "Complete Response Received Date", vbCritical, "Error Message - '" & strUserName & "'"
ElseIf ([ActionCompleteDate] = "") Then MsgBox "Complete Action Completed Date", vbCritical, "Error Message - '" & strUserName & "'"
ElseIf ([LatestPCD] = "") Then MsgBox "Complete Latest PCD", vbCritical, "Error Message - '" & strUserName & "'"
ElseIf ([PCD] = "") Then MsgBox "Complete PCD", vbCritical, "Error Message - '" & strUserName & "'"
ElseIf ([RootCauseCode] = "") Then MsgBox "Complete Root Cause Code", vbCritical, "Error Message - '" & strUserName & "'"
ElseIf ([Function] = "") Then MsgBox "Complete Function", vbCritical, "Error Message - '" & strUserName & "'"
End If

If ([NCRRD] <> "" And [ResponseReceived] <> "" And [ActionCompleteDate] <> "" And [LatestPCD] <> "" And [PCD] <> "" And [RootCauseCode] <> "" And [Function] <> "") Then
[Status] = "C"
End If


I think that the problem is the elseif that you have in your code. Try it like I've typed it above, and see if that works.

 
MellisaKT I am still getting the same Error message "Compile Error Else without If
 
This is a good chance to use the tag property and simplify your code
In the tag property of controls to validate I put the common name of the control. Then I check all controls to see if they have something in the tag property. In this example I limited it to only textbox for demonstration. Then I check if the control is null, empty, or an empty string.

Code:
Private Sub Command11_Click()
 Dim strUserName As String
 Dim cntl As Access.Control
 strUserName = Environ("UserName")
 For Each cntl In Me.Controls
   If (cntl.ControlType = acTextBox) And Not (cntl.Tag = "") Then
     If Trim(cntl.Value & " ") = "" Then
       MsgBox "Complete " & cntl.Tag, vbCritical, "Error Message - '" & strUserName & "'"
       Exit Sub
     End If
   End If
 Next cntl
 'Me.status = "C"
 MsgBox "Thank you for updating the data base"
End Sub
 
MajP
Thanks for the suggestion but even with all the fields empty all I get using your code is "Thank you for updating the data base". Unfortunately I am not experienced enough in vb to see what the problem is. The majority of fields that I need to check are Date fields but there are some text fields as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top