Re: Open A reprt with Conditions from a subform

Tech-Archive recommends: Fix windows errors by optimizing your registry



Alvin,

The general concept you are aiming at is *almost* there. I think it would work like this...

Private Sub Report_Open(Cancel As Integer)
If IsNull(Forms!Breeders![Breeders subform].Form!MatingOrderID) Then
            MsgBox "You Have Chosen a Mating Order That Has No Value. " &
Chr(13) & Chr(10) & Chr(10) & "You Must Select A Female to Breed With ( " &
[Form_Breeders]![Name] & " ) before you can View This Report! ",
vbInformation, "Breed Manager"
    Cancel = -1
ElseIf IsNull(Forms!Breeders![Breeders subform].Form![BreadFemale]) Then
            MsgBox "You Must Choose A Female To Be Bred! " & Chr(13) &
Chr(10) & Chr(10) & "You Have Chosen ( " & [Breeders
subform].Form![BreadFemale].Column(1) & " ) To Breed With ( " &
[Form_Breeders]![Name] & " ) You Must Now Enter a Breed Date! ",
vbInformation, "Breed Manager"
    Cancel = -1
End If
End Sub

However, a few comments that are hpefully helpful...

 - I am surprised that this section of your code works:
 ..To Breed With ( " & [Form_Breeders]![Name] & " )
I would have expected you to need Forms![Form_Breeders]![Name]

- In any case, Name is a Reserved Word (i.e. has a special meaning) in Access, and it is strongly recommended not to use it as the name of a field or control or database object.

- In code, I think it is preferable to use vbCrLf in the place of Chr(13) & Chr(10)

- Instead of waiting for the Open event of the report, I would do this validation on the event that triggers the opening of the report. For example, from what you have told us so far, I would imagine there is a command button that you click on the Breeders form to run the report, or some other form-based event to run the report... am I right? If so, you could do like this...

Private Sub YourButton_Click()
If IsNull(Me![Breeders subform].Form!MatingOrderID) Then
MsgBox "You Have Chosen a Mating Order That Has No Value." & vbCrLf & "You Must Select A Female to Breed With ( " & Me![TheName] & ") before you can View This Report!", vbInformation, "Breed Manager"
ElseIf IsNull(Me![Breeders subform].Form![BreadFemale]) Then
MsgBox "You Must Choose A Female To Be Bred! " & vbCrLf & "You Have Chosen ( " & Me![Breeders subform].Form![BreadFemale].Column(1) & ") To Breed With ( " & Me![TheName] & ") You Must Now Enter a Breed Date!", vbInformation, "Breed Manager"
Else
DoCmd.OpenReport "YourReport"
End If
End Sub


--
Steve Schapel, Microsoft Access MVP


Alvin wrote:
I have the following code in the On Open Event of a Report. I am new to VB Code but am learning. I can get one condition to work when I use it Like this.

----------- It Works Like This---------------

Private Sub Report_Open(Cancel As Integer)
If IsNull(Forms!Breeders![Breeders subform].Form!MatingOrderID) Then
MsgBox "You Have Chosen a Mating Order That Has No Value. " & Chr(13) & Chr(10) & Chr(10) & "You Must Select A Female to Breed With ( " & [Form_Breeders]![Name] & " ) before you can View This Report! ", vbInformation, "Breed Manager"
Cancel = -1
Else
End If
End Sub
---------------------------------------------------------------------------------------


but I don't know how to add the second or third. Here are the conditions and actual names I want to use something Like the following where I can Make sure needed field have been entered before viewing the Report.

----------------This way I can't get it to work---------------------

Private Sub Report_Open(Cancel As Integer)
If IsNull(Forms!Breeders![Breeders subform].Form!MatingOrderID) Then
MsgBox "You Have Chosen a Mating Order That Has No Value. " & Chr(13) & Chr(10) & Chr(10) & "You Must Select A Female to Breed With ( " & [Form_Breeders]![Name] & " ) before you can View This Report! ", vbInformation, "Breed Manager"
Cancel = -1
Else
If IsNull(Forms!Breeders![Breeders subform].Form![BreadFemale]) Then
MsgBox "You Must Choose A Female To Be Bred! " & Chr(13) & Chr(10) & Chr(10) & "You Have Chosen ( " & [Breeders subform].Form![BreadFemale].Column(1) & " ) To Breed With ( " & [Form_Breeders]![Name] & " ) You Must Now Enter a Breed Date! ", vbInformation, "Breed Manager"
Cancel = -1
Else
End If
End If
End Sub


----------------------------------------------------------------

Thanks in Advance
Alvin Smith
.


Quantcast