Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old November 13th, 2005, 09:58 AM
highway of diamonds
Guest
 
Posts: n/a
Default close form if query returns no records

I have a form based on a query. I would like to generate a msgbox and
close the form should the query return no records. [or not open the form
at all, either would be good]

TIA

R.

PS I would even be happy to lose the form and just do the same with the
query in datasheet view.
  #2  
Old November 13th, 2005, 09:59 AM
pietlinden@hotmail.com
Guest
 
Posts: n/a
Default Re: close form if query returns no records

normally, you'd trap for the NoData event of a report, but alas, this
isn't a report. I did get this to work, but I'd recommend that any
self-respecting Access programmer finish swallowing before reading
futher...

This works...

Private Sub Form_Load()
Dim rs As DAO.Recordset
Dim intRecords As Integer

Set rs = Me.RecordsetClone
'---you shouldn't need these two lines. You'll get 1 if there are
any records.
rs.MoveLast
rs.MoveFirst

If rs.RecordCount =0 Then
rs.Close
Set rs = Nothing
DoCmd.Close acForm, Me.Name, acSaveNo
Else
MsgBox rs.RecordCount & " records."
rs.Close
Set rs = Nothing
End If

End Sub

  #3  
Old November 13th, 2005, 09:59 AM
Jeff Conrad
Guest
 
Posts: n/a
Default Re: close form if query returns no records

Even shorter:

Private Sub Form_Open(Cancel As Integer)
If Me.RecordsetClone.RecordCount = 0 Then
MsgBox "There are no records to display at this time." _
, vbInformation, "No Records"
Cancel = True
End If
End Sub

You will of course need to trap for and ignore Error 2501
in the code rountine that opens the form.

--
Jeff Conrad
Access Junkie
Bend, Oregon

<pietlinden@hotmail.com> wrote in message
news:1113623069.185302.163790@l41g2000cwc.googlegr oups.com...
[color=blue]
> normally, you'd trap for the NoData event of a report, but alas, this
> isn't a report. I did get this to work, but I'd recommend that any
> self-respecting Access programmer finish swallowing before reading
> futher...
>
> This works...
>
> Private Sub Form_Load()
> Dim rs As DAO.Recordset
> Dim intRecords As Integer
>
> Set rs = Me.RecordsetClone
> '---you shouldn't need these two lines. You'll get 1 if there are
> any records.
> rs.MoveLast
> rs.MoveFirst
>
> If rs.RecordCount =0 Then
> rs.Close
> Set rs = Nothing
> DoCmd.Close acForm, Me.Name, acSaveNo
> Else
> MsgBox rs.RecordCount & " records."
> rs.Close
> Set rs = Nothing
> End If
>
> End Sub[/color]



----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
  #4  
Old November 13th, 2005, 09:59 AM
Tom Travolta
Guest
 
Posts: n/a
Default Re: close form if query returns no records

"Jeff Conrad" <jeffc@ernstbrothers.com> wrote in message
news:42608d09$1_2@127.0.0.1...[color=blue]
> Even shorter:
>
> Private Sub Form_Open(Cancel As Integer)
> If Me.RecordsetClone.RecordCount = 0 Then
> MsgBox "There are no records to display at this time." _
> , vbInformation, "No Records"
> Cancel = True
> End If
> End Sub
>
> You will of course need to trap for and ignore Error 2501
> in the code rountine that opens the form.
>
> --
> Jeff Conrad[/color]


Even shorter:

Private Sub Form_Open(Cancel As Integer)
If Me.RecordsetClone.RecordCount = 0 Then
MsgBox "There are no..."
Cancel = True
End If
End Sub


  #5  
Old November 13th, 2005, 09:59 AM
highway of diamonds
Guest
 
Posts: n/a
Default Re: close form if query returns no records

Tom Travolta wrote:[color=blue]
> "Jeff Conrad" <jeffc@ernstbrothers.com> wrote in message
> news:42608d09$1_2@127.0.0.1...[color=green]
>> Even shorter:
>>
>> Private Sub Form_Open(Cancel As Integer)
>> If Me.RecordsetClone.RecordCount = 0 Then
>> MsgBox "There are no records to display at this time." _
>> , vbInformation, "No Records"
>> Cancel = True
>> End If
>> End Sub
>>
>> You will of course need to trap for and ignore Error 2501
>> in the code rountine that opens the form.
>>
>> --
>> Jeff Conrad[/color]
>
>
> Even shorter:
>
> Private Sub Form_Open(Cancel As Integer)
> If Me.RecordsetClone.RecordCount = 0 Then
> MsgBox "There are no..."
> Cancel = True
> End If
> End Sub
>
>[/color]
Ok, thanks folks. I'll have to digest and try these. My VBA skills
aren't that good and I was hoping to do it with macro or form properties
if I could.

R.
  #6  
Old November 13th, 2005, 09:59 AM
highway of diamonds
Guest
 
Posts: n/a
Default Re: close form if query returns no records

highway of diamonds wrote:[color=blue]
> Tom Travolta wrote:[color=green]
>> "Jeff Conrad" <jeffc@ernstbrothers.com> wrote in message
>> news:42608d09$1_2@127.0.0.1...[color=darkred]
>>> Even shorter:
>>>
>>> Private Sub Form_Open(Cancel As Integer)
>>> If Me.RecordsetClone.RecordCount = 0 Then
>>> MsgBox "There are no records to display at this time." _
>>> , vbInformation, "No Records"
>>> Cancel = True
>>> End If
>>> End Sub
>>>
>>> You will of course need to trap for and ignore Error 2501
>>> in the code rountine that opens the form.
>>>
>>> --
>>> Jeff Conrad[/color]
>>
>>
>> Even shorter:
>>
>> Private Sub Form_Open(Cancel As Integer)
>> If Me.RecordsetClone.RecordCount = 0 Then
>> MsgBox "There are no..."
>> Cancel = True
>> End If
>> End Sub
>>
>>[/color]
> Ok, thanks folks. I'll have to digest and try these. My VBA skills
> aren't that good and I was hoping to do it with macro or form properties
> if I could.
>
> R.[/color]
Yes!!! works well, thanks very much.

I don't use Access much these days [or Vis Basic], this was a project I
was asked to do, got it up and running but small things like this make a
lot more user friendly. Need to keep my hand in I think.

R.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles