SetFocus -- Selecting a Control on a Nested Subform

L

lmcc007

When I TAB from the txtPhoneExt control of the subform (s_frmPhoneNumbers), I
want the next subform (Child22) control to become the active control. And If
I keep tabbing I want Child23, Child24, and then go to another subform
s_Addresses. I am doing this so I will not have to hit Control Tab all the
time.


Below is the code I am using:


Private Sub txtPhoneExt_Exit(Cancel As Integer)


Dim rs As Object
Set rs = Me.Recordset.Clone
rs.MoveLast
If StrComp(Me.Bookmark, rs.Bookmark, 0) = 0 Then
Forms![s_frmPhoneNumbers1]![Child22].SetFocus

End If
End Sub

It works on the first subform going to Child22, but then I get the following
error:

Runtime Error 3021:

No current record


I know I am missing something. I think it needs to loop or something until
I get to the last subform, which is Child24 and then jump to s_Addresses
after hitting tab, but I don’t know how to tell it to do that.

Help! Any suggestions?

Thanks!
 
L

lmcc007

I hope I am understanding the question. I am trying to do a GoToControl, but
I read that SetFocus was a better chose when moving between subforms.

I just want to be able to move from subform to subform without having to use
Control Tab. Let me explain, I have:

1. Main Form called mfrmCompanies and then I have a

2. Subform called sfrmPhoneNumbers and in this subform I a have four
subforms called:

a. sfrmPNBusiness
b. sfrmPNBusiness2
c. sfrmPNBusiness3
d. sfrmPNBusinessFax

I cannot tab out of these subforms unless I hit Control Tab.

3. Then back to main form with another subform call sfrmAddresses.


BruceS said:
Imcc007,

Assuming that all of the subforms and their record sources are properly
linked to the main form via common fields, why are you doing the record move?
Access should already have the proper record in the subform.

Just curious...

Bruce

lmcc007 said:
When I TAB from the txtPhoneExt control of the subform (s_frmPhoneNumbers), I
want the next subform (Child22) control to become the active control. And If
I keep tabbing I want Child23, Child24, and then go to another subform
s_Addresses. I am doing this so I will not have to hit Control Tab all the
time.


Below is the code I am using:


Private Sub txtPhoneExt_Exit(Cancel As Integer)


Dim rs As Object
Set rs = Me.Recordset.Clone
rs.MoveLast
If StrComp(Me.Bookmark, rs.Bookmark, 0) = 0 Then
Forms![s_frmPhoneNumbers1]![Child22].SetFocus

End If
End Sub

It works on the first subform going to Child22, but then I get the following
error:

Runtime Error 3021:

No current record


I know I am missing something. I think it needs to loop or something until
I get to the last subform, which is Child24 and then jump to s_Addresses
after hitting tab, but I don’t know how to tell it to do that.

Help! Any suggestions?

Thanks!
 
B

BruceS

Imcc007,

Assuming that all of the subforms and their record sources are properly
linked to the main form via common fields, why are you doing the record move?
Access should already have the proper record in the subform.

Just curious...

Bruce
 
J

Jack Leach

Forms![s_frmPhoneNumbers1]![Child22].SetFocus

I'm not sure how much difference it will make but you can try calling the
control out more specifically. Also, you may want to set the focus to a
control inside the subform rather than the subform control itself (I don't
know that a "subform control" can actually have the focus...).

Also, if AllowAdditions is off in a form that doesn't have any records yet,
you probably won't be able to set the focus to it.

Maybe this might work better (to get to a 2nd nested subform)

Forms("Mainform").Controls("subformcontrol1").Form.Controls("Subformcontrol2").ControlName.SetFocus

or

Forms![main]!SubControl1.Form!SubControl2.Form!Controlname.Setfocus


with this:
Forms![s_frmPhoneNumbers1]![Child22].SetFocus

you seem to be trying to directly refer to the subform without going through
the main form. Access (IME) has a funny way of dealing with subforms... I
always try to explicitly walk the line to it from the ground up. Not exactly
sure what that might have to do with a No Current Record error, but it may be
worth a shot.

hth
--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)



lmcc007 said:
I hope I am understanding the question. I am trying to do a GoToControl, but
I read that SetFocus was a better chose when moving between subforms.

I just want to be able to move from subform to subform without having to use
Control Tab. Let me explain, I have:

1. Main Form called mfrmCompanies and then I have a

2. Subform called sfrmPhoneNumbers and in this subform I a have four
subforms called:

a. sfrmPNBusiness
b. sfrmPNBusiness2
c. sfrmPNBusiness3
d. sfrmPNBusinessFax

I cannot tab out of these subforms unless I hit Control Tab.

3. Then back to main form with another subform call sfrmAddresses.


BruceS said:
Imcc007,

Assuming that all of the subforms and their record sources are properly
linked to the main form via common fields, why are you doing the record move?
Access should already have the proper record in the subform.

Just curious...

Bruce

lmcc007 said:
When I TAB from the txtPhoneExt control of the subform (s_frmPhoneNumbers), I
want the next subform (Child22) control to become the active control. And If
I keep tabbing I want Child23, Child24, and then go to another subform
s_Addresses. I am doing this so I will not have to hit Control Tab all the
time.


Below is the code I am using:


Private Sub txtPhoneExt_Exit(Cancel As Integer)


Dim rs As Object
Set rs = Me.Recordset.Clone
rs.MoveLast
If StrComp(Me.Bookmark, rs.Bookmark, 0) = 0 Then
Forms![s_frmPhoneNumbers1]![Child22].SetFocus

End If
End Sub

It works on the first subform going to Child22, but then I get the following
error:

Runtime Error 3021:

No current record


I know I am missing something. I think it needs to loop or something until
I get to the last subform, which is Child24 and then jump to s_Addresses
after hitting tab, but I don’t know how to tell it to do that.

Help! Any suggestions?

Thanks!
 
L

lmcc007

The code is working but it stops at rs.MoveLast. I don't know what
rs.MoveLast means. I copied from this code from examples given I found on
the Internet.

I thought this would be a simple question b/c when I hit Control + Tab it
jumps to the next field (control) in the subform and all I want to do is to
be able to hit Tab and go to the next field in the next subform.

Jack Leach said:
Forms![s_frmPhoneNumbers1]![Child22].SetFocus

I'm not sure how much difference it will make but you can try calling the
control out more specifically. Also, you may want to set the focus to a
control inside the subform rather than the subform control itself (I don't
know that a "subform control" can actually have the focus...).

Also, if AllowAdditions is off in a form that doesn't have any records yet,
you probably won't be able to set the focus to it.

Maybe this might work better (to get to a 2nd nested subform)

Forms("Mainform").Controls("subformcontrol1").Form.Controls("Subformcontrol2").ControlName.SetFocus

or

Forms![main]!SubControl1.Form!SubControl2.Form!Controlname.Setfocus


with this:
Forms![s_frmPhoneNumbers1]![Child22].SetFocus

you seem to be trying to directly refer to the subform without going through
the main form. Access (IME) has a funny way of dealing with subforms... I
always try to explicitly walk the line to it from the ground up. Not exactly
sure what that might have to do with a No Current Record error, but it may be
worth a shot.

hth
--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)



lmcc007 said:
I hope I am understanding the question. I am trying to do a GoToControl, but
I read that SetFocus was a better chose when moving between subforms.

I just want to be able to move from subform to subform without having to use
Control Tab. Let me explain, I have:

1. Main Form called mfrmCompanies and then I have a

2. Subform called sfrmPhoneNumbers and in this subform I a have four
subforms called:

a. sfrmPNBusiness
b. sfrmPNBusiness2
c. sfrmPNBusiness3
d. sfrmPNBusinessFax

I cannot tab out of these subforms unless I hit Control Tab.

3. Then back to main form with another subform call sfrmAddresses.


BruceS said:
Imcc007,

Assuming that all of the subforms and their record sources are properly
linked to the main form via common fields, why are you doing the record move?
Access should already have the proper record in the subform.

Just curious...

Bruce

:

When I TAB from the txtPhoneExt control of the subform (s_frmPhoneNumbers), I
want the next subform (Child22) control to become the active control. And If
I keep tabbing I want Child23, Child24, and then go to another subform
s_Addresses. I am doing this so I will not have to hit Control Tab all the
time.


Below is the code I am using:


Private Sub txtPhoneExt_Exit(Cancel As Integer)


Dim rs As Object
Set rs = Me.Recordset.Clone
rs.MoveLast
If StrComp(Me.Bookmark, rs.Bookmark, 0) = 0 Then
Forms![s_frmPhoneNumbers1]![Child22].SetFocus

End If
End Sub

It works on the first subform going to Child22, but then I get the following
error:

Runtime Error 3021:

No current record


I know I am missing something. I think it needs to loop or something until
I get to the last subform, which is Child24 and then jump to s_Addresses
after hitting tab, but I don’t know how to tell it to do that.

Help! Any suggestions?

Thanks!
 
L

lmcc007

I reworded it in hopes I a clear on what I am trying to do:

Press the TAB key or ENTER key to move from the last control on the last
record of a subform to another subform or the first control on another
subform.



lmcc007 said:
The code is working but it stops at rs.MoveLast. I don't know what
rs.MoveLast means. I copied from this code from examples given I found on
the Internet.

I thought this would be a simple question b/c when I hit Control + Tab it
jumps to the next field (control) in the subform and all I want to do is to
be able to hit Tab and go to the next field in the next subform.

Jack Leach said:
Forms![s_frmPhoneNumbers1]![Child22].SetFocus

I'm not sure how much difference it will make but you can try calling the
control out more specifically. Also, you may want to set the focus to a
control inside the subform rather than the subform control itself (I don't
know that a "subform control" can actually have the focus...).

Also, if AllowAdditions is off in a form that doesn't have any records yet,
you probably won't be able to set the focus to it.

Maybe this might work better (to get to a 2nd nested subform)

Forms("Mainform").Controls("subformcontrol1").Form.Controls("Subformcontrol2").ControlName.SetFocus

or

Forms![main]!SubControl1.Form!SubControl2.Form!Controlname.Setfocus


with this:
Forms![s_frmPhoneNumbers1]![Child22].SetFocus

you seem to be trying to directly refer to the subform without going through
the main form. Access (IME) has a funny way of dealing with subforms... I
always try to explicitly walk the line to it from the ground up. Not exactly
sure what that might have to do with a No Current Record error, but it may be
worth a shot.

hth
--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)



lmcc007 said:
I hope I am understanding the question. I am trying to do a GoToControl, but
I read that SetFocus was a better chose when moving between subforms.

I just want to be able to move from subform to subform without having to use
Control Tab. Let me explain, I have:

1. Main Form called mfrmCompanies and then I have a

2. Subform called sfrmPhoneNumbers and in this subform I a have four
subforms called:

a. sfrmPNBusiness
b. sfrmPNBusiness2
c. sfrmPNBusiness3
d. sfrmPNBusinessFax

I cannot tab out of these subforms unless I hit Control Tab.

3. Then back to main form with another subform call sfrmAddresses.


:

Imcc007,

Assuming that all of the subforms and their record sources are properly
linked to the main form via common fields, why are you doing the record move?
Access should already have the proper record in the subform.

Just curious...

Bruce

:

When I TAB from the txtPhoneExt control of the subform (s_frmPhoneNumbers), I
want the next subform (Child22) control to become the active control. And If
I keep tabbing I want Child23, Child24, and then go to another subform
s_Addresses. I am doing this so I will not have to hit Control Tab all the
time.


Below is the code I am using:


Private Sub txtPhoneExt_Exit(Cancel As Integer)


Dim rs As Object
Set rs = Me.Recordset.Clone
rs.MoveLast
If StrComp(Me.Bookmark, rs.Bookmark, 0) = 0 Then
Forms![s_frmPhoneNumbers1]![Child22].SetFocus

End If
End Sub

It works on the first subform going to Child22, but then I get the following
error:

Runtime Error 3021:

No current record


I know I am missing something. I think it needs to loop or something until
I get to the last subform, which is Child24 and then jump to s_Addresses
after hitting tab, but I don’t know how to tell it to do that.

Help! Any suggestions?

Thanks!
 
J

Jack Leach

The code is working but it stops at rs.MoveLast. I don't know what
rs.MoveLast means.

MoveLast moves to the last record in a recordset (nothing to do with a
control focus). If there are no records in the current recordset then you
will get this error trying to move to the last record. Before attempting any
recordset operation good practice dictates to verify that there ARE records...

If rs.RecordCount <> 0 Then
rs.MoveLast
...
End If



As for the issue with the tabbing, I've never actually tried to do what you
are doing, so I'm not exactly sure how to accomplish it. In the meantime, if
you are going to copy/paste code into your project, it's usually a good idea
to try and get some grasp of what is going on with it. Some searches on
recordsets will return plenty (you probably want a DAO recordset rather than
an ADO recordset, and the recordset should technically be declared as a
DAO.Recrodset rather than an Object). Anyway...


good luck!

--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)



lmcc007 said:
The code is working but it stops at rs.MoveLast. I don't know what
rs.MoveLast means. I copied from this code from examples given I found on
the Internet.

I thought this would be a simple question b/c when I hit Control + Tab it
jumps to the next field (control) in the subform and all I want to do is to
be able to hit Tab and go to the next field in the next subform.

Jack Leach said:
Forms![s_frmPhoneNumbers1]![Child22].SetFocus

I'm not sure how much difference it will make but you can try calling the
control out more specifically. Also, you may want to set the focus to a
control inside the subform rather than the subform control itself (I don't
know that a "subform control" can actually have the focus...).

Also, if AllowAdditions is off in a form that doesn't have any records yet,
you probably won't be able to set the focus to it.

Maybe this might work better (to get to a 2nd nested subform)

Forms("Mainform").Controls("subformcontrol1").Form.Controls("Subformcontrol2").ControlName.SetFocus

or

Forms![main]!SubControl1.Form!SubControl2.Form!Controlname.Setfocus


with this:
Forms![s_frmPhoneNumbers1]![Child22].SetFocus

you seem to be trying to directly refer to the subform without going through
the main form. Access (IME) has a funny way of dealing with subforms... I
always try to explicitly walk the line to it from the ground up. Not exactly
sure what that might have to do with a No Current Record error, but it may be
worth a shot.

hth
--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)



lmcc007 said:
I hope I am understanding the question. I am trying to do a GoToControl, but
I read that SetFocus was a better chose when moving between subforms.

I just want to be able to move from subform to subform without having to use
Control Tab. Let me explain, I have:

1. Main Form called mfrmCompanies and then I have a

2. Subform called sfrmPhoneNumbers and in this subform I a have four
subforms called:

a. sfrmPNBusiness
b. sfrmPNBusiness2
c. sfrmPNBusiness3
d. sfrmPNBusinessFax

I cannot tab out of these subforms unless I hit Control Tab.

3. Then back to main form with another subform call sfrmAddresses.


:

Imcc007,

Assuming that all of the subforms and their record sources are properly
linked to the main form via common fields, why are you doing the record move?
Access should already have the proper record in the subform.

Just curious...

Bruce

:

When I TAB from the txtPhoneExt control of the subform (s_frmPhoneNumbers), I
want the next subform (Child22) control to become the active control. And If
I keep tabbing I want Child23, Child24, and then go to another subform
s_Addresses. I am doing this so I will not have to hit Control Tab all the
time.


Below is the code I am using:


Private Sub txtPhoneExt_Exit(Cancel As Integer)


Dim rs As Object
Set rs = Me.Recordset.Clone
rs.MoveLast
If StrComp(Me.Bookmark, rs.Bookmark, 0) = 0 Then
Forms![s_frmPhoneNumbers1]![Child22].SetFocus

End If
End Sub

It works on the first subform going to Child22, but then I get the following
error:

Runtime Error 3021:

No current record


I know I am missing something. I think it needs to loop or something until
I get to the last subform, which is Child24 and then jump to s_Addresses
after hitting tab, but I don’t know how to tell it to do that.

Help! Any suggestions?

Thanks!
 
J

John W. Vinson

I reworded it in hopes I a clear on what I am trying to do:

Press the TAB key or ENTER key to move from the last control on the last
record of a subform to another subform or the first control on another
subform.

It takes a bit of trickery to get the tab or enter key to move OFF a subform
onto another subform: the assumption is that once you're on a form you may
want to enter multiple records *on that form*, so you have the choice of
setting the Cycle to Next Record (the default) or Current Record (to loop back
on the same record).

If you want to bounce the user off the subform (making it hard to add more
than one record, but that may be your intention!) you can put an additional
textbox on the form, last in the tab order. Make it tiny, and/or hide it
behind another textbox - but its Visible and Enabled properties must be Yes so
that it can get the focus.

In its GotFocus event put code like

Private Sub txtRelay_GotFocus()
Parent!subSecondSubform.SetFocus
Parent!subSecondSubform.Form!controlname.SetFocus
End Sub

I'm pretty sure you need both setfocus events - to the form, and then to a
control on that form.
 
L

lmcc007

Okay, thanks! I see what you are doing.

John W. Vinson said:
It takes a bit of trickery to get the tab or enter key to move OFF a subform
onto another subform: the assumption is that once you're on a form you may
want to enter multiple records *on that form*, so you have the choice of
setting the Cycle to Next Record (the default) or Current Record (to loop back
on the same record).

If you want to bounce the user off the subform (making it hard to add more
than one record, but that may be your intention!) you can put an additional
textbox on the form, last in the tab order. Make it tiny, and/or hide it
behind another textbox - but its Visible and Enabled properties must be Yes so
that it can get the focus.

In its GotFocus event put code like

Private Sub txtRelay_GotFocus()
Parent!subSecondSubform.SetFocus
Parent!subSecondSubform.Form!controlname.SetFocus
End Sub

I'm pretty sure you need both setfocus events - to the form, and then to a
control on that form.
 
L

lmcc007

Okay, I have been reading and trying to understand the code all day but I am
still at a lost.

Haven't been able to find a good explanation for StrComp, Bookmark, and
RecordsetClone. I would like to have a clear explanation and example of what
each term mean.

Is there a really good source that give the meaning of these terms in a very
basic (clear enough for a dummy) way?

Jack Leach said:
The code is working but it stops at rs.MoveLast. I don't know what
rs.MoveLast means.

MoveLast moves to the last record in a recordset (nothing to do with a
control focus). If there are no records in the current recordset then you
will get this error trying to move to the last record. Before attempting any
recordset operation good practice dictates to verify that there ARE records...

If rs.RecordCount <> 0 Then
rs.MoveLast
...
End If



As for the issue with the tabbing, I've never actually tried to do what you
are doing, so I'm not exactly sure how to accomplish it. In the meantime, if
you are going to copy/paste code into your project, it's usually a good idea
to try and get some grasp of what is going on with it. Some searches on
recordsets will return plenty (you probably want a DAO recordset rather than
an ADO recordset, and the recordset should technically be declared as a
DAO.Recrodset rather than an Object). Anyway...


good luck!

--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)



lmcc007 said:
The code is working but it stops at rs.MoveLast. I don't know what
rs.MoveLast means. I copied from this code from examples given I found on
the Internet.

I thought this would be a simple question b/c when I hit Control + Tab it
jumps to the next field (control) in the subform and all I want to do is to
be able to hit Tab and go to the next field in the next subform.

Jack Leach said:
Forms![s_frmPhoneNumbers1]![Child22].SetFocus

I'm not sure how much difference it will make but you can try calling the
control out more specifically. Also, you may want to set the focus to a
control inside the subform rather than the subform control itself (I don't
know that a "subform control" can actually have the focus...).

Also, if AllowAdditions is off in a form that doesn't have any records yet,
you probably won't be able to set the focus to it.

Maybe this might work better (to get to a 2nd nested subform)

Forms("Mainform").Controls("subformcontrol1").Form.Controls("Subformcontrol2").ControlName.SetFocus

or

Forms![main]!SubControl1.Form!SubControl2.Form!Controlname.Setfocus


with this:
Forms![s_frmPhoneNumbers1]![Child22].SetFocus

you seem to be trying to directly refer to the subform without going through
the main form. Access (IME) has a funny way of dealing with subforms... I
always try to explicitly walk the line to it from the ground up. Not exactly
sure what that might have to do with a No Current Record error, but it may be
worth a shot.

hth
--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)



:

I hope I am understanding the question. I am trying to do a GoToControl, but
I read that SetFocus was a better chose when moving between subforms.

I just want to be able to move from subform to subform without having to use
Control Tab. Let me explain, I have:

1. Main Form called mfrmCompanies and then I have a

2. Subform called sfrmPhoneNumbers and in this subform I a have four
subforms called:

a. sfrmPNBusiness
b. sfrmPNBusiness2
c. sfrmPNBusiness3
d. sfrmPNBusinessFax

I cannot tab out of these subforms unless I hit Control Tab.

3. Then back to main form with another subform call sfrmAddresses.


:

Imcc007,

Assuming that all of the subforms and their record sources are properly
linked to the main form via common fields, why are you doing the record move?
Access should already have the proper record in the subform.

Just curious...

Bruce

:

When I TAB from the txtPhoneExt control of the subform (s_frmPhoneNumbers), I
want the next subform (Child22) control to become the active control. And If
I keep tabbing I want Child23, Child24, and then go to another subform
s_Addresses. I am doing this so I will not have to hit Control Tab all the
time.


Below is the code I am using:


Private Sub txtPhoneExt_Exit(Cancel As Integer)


Dim rs As Object
Set rs = Me.Recordset.Clone
rs.MoveLast
If StrComp(Me.Bookmark, rs.Bookmark, 0) = 0 Then
Forms![s_frmPhoneNumbers1]![Child22].SetFocus

End If
End Sub

It works on the first subform going to Child22, but then I get the following
error:

Runtime Error 3021:

No current record


I know I am missing something. I think it needs to loop or something until
I get to the last subform, which is Child24 and then jump to s_Addresses
after hitting tab, but I don’t know how to tell it to do that.

Help! Any suggestions?

Thanks!
 
L

lmcc007

Hey John Vinson,

Why can't I just enter something like this:

Forms!frmCompanies!Child5.SetFocus

because I tried it and it worked.

Is Dim rs As Object, Set rs = Me.Recordset.Clone, rs.MoveLast and so on a
must? Will it make my database more reliable and stuff?
 
J

John W. Vinson

Hey John Vinson,

Why can't I just enter something like this:

Forms!frmCompanies!Child5.SetFocus

because I tried it and it worked.

Is Dim rs As Object, Set rs = Me.Recordset.Clone, rs.MoveLast and so on a
must? Will it make my database more reliable and stuff?

That will set focus *to the subform* but it's pot luck which control on the
subform; and (in this context)

Parent!Child5

is a synonym of

Forms!frmCompanies!Child5

and it's simpler, shorter, and more portable if you want to do this on another
form.

The recordset operations are unrelated to the set focus, and I haven't
actually been following that part of the thread.
 
L

lmcc007

Okay, thanks! It's only two fields on the subform (phone and ext.).
Parent!Child5 is shorter.

Thanks for your help.
 
J

Jack Leach

Is Dim rs As Object, Set rs = Me.Recordset.Clone, rs.MoveLast and so on a
must? Will it make my database more reliable and stuff?

What this code is doing is simply checking to see if you are on the last
record, and only forcing the focus to the next form if you are on the last
record.

A recordset is a set of records (go figure). Each form has an underlying
recordset, be it directly from a table or from a query (an SQL Statement
returns a recordset...). Recordsets can be created programmatically and be
used to modify data or check stuff or whatever, they don't have to be
assigned to a form (the data in a report is based on a recordset as well).

A bookmark is (simply put) the current position in the recordset.

The StrComp function compares two strings to see if they are alike.

So here's a repost of your code, with comments added to explain how this is
checking to make sure we are on the last record before setting the focus on
the next form (instead of going to the next record in the same form).



Private Sub txtPhoneExt_Exit(Cancel As Integer)

'dim the object to hold the recordset
Dim rs As Object

'Set this new rs you just made to be the same as the one
'in this form (it's Clone). Now we have the form's recordset
'and this one we just created to play around with
Set rs = Me.Recordset.Clone

'rs is the clone, so move to the last record in that
rs.MoveLast

'compare the current position of the form's recordset to
'the current position (last record) of the clone... if they're
'the same (if you know you're on the last record), move
'the focus as desired
If StrComp(Me.Bookmark, rs.Bookmark, 0) = 0 Then
Forms![s_frmPhoneNumbers1]![Child22].SetFocus

End If
End Sub




There's a problem with the above code though. If you exit the control
without any records in there, you can't rs.MoveLast if there's no last record
to move to. This might happen if the subform is blank and you tab through
the empty fields in the New Record to jump to the next subform. You would be
able to tab, but there's no record saved if you don't enter anything. Hence
your No Current Record error.

I would re-write it as so:

Private Sub txtPhoneExt_Exit(Cancel As Integer)

'create and set the recordset
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone

'make sure there's records before you try moving to one
If rs.Recordcount <> 0 Then

'if theres records, move to the last one
rs.MoveLast

'run your check and move the focus accordingly
If StrComp(Me.Bookmark, rs.Bookmark, 0) = 0 Then
Forms!............SetFocus
End If

Else
'if there's no records just go to the next subform
Forms!............SetFocus
End If

End Sub



(..... in replacement of your form names, etc so the word wrap here doesn't
make it hard to read).

Some more recordset info:

rs.BOF (beginning of file, default position when opened... not a record, you
need to MoveFirst to get to the first record. from the first record
rs.MovePrevious puts you at BOF. You can't try and read a bookmark from BOF
because its not a record).

rs.EOF (end of file... you can't reference a bookmark here either, it's not
actually a record... rs.MovePrevious from EOF will put you on the last
record, if there's any records)

Another way to check for recordset is to see if BOF and EOF are true at the
same time. If the are both true, there's no records (and MoveNext,
MoveFirst, MoveLast, MovePrevious will not work)

hope this sheds a little light on what's going on.

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
L

lmcc007

Thanks a bunch!!! This is some good information. I will keep rereading
until my mind comprehends.

I took up a VBA course online and it covers none of this; I really didn't
learn much. I like to know what everything means instead of just doing. He
basically did a lot of DoCmd, DLookup, messages boxes and command buttons.

Do you have any suggestions where to really learn this information?

Thanks!

Jack Leach said:
Is Dim rs As Object, Set rs = Me.Recordset.Clone, rs.MoveLast and so on a
must? Will it make my database more reliable and stuff?

What this code is doing is simply checking to see if you are on the last
record, and only forcing the focus to the next form if you are on the last
record.

A recordset is a set of records (go figure). Each form has an underlying
recordset, be it directly from a table or from a query (an SQL Statement
returns a recordset...). Recordsets can be created programmatically and be
used to modify data or check stuff or whatever, they don't have to be
assigned to a form (the data in a report is based on a recordset as well).

A bookmark is (simply put) the current position in the recordset.

The StrComp function compares two strings to see if they are alike.

So here's a repost of your code, with comments added to explain how this is
checking to make sure we are on the last record before setting the focus on
the next form (instead of going to the next record in the same form).



Private Sub txtPhoneExt_Exit(Cancel As Integer)

'dim the object to hold the recordset
Dim rs As Object

'Set this new rs you just made to be the same as the one
'in this form (it's Clone). Now we have the form's recordset
'and this one we just created to play around with
Set rs = Me.Recordset.Clone

'rs is the clone, so move to the last record in that
rs.MoveLast

'compare the current position of the form's recordset to
'the current position (last record) of the clone... if they're
'the same (if you know you're on the last record), move
'the focus as desired
If StrComp(Me.Bookmark, rs.Bookmark, 0) = 0 Then
Forms![s_frmPhoneNumbers1]![Child22].SetFocus

End If
End Sub




There's a problem with the above code though. If you exit the control
without any records in there, you can't rs.MoveLast if there's no last record
to move to. This might happen if the subform is blank and you tab through
the empty fields in the New Record to jump to the next subform. You would be
able to tab, but there's no record saved if you don't enter anything. Hence
your No Current Record error.

I would re-write it as so:

Private Sub txtPhoneExt_Exit(Cancel As Integer)

'create and set the recordset
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone

'make sure there's records before you try moving to one
If rs.Recordcount <> 0 Then

'if theres records, move to the last one
rs.MoveLast

'run your check and move the focus accordingly
If StrComp(Me.Bookmark, rs.Bookmark, 0) = 0 Then
Forms!............SetFocus
End If

Else
'if there's no records just go to the next subform
Forms!............SetFocus
End If

End Sub



(..... in replacement of your form names, etc so the word wrap here doesn't
make it hard to read).

Some more recordset info:

rs.BOF (beginning of file, default position when opened... not a record, you
need to MoveFirst to get to the first record. from the first record
rs.MovePrevious puts you at BOF. You can't try and read a bookmark from BOF
because its not a record).

rs.EOF (end of file... you can't reference a bookmark here either, it's not
actually a record... rs.MovePrevious from EOF will put you on the last
record, if there's any records)

Another way to check for recordset is to see if BOF and EOF are true at the
same time. If the are both true, there's no records (and MoveNext,
MoveFirst, MoveLast, MovePrevious will not work)

hope this sheds a little light on what's going on.

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)



lmcc007 said:
Hey John Vinson,

Why can't I just enter something like this:

Forms!frmCompanies!Child5.SetFocus

because I tried it and it worked.

Is Dim rs As Object, Set rs = Me.Recordset.Clone, rs.MoveLast and so on a
must? Will it make my database more reliable and stuff?
 
J

Jack Leach

Do you have any suggestions where to really learn this information?


Right here is the best resource :)

There's no single place that I've found that makes it easy to cover
everything. Actually, come to thing of it, MVP Crystal (Strive4Peace) has an
awsome site for the beginner that covers just about everything you need for a
good foundation ... I wish I had found it when I was starting instead of
later in the game.

Allen Browne has a tips site aimed at Casual, Intermediate and Serious
programmers and users of Access. This is where I spent most of my time when
learning the ropes. Lots of great examples.

Many other MVPs have sites as well... I'm not sure I can dig them all up
though. A lot of these more or less specialize in certain areas rather than
a broad scope of the whole works like what can be found in Allen's and
Crystal's work.

I've found the blueclaw site to be very helpful as far as getting
"indroductory" examples on new areas. This is where I got most of my
recordset info from when I started learning about them. The cover a wide
scope and tend to give real-life simple examples, rather than explaining
every single thing that any function or concept is capable of.

The built-in help is actually an excellent resource as well. My only issue
with this is that, in opposition to the blueclaw site, they give you
everything you could need to know, which is usually extremely confusing for
the newbie. But once you learn how to filter out what you don't need (e.i. -
get the basics down), the Help is great for working out details or learning
about further functionality.

I've got literally hundreds of links to various sites and discussions and
the like that I've collected since I started. So, no there's not really one
good place to learn it all, but here's some stuff to get you started...


http://www.accessmvp.com/strive4peace/

http://www.allenbrowne.com/tips.html

http://www.blueclaw-db.com/ms_access_examples/


If you need specific help with anything else feel free to ask. The best
part is, usually someone has already asked it, so a google with the right
keywords usually returns stuff from sites like pcreview or tektips threads
(which are all pulled from this or one or two other major newsgroups).
UtterAccess.com also has a plethera of info. The newsgroups are by far the
best resource available.

happy coding!
--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)
 
J

John W. Vinson

What this code is doing is simply checking to see if you are on the last
record, and only forcing the focus to the next form if you are on the last
record.

I think that's the source of some confusion in this thread: it appears that
lmcc007 was actually thinking of the last *FIELD* in a record, rather than the
last record on a form. Your code will of course work fine for the last record!
 
L

lmcc007

Thanks!! Got a lot of reading to do.

Jack Leach said:
Right here is the best resource :)

There's no single place that I've found that makes it easy to cover
everything. Actually, come to thing of it, MVP Crystal (Strive4Peace) has an
awsome site for the beginner that covers just about everything you need for a
good foundation ... I wish I had found it when I was starting instead of
later in the game.

Allen Browne has a tips site aimed at Casual, Intermediate and Serious
programmers and users of Access. This is where I spent most of my time when
learning the ropes. Lots of great examples.

Many other MVPs have sites as well... I'm not sure I can dig them all up
though. A lot of these more or less specialize in certain areas rather than
a broad scope of the whole works like what can be found in Allen's and
Crystal's work.

I've found the blueclaw site to be very helpful as far as getting
"indroductory" examples on new areas. This is where I got most of my
recordset info from when I started learning about them. The cover a wide
scope and tend to give real-life simple examples, rather than explaining
every single thing that any function or concept is capable of.

The built-in help is actually an excellent resource as well. My only issue
with this is that, in opposition to the blueclaw site, they give you
everything you could need to know, which is usually extremely confusing for
the newbie. But once you learn how to filter out what you don't need (e.i. -
get the basics down), the Help is great for working out details or learning
about further functionality.

I've got literally hundreds of links to various sites and discussions and
the like that I've collected since I started. So, no there's not really one
good place to learn it all, but here's some stuff to get you started...


http://www.accessmvp.com/strive4peace/

http://www.allenbrowne.com/tips.html

http://www.blueclaw-db.com/ms_access_examples/


If you need specific help with anything else feel free to ask. The best
part is, usually someone has already asked it, so a google with the right
keywords usually returns stuff from sites like pcreview or tektips threads
(which are all pulled from this or one or two other major newsgroups).
UtterAccess.com also has a plethera of info. The newsgroups are by far the
best resource available.

happy coding!
--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)
 
L

lmcc007

It is only two fields (PhoneNum and Ext) in each subform, so when I tab out
of Ext I want it go to Child1 (next subform)...

I have a subform call frmPhoneNumbers with two fields (PhoneNum and Ext). I
dragged the subform onto my main form and copied the subform 3 times (Child1,
Child2, Child3)
 
J

John W. Vinson

It is only two fields (PhoneNum and Ext) in each subform, so when I tab out
of Ext I want it go to Child1 (next subform)...

That's ok if you have only one phone number per client...
I have a subform call frmPhoneNumbers with two fields (PhoneNum and Ext). I
dragged the subform onto my main form and copied the subform 3 times (Child1,
Child2, Child3)

But that makes NO SENSE AT ALL. It will display the same phone number three
times! What are you trying to *ACCOMPLISH* in the real world here (rather than
describing how you're trying to do it)?
 

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

Similar Threads


Top