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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Message In Statement

Status
Not open for further replies.

gator9

Technical User
May 17, 2002
162
US
Hey guy! The code below works great but when none of the criteria meets it shows the form, but shows it blank. What I would like is for it to prompt with a message and a button to close the message box without opening the form if the criteria doesn’t meet, any help?

SELECT DISTINCTROW MDraft.*
FROM MDraft
WHERE (((MDraft.DraftStartDate)<Date()+1) AND ((MDraft.MonthlyDraftYes)=Yes));


Sincerely,

Charles
 
How are ya gator9 . . . . .

Try this:
Code:
[blue]   Dim Criteria As String, Msg As String
   Dim Style As Integer, Title As String
   
   Criteria = "WHERE ([DraftStartDate] < Date()+1) AND " & _
                    "([MonthlyDraftYes] = Yes"
   
   If IsNull(DLookup("[DraftStartDate]", "MDraft", Criteria)) Then
      Msg = "No Records Match Criteria!"
      Style = vbInformation + vbOKOnly
      Title = "No Matching Records Notice . . ."
      MsgBox Msg, Style, Title
   Else
      DoCmd.OpenForm "[purple][b]YourFormName[/b][/purple]"
   End If[/blue]

Calvin.gif
See Ya! . . . . . .
 
Sure . . . with an added line, add the following to the [blue]On Open Event[/blue] of the form:
Code:
[blue]   Dim Criteria As String, Msg As String
   Dim Style As Integer, Title As String
 
   Criteria = "WHERE ([DraftStartDate] < Date()+1) AND " & _
                    "([MonthlyDraftYes] = Yes"
   
   If IsNull(DLookup("[DraftStartDate]", "MDraft", Criteria)) Then
      Msg = "No Records Match Criteria!"
      Style = vbInformation + vbOKOnly
      Title = "No Matching Records Notice . . ."
      MsgBox Msg, Style, Title
      [b]Cancel = True[/b]
   Else
      DoCmd.OpenForm "[purple][b]YourFormName[/b][/purple]"
   End If[/blue]

Calvin.gif
See Ya! . . . . . .
 
Still getting an error but I do see what your shooting for.

If IsNull(DLookup("[DraftStartDate]", "MDraft", Criteria)) <--- Error in above line

Sincerely,

Charles
 
Hmmmmm . . . missing a closing parenthese:
Code:
[blue]   Criteria = "WHERE ([DraftStartDate] < Date()+1) AND " & _
                    "([MonthlyDraftYes] = Yes[purple][b])[/b][/purple]"[/blue]

Calvin.gif
See Ya! . . . . . .
 
Here is the exact error:

Run-time error '3075':


Missing ), ], or Item in query expression 'WHERE ([DraftStartDate]< Date()+1) AND ([MonthlyDraftYes] = Yes'.

Sincerely,

Charles
 
Ok Got it, but if you don't bound the record source as it is below it will open all the records once the criteria meets. Oh and I took out the where because I kept getting an error and it worked great.

Record Source =
----
SELECT DISTINCTROW MDraft.*
FROM MDraft
WHERE (((MDraft.DraftStartDate)<Date()+1) AND ((MDraft.MonthlyDraftYes)=Yes));



On Open (On The Form) =
----
Dim Criteria As String, Msg As String
Dim Style As Integer, Title As String

Criteria = "([DraftStartDate] < Date()+1) AND " & _
"([MonthlyDraftYes] = Yes)"

If IsNull(DLookup("[DraftStartDate]", "MDraft", Criteria)) Then
Msg = "No Records Match Criteria!"
Style = vbInformation + vbOKOnly
Title = "No Matching Records Notice . . ."
MsgBox Msg, Style, Title
Cancel = True
Else
DoCmd.OpenForm "ReDraft"
End If

Sincerely,

Charles
 
gator9 . . . .

Yes your right. DLookup uses criteria without the where! . . . . I must be tired.

[blue]Cheers![/blue]

Calvin.gif
See Ya! . . . . . .
 
Below is another way that seems to work pretty good and is easer not to make a mistake in having both the record source and the on open the same: Thanks for your help.

Record Source =
----
SELECT DISTINCTROW MDraft.*
FROM MDraft
WHERE (((MDraft.DraftStartDate)<Date()+1) AND ((MDraft.MonthlyDraftYes)=Yes));



On Open (On The Form) =
----
If (IsNull(Me.EstStartDate = Null)) Then
Msg = "No Records Match Criteria!"
Style = vbInformation + vbOKOnly
Title = "No Matching Records Notice . . ."
MsgBox Msg, Style, Title
Cancel = True
Else
DoCmd.OpenForm "RePlan"
End If




Sincerely,

Charles
 
gator9 . . . . .
Code:
[blue]If (IsNull([purple][b]Me.EstStartDate = Null[/b][/purple])) Then[/blue]
Your assigning a [blue]null[/blue] to [blue]Me.EstStartDate[/blue], which always makes [blue]IsNull()[/blue] true! This is pointless. You'll get the message everytime you open the form . . . . no matter what.

It also does not take into account the [blue]specific criteria[/blue] you desired to ping against.

Calvin.gif
See Ya! . . . . . .
 
Why not simply playing with Me.Recorset.RecordCount ?
I surely have missed something.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
How are ya PHV . . . . an honor to have you stop by!

From the post origination:
[blue]What I would like is for it to prompt with a message and a button to close the message box [purple]without opening the form if the criteria doesn’t meet[/purple],[/blue]

Calvin.gif
See Ya! . . . . . .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top