Subform recordset

J

jnew

Greetings,

I have a form with a subform. The subform has custom navigation buttons,
i.e., "Previous Record", "Next Record", etc. I am using the subform's
onCurrent event property to enable/disable the navigation buttons with the
Recordsetclone property depending on the current record. Such as:
cmdPrevRec.Enabled = Me.CurrentRecord > 1

Here's the gotcha - the subform is not populated with data until the user
selects a value from a combo box on the main form. The subform's recordset
clone, therefore, has no data when the user opens the main form, triggering
an error in the code to enable/disable the subform's navigation buttons (Run
Time Error: '3021', to be exact). How can I trap or prevent this error from
occuring?

Any help will definitely be appreciated.

jn
 
K

Ken Snell [MVP]

Post the code that you're using for your navigation buttons. That should
allow us the chance to see where error handling is best added.
 
J

jnew

Ken,

Thanks for looking at this. Here's the code:

Private Sub Form_Current()

With Me.RecordsetClone
.MoveLast
cmdNextRecord.Enabled = Me.CurrentRecord < .RecordCount + 1
cmdPreviousRecord.Enabled = Me.CurrentRecord > 1
cmdFirstRecord.Enabled = Me.CurrentRecord > 1
cmdLastRecord.Enabled = Me.CurrentRecord < .RecordCount + 1
End With
End Sub
 
P

PC Datasheet

Try this code:
Dim Rst As DAO.Recordset
Set Rst = Me.RecordsetClone
cmdPrevRec.Enabled = Not Rst.BOF
 
J

jnew

PC Datasheet,

Thanks for the input. Unfortunately, I'm unable to get this to work. Is it
possible that's because I am working with a subform inside a main form with
similar code? In other words, can I use "Me.RecordsetClone" in both the main
form and subform simultaneously?

jn
 
P

PC Datasheet

To answer your last sentence, Yes!

Try changing to this:
Dim Rst As DAO.Recordset
Set Rst = Me.RecordsetClone
If Rst.Recordcount = 0 Then
cmdPrevRec.Enabled = False
Else
cmdPrevRec.Enabled = Not Rst.BOF
End If

Steve
PC Datasheet
 
K

Ken Snell [MVP]

Try this:

With Me.RecordsetClone
If .RecordCount <> 0 Then
.MoveLast
cmdNextRecord.Enabled = Me.CurrentRecord < .RecordCount + 1
cmdPreviousRecord.Enabled = Me.CurrentRecord > 1
cmdFirstRecord.Enabled = Me.CurrentRecord > 1
cmdLastRecord.Enabled = Me.CurrentRecord < .RecordCount + 1
End If
End With
End Sub
 

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