Closing form in subprocedure

P

Pele

Below is a code that I need modified.

Can anybody tell me what changes I need to make to this
code to make it CLOSE the form and STOP a macro INSTEAD of
displaying a message box like it presently does.

Basically, I want the code to close the form instead of
displaying the message. Any help will be appreciated.

Pele


Public Sub Arethereanyrecords(frmName As Form)
If frmName.RecordsetClone.RecordCount = 0 Then
MsgBox "Your query request has returned no records....You
will need to increase your query limit.",
vbInformation, "No Records in Query"
End If
End Sub

Public Function fnArethereanyrecords(frmName As Form)
Call Arethereanyrecords(frmName)
End Function
 
S

Steve Schapel

Pele,

Private Sub Form_Load()
If Me.RecordsetClone.RecordCount = 0 Then
DoCmd.Close acForm, Me.Name
End If
End Sub

- Steve Schapel, Microsoft Access MVP
 
P

Pele

Steve,

Below is how I tried to use your code but Access said it
is an invalid use of Me. What should I do.

I called the function in a macro I had used to open a form.

Pele

Private Sub CloseFormWithNoRecords()
If Me.RecordsetClone.RecordCount = 0 Then
DoCmd.Close acForm, Me.Name
End If
End Sub

Public Function fnCloseFormWithNoRecords()
Call CloseFormWithNoRecords
End Function
-----Original Message-----
Pele,

Private Sub Form_Load()
If Me.RecordsetClone.RecordCount = 0 Then
DoCmd.Close acForm, Me.Name
End If
End Sub

- Steve Schapel, Microsoft Access MVP
 
S

Steve Schapel

Pele,

As per my earlier post, use the form's OnLoad event.

I can't really see the purpose behind the way you are doing it, with a
function defined as calling a procedure. But in any case, you can't
close a form until it's open, and you can't measure its Recordset
until it's loaded. If you want to prevent the form opening if there
are no records, then you can use a DCount() function to see if there
are any records in the form's underlying query, in the procedure (I
doubt very much that you're using a macro!) which you use to open the
form, for example...

Private Sub YourButton_Click()
If DCount("*","NameOfYourQuery")>0 Then
DoCmd.OpenForm "NameOfYourForm"
End If

- Steve Schapel, Microsoft Access MVP


Steve,

Below is how I tried to use your code but Access said it
is an invalid use of Me. What should I do.

I called the function in a macro I had used to open a form.

Pele

Private Sub CloseFormWithNoRecords()
If Me.RecordsetClone.RecordCount = 0 Then
DoCmd.Close acForm, Me.Name
End If
End Sub

Public Function fnCloseFormWithNoRecords()
Call CloseFormWithNoRecords
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top