PC Review


Reply
Thread Tools Rate Thread

How to Apply Current Recordsource?

 
 
NEWER USER
Guest
Posts: n/a
 
      30th Jan 2010
I have a toolbar that I want to assign to multiple forms that are viewed in
Datasheet view mode only. I have assigned a Function on the toolbar to run an
On Acxtion command. Each form has a different Recordsource (qryAll,
qryTotals, qryParts, etc). The following works well on one form only. As the
form and Recordsource change, I need to be able to recognize the current
Recordsouce so I don't have to create a separate toolbar for each form only
to change qryAll to qryTotals. Any help appreciated.

Function QTag()
Dim strWhere As String
Dim strMsg As String

With CodeContextObject
strWhere = "WHERE " & .Filter
End With

strMsg = "FILTERED records will be tagged."
If (MsgBox(strMsg, 273, "Warning") <> 1) Then
'Tag filtered records on form
DoCmd.CancelEvent
Exit Function
End If

DoCmd.SetWarnings False

'This is where I need to vary the recorsource below
DoCmd.RunSQL "UPDATE qryAll " & _
"SET qryAll.T = '-1'" & strWhere

DoCmd.Requery
DoCmd.SetWarnings True

End Fuction
 
Reply With Quote
 
 
 
 
Tom van Stiphout
Guest
Posts: n/a
 
      30th Jan 2010
On Sat, 30 Jan 2010 08:12:06 -0800, NEWER USER
<(E-Mail Removed)> wrote:

There is a better way to filter: rather than changing the
RecordSource: set the Filter and FilterOn property.

For readability change your MsgBox statement to something like:
If (MsgBox(strMsg, vbYesNo or vbQuestion, "Warning") = vbNo) Then

-Tom.
Microsoft Access MVP


>I have a toolbar that I want to assign to multiple forms that are viewed in
>Datasheet view mode only. I have assigned a Function on the toolbar to run an
>On Acxtion command. Each form has a different Recordsource (qryAll,
>qryTotals, qryParts, etc). The following works well on one form only. As the
>form and Recordsource change, I need to be able to recognize the current
>Recordsouce so I don't have to create a separate toolbar for each form only
>to change qryAll to qryTotals. Any help appreciated.
>
>Function QTag()
> Dim strWhere As String
> Dim strMsg As String
>
> With CodeContextObject
> strWhere = "WHERE " & .Filter
> End With
>
> strMsg = "FILTERED records will be tagged."
> If (MsgBox(strMsg, 273, "Warning") <> 1) Then
> 'Tag filtered records on form
> DoCmd.CancelEvent
> Exit Function
>End If
>
>DoCmd.SetWarnings False
>
>'This is where I need to vary the recorsource below
>DoCmd.RunSQL "UPDATE qryAll " & _
> "SET qryAll.T = '-1'" & strWhere
>
>DoCmd.Requery
>DoCmd.SetWarnings True
>
>End Fuction

 
Reply With Quote
 
NEWER USER
Guest
Posts: n/a
 
      30th Jan 2010
I don't think we connected. Normally, I would place a command button on the
form to run the Update SQL based on the open form and any filters that may be
applied since opening the form. Howver, datasheet view doesn't allow for a
command button, so I chose to place a toolbar assigned to the open form and
run a Function (On Action property). If I were to place the toolbar on
another form with a differ recorsource, I would have to create a new toolbar
and function that references the current form. I want to have one toolbar on
several different forms and have the Function recognize the Current Form and
recordset. Can this be done?

"Tom van Stiphout" wrote:

> On Sat, 30 Jan 2010 08:12:06 -0800, NEWER USER
> <(E-Mail Removed)> wrote:
>
> There is a better way to filter: rather than changing the
> RecordSource: set the Filter and FilterOn property.
>
> For readability change your MsgBox statement to something like:
> If (MsgBox(strMsg, vbYesNo or vbQuestion, "Warning") = vbNo) Then
>
> -Tom.
> Microsoft Access MVP
>
>
> >I have a toolbar that I want to assign to multiple forms that are viewed in
> >Datasheet view mode only. I have assigned a Function on the toolbar to run an
> >On Acxtion command. Each form has a different Recordsource (qryAll,
> >qryTotals, qryParts, etc). The following works well on one form only. As the
> >form and Recordsource change, I need to be able to recognize the current
> >Recordsouce so I don't have to create a separate toolbar for each form only
> >to change qryAll to qryTotals. Any help appreciated.
> >
> >Function QTag()
> > Dim strWhere As String
> > Dim strMsg As String
> >
> > With CodeContextObject
> > strWhere = "WHERE " & .Filter
> > End With
> >
> > strMsg = "FILTERED records will be tagged."
> > If (MsgBox(strMsg, 273, "Warning") <> 1) Then
> > 'Tag filtered records on form
> > DoCmd.CancelEvent
> > Exit Function
> >End If
> >
> >DoCmd.SetWarnings False
> >
> >'This is where I need to vary the recorsource below
> >DoCmd.RunSQL "UPDATE qryAll " & _
> > "SET qryAll.T = '-1'" & strWhere
> >
> >DoCmd.Requery
> >DoCmd.SetWarnings True
> >
> >End Fuction

> .
>

 
Reply With Quote
 
Tom van Stiphout
Guest
Posts: n/a
 
      31st Jan 2010
On Sat, 30 Jan 2010 12:13:01 -0800, NEWER USER
<(E-Mail Removed)> wrote:

You're right, sorry about that. I thought you wanted to filter a form,
but you want to run an update query.

The current form can be referenced using Screen.ActiveForm. The
current RecordSource is Screen.ActiveForm.RecordSource.

-Tom.
Microsoft Access MVP




>I don't think we connected. Normally, I would place a command button on the
>form to run the Update SQL based on the open form and any filters that may be
>applied since opening the form. Howver, datasheet view doesn't allow for a
>command button, so I chose to place a toolbar assigned to the open form and
>run a Function (On Action property). If I were to place the toolbar on
>another form with a differ recorsource, I would have to create a new toolbar
>and function that references the current form. I want to have one toolbar on
>several different forms and have the Function recognize the Current Form and
>recordset. Can this be done?
>
>"Tom van Stiphout" wrote:
>
>> On Sat, 30 Jan 2010 08:12:06 -0800, NEWER USER
>> <(E-Mail Removed)> wrote:
>>
>> There is a better way to filter: rather than changing the
>> RecordSource: set the Filter and FilterOn property.
>>
>> For readability change your MsgBox statement to something like:
>> If (MsgBox(strMsg, vbYesNo or vbQuestion, "Warning") = vbNo) Then
>>
>> -Tom.
>> Microsoft Access MVP
>>
>>
>> >I have a toolbar that I want to assign to multiple forms that are viewed in
>> >Datasheet view mode only. I have assigned a Function on the toolbar to run an
>> >On Acxtion command. Each form has a different Recordsource (qryAll,
>> >qryTotals, qryParts, etc). The following works well on one form only. As the
>> >form and Recordsource change, I need to be able to recognize the current
>> >Recordsouce so I don't have to create a separate toolbar for each form only
>> >to change qryAll to qryTotals. Any help appreciated.
>> >
>> >Function QTag()
>> > Dim strWhere As String
>> > Dim strMsg As String
>> >
>> > With CodeContextObject
>> > strWhere = "WHERE " & .Filter
>> > End With
>> >
>> > strMsg = "FILTERED records will be tagged."
>> > If (MsgBox(strMsg, 273, "Warning") <> 1) Then
>> > 'Tag filtered records on form
>> > DoCmd.CancelEvent
>> > Exit Function
>> >End If
>> >
>> >DoCmd.SetWarnings False
>> >
>> >'This is where I need to vary the recorsource below
>> >DoCmd.RunSQL "UPDATE qryAll " & _
>> > "SET qryAll.T = '-1'" & strWhere
>> >
>> >DoCmd.Requery
>> >DoCmd.SetWarnings True
>> >
>> >End Fuction

>> .
>>

 
Reply With Quote
 
NEWER USER
Guest
Posts: n/a
 
      31st Jan 2010
This is where I stumble. In my code below, what do I replace "qryAll" with?
As other forms open the Recordsource will be a different query. The
Recordsource will have to vary (qryTotals, qryParts, etc). Your help is
appreciated.

DoCmd.RunSQL "UPDATE qryAll " & _
"SET qryAll.T = '-1'" & strWhere


"Tom van Stiphout" wrote:

> On Sat, 30 Jan 2010 12:13:01 -0800, NEWER USER
> <(E-Mail Removed)> wrote:
>
> You're right, sorry about that. I thought you wanted to filter a form,
> but you want to run an update query.
>
> The current form can be referenced using Screen.ActiveForm. The
> current RecordSource is Screen.ActiveForm.RecordSource.
>
> -Tom.
> Microsoft Access MVP
>
>
>
>
> >I don't think we connected. Normally, I would place a command button on the
> >form to run the Update SQL based on the open form and any filters that may be
> >applied since opening the form. Howver, datasheet view doesn't allow for a
> >command button, so I chose to place a toolbar assigned to the open form and
> >run a Function (On Action property). If I were to place the toolbar on
> >another form with a differ recorsource, I would have to create a new toolbar
> >and function that references the current form. I want to have one toolbar on
> >several different forms and have the Function recognize the Current Form and
> >recordset. Can this be done?
> >
> >"Tom van Stiphout" wrote:
> >
> >> On Sat, 30 Jan 2010 08:12:06 -0800, NEWER USER
> >> <(E-Mail Removed)> wrote:
> >>
> >> There is a better way to filter: rather than changing the
> >> RecordSource: set the Filter and FilterOn property.
> >>
> >> For readability change your MsgBox statement to something like:
> >> If (MsgBox(strMsg, vbYesNo or vbQuestion, "Warning") = vbNo) Then
> >>
> >> -Tom.
> >> Microsoft Access MVP
> >>
> >>
> >> >I have a toolbar that I want to assign to multiple forms that are viewed in
> >> >Datasheet view mode only. I have assigned a Function on the toolbar to run an
> >> >On Acxtion command. Each form has a different Recordsource (qryAll,
> >> >qryTotals, qryParts, etc). The following works well on one form only. As the
> >> >form and Recordsource change, I need to be able to recognize the current
> >> >Recordsouce so I don't have to create a separate toolbar for each form only
> >> >to change qryAll to qryTotals. Any help appreciated.
> >> >
> >> >Function QTag()
> >> > Dim strWhere As String
> >> > Dim strMsg As String
> >> >
> >> > With CodeContextObject
> >> > strWhere = "WHERE " & .Filter
> >> > End With
> >> >
> >> > strMsg = "FILTERED records will be tagged."
> >> > If (MsgBox(strMsg, 273, "Warning") <> 1) Then
> >> > 'Tag filtered records on form
> >> > DoCmd.CancelEvent
> >> > Exit Function
> >> >End If
> >> >
> >> >DoCmd.SetWarnings False
> >> >
> >> >'This is where I need to vary the recorsource below
> >> >DoCmd.RunSQL "UPDATE qryAll " & _
> >> > "SET qryAll.T = '-1'" & strWhere
> >> >
> >> >DoCmd.Requery
> >> >DoCmd.SetWarnings True
> >> >
> >> >End Fuction
> >> .
> >>

> .
>

 
Reply With Quote
 
Tom van Stiphout
Guest
Posts: n/a
 
      31st Jan 2010
On Sat, 30 Jan 2010 19:33:01 -0800, NEWER USER
<(E-Mail Removed)> wrote:

You concatenate the correct sql statement. Something like:
dim sql as string
dim frm as form
set frm = screen.activeform
sql = "update " & frm.Recordsource & " set " & frm.recordsource & ".T
= '-1'" & strWhere
docmd.runsql sql

This would of course assume T is a field in all these queries.

-Tom.
Microsoft Access MVP



>This is where I stumble. In my code below, what do I replace "qryAll" with?
>As other forms open the Recordsource will be a different query. The
>Recordsource will have to vary (qryTotals, qryParts, etc). Your help is
>appreciated.
>
>DoCmd.RunSQL "UPDATE qryAll " & _
> "SET qryAll.T = '-1'" & strWhere
>
>
>"Tom van Stiphout" wrote:
>
>> On Sat, 30 Jan 2010 12:13:01 -0800, NEWER USER
>> <(E-Mail Removed)> wrote:
>>
>> You're right, sorry about that. I thought you wanted to filter a form,
>> but you want to run an update query.
>>
>> The current form can be referenced using Screen.ActiveForm. The
>> current RecordSource is Screen.ActiveForm.RecordSource.
>>
>> -Tom.
>> Microsoft Access MVP
>>
>>
>>
>>
>> >I don't think we connected. Normally, I would place a command button on the
>> >form to run the Update SQL based on the open form and any filters that may be
>> >applied since opening the form. Howver, datasheet view doesn't allow for a
>> >command button, so I chose to place a toolbar assigned to the open form and
>> >run a Function (On Action property). If I were to place the toolbar on
>> >another form with a differ recorsource, I would have to create a new toolbar
>> >and function that references the current form. I want to have one toolbar on
>> >several different forms and have the Function recognize the Current Form and
>> >recordset. Can this be done?
>> >
>> >"Tom van Stiphout" wrote:
>> >
>> >> On Sat, 30 Jan 2010 08:12:06 -0800, NEWER USER
>> >> <(E-Mail Removed)> wrote:
>> >>
>> >> There is a better way to filter: rather than changing the
>> >> RecordSource: set the Filter and FilterOn property.
>> >>
>> >> For readability change your MsgBox statement to something like:
>> >> If (MsgBox(strMsg, vbYesNo or vbQuestion, "Warning") = vbNo) Then
>> >>
>> >> -Tom.
>> >> Microsoft Access MVP
>> >>
>> >>
>> >> >I have a toolbar that I want to assign to multiple forms that are viewed in
>> >> >Datasheet view mode only. I have assigned a Function on the toolbar to run an
>> >> >On Acxtion command. Each form has a different Recordsource (qryAll,
>> >> >qryTotals, qryParts, etc). The following works well on one form only. As the
>> >> >form and Recordsource change, I need to be able to recognize the current
>> >> >Recordsouce so I don't have to create a separate toolbar for each form only
>> >> >to change qryAll to qryTotals. Any help appreciated.
>> >> >
>> >> >Function QTag()
>> >> > Dim strWhere As String
>> >> > Dim strMsg As String
>> >> >
>> >> > With CodeContextObject
>> >> > strWhere = "WHERE " & .Filter
>> >> > End With
>> >> >
>> >> > strMsg = "FILTERED records will be tagged."
>> >> > If (MsgBox(strMsg, 273, "Warning") <> 1) Then
>> >> > 'Tag filtered records on form
>> >> > DoCmd.CancelEvent
>> >> > Exit Function
>> >> >End If
>> >> >
>> >> >DoCmd.SetWarnings False
>> >> >
>> >> >'This is where I need to vary the recorsource below
>> >> >DoCmd.RunSQL "UPDATE qryAll " & _
>> >> > "SET qryAll.T = '-1'" & strWhere
>> >> >
>> >> >DoCmd.Requery
>> >> >DoCmd.SetWarnings True
>> >> >
>> >> >End Fuction
>> >> .
>> >>

>> .
>>

 
Reply With Quote
 
NEWER USER
Guest
Posts: n/a
 
      31st Jan 2010
YOU NAILED IT! Thanks for all the effort and patience. TOP MVP in my book.
Thanks again.

"Tom van Stiphout" wrote:

> On Sat, 30 Jan 2010 19:33:01 -0800, NEWER USER
> <(E-Mail Removed)> wrote:
>
> You concatenate the correct sql statement. Something like:
> dim sql as string
> dim frm as form
> set frm = screen.activeform
> sql = "update " & frm.Recordsource & " set " & frm.recordsource & ".T
> = '-1'" & strWhere
> docmd.runsql sql
>
> This would of course assume T is a field in all these queries.
>
> -Tom.
> Microsoft Access MVP
>
>
>
> >This is where I stumble. In my code below, what do I replace "qryAll" with?
> >As other forms open the Recordsource will be a different query. The
> >Recordsource will have to vary (qryTotals, qryParts, etc). Your help is
> >appreciated.
> >
> >DoCmd.RunSQL "UPDATE qryAll " & _
> > "SET qryAll.T = '-1'" & strWhere
> >
> >
> >"Tom van Stiphout" wrote:
> >
> >> On Sat, 30 Jan 2010 12:13:01 -0800, NEWER USER
> >> <(E-Mail Removed)> wrote:
> >>
> >> You're right, sorry about that. I thought you wanted to filter a form,
> >> but you want to run an update query.
> >>
> >> The current form can be referenced using Screen.ActiveForm. The
> >> current RecordSource is Screen.ActiveForm.RecordSource.
> >>
> >> -Tom.
> >> Microsoft Access MVP
> >>
> >>
> >>
> >>
> >> >I don't think we connected. Normally, I would place a command button on the
> >> >form to run the Update SQL based on the open form and any filters that may be
> >> >applied since opening the form. Howver, datasheet view doesn't allow for a
> >> >command button, so I chose to place a toolbar assigned to the open form and
> >> >run a Function (On Action property). If I were to place the toolbar on
> >> >another form with a differ recorsource, I would have to create a new toolbar
> >> >and function that references the current form. I want to have one toolbar on
> >> >several different forms and have the Function recognize the Current Form and
> >> >recordset. Can this be done?
> >> >
> >> >"Tom van Stiphout" wrote:
> >> >
> >> >> On Sat, 30 Jan 2010 08:12:06 -0800, NEWER USER
> >> >> <(E-Mail Removed)> wrote:
> >> >>
> >> >> There is a better way to filter: rather than changing the
> >> >> RecordSource: set the Filter and FilterOn property.
> >> >>
> >> >> For readability change your MsgBox statement to something like:
> >> >> If (MsgBox(strMsg, vbYesNo or vbQuestion, "Warning") = vbNo) Then
> >> >>
> >> >> -Tom.
> >> >> Microsoft Access MVP
> >> >>
> >> >>
> >> >> >I have a toolbar that I want to assign to multiple forms that are viewed in
> >> >> >Datasheet view mode only. I have assigned a Function on the toolbar to run an
> >> >> >On Acxtion command. Each form has a different Recordsource (qryAll,
> >> >> >qryTotals, qryParts, etc). The following works well on one form only. As the
> >> >> >form and Recordsource change, I need to be able to recognize the current
> >> >> >Recordsouce so I don't have to create a separate toolbar for each form only
> >> >> >to change qryAll to qryTotals. Any help appreciated.
> >> >> >
> >> >> >Function QTag()
> >> >> > Dim strWhere As String
> >> >> > Dim strMsg As String
> >> >> >
> >> >> > With CodeContextObject
> >> >> > strWhere = "WHERE " & .Filter
> >> >> > End With
> >> >> >
> >> >> > strMsg = "FILTERED records will be tagged."
> >> >> > If (MsgBox(strMsg, 273, "Warning") <> 1) Then
> >> >> > 'Tag filtered records on form
> >> >> > DoCmd.CancelEvent
> >> >> > Exit Function
> >> >> >End If
> >> >> >
> >> >> >DoCmd.SetWarnings False
> >> >> >
> >> >> >'This is where I need to vary the recorsource below
> >> >> >DoCmd.RunSQL "UPDATE qryAll " & _
> >> >> > "SET qryAll.T = '-1'" & strWhere
> >> >> >
> >> >> >DoCmd.Requery
> >> >> >DoCmd.SetWarnings True
> >> >> >
> >> >> >End Fuction
> >> >> .
> >> >>
> >> .
> >>

> .
>

 
Reply With Quote
 
NEWER USER
Guest
Posts: n/a
 
      31st Jan 2010
Because this worked so well, I want to go one step further. Suppose I have
a Tab Control with multiple subforms on a Main form. You taught me how to
reference the Active Main form (below) and I now want to reference the active
subform once I have clicked a page on the tab control. How might I do this
without a specific subform name?

My plan is to build one function and assign it to a command button on
several subforms which can perform updates on the same field as I did
earlier. I want to get away from using repetitive code only to change the
current subform name in the update sql. Can this be done?

dim frm as form
set frm = screen.activeform

"Tom van Stiphout" wrote:

> On Sat, 30 Jan 2010 19:33:01 -0800, NEWER USER
> <(E-Mail Removed)> wrote:
>
> You concatenate the correct sql statement. Something like:
> dim sql as string
> dim frm as form
> set frm = screen.activeform
> sql = "update " & frm.Recordsource & " set " & frm.recordsource & ".T
> = '-1'" & strWhere
> docmd.runsql sql
>
> This would of course assume T is a field in all these queries.
>
> -Tom.
> Microsoft Access MVP
>
>
>
> >This is where I stumble. In my code below, what do I replace "qryAll" with?
> >As other forms open the Recordsource will be a different query. The
> >Recordsource will have to vary (qryTotals, qryParts, etc). Your help is
> >appreciated.
> >
> >DoCmd.RunSQL "UPDATE qryAll " & _
> > "SET qryAll.T = '-1'" & strWhere
> >
> >
> >"Tom van Stiphout" wrote:
> >
> >> On Sat, 30 Jan 2010 12:13:01 -0800, NEWER USER
> >> <(E-Mail Removed)> wrote:
> >>
> >> You're right, sorry about that. I thought you wanted to filter a form,
> >> but you want to run an update query.
> >>
> >> The current form can be referenced using Screen.ActiveForm. The
> >> current RecordSource is Screen.ActiveForm.RecordSource.
> >>
> >> -Tom.
> >> Microsoft Access MVP
> >>
> >>
> >>
> >>
> >> >I don't think we connected. Normally, I would place a command button on the
> >> >form to run the Update SQL based on the open form and any filters that may be
> >> >applied since opening the form. Howver, datasheet view doesn't allow for a
> >> >command button, so I chose to place a toolbar assigned to the open form and
> >> >run a Function (On Action property). If I were to place the toolbar on
> >> >another form with a differ recorsource, I would have to create a new toolbar
> >> >and function that references the current form. I want to have one toolbar on
> >> >several different forms and have the Function recognize the Current Form and
> >> >recordset. Can this be done?
> >> >
> >> >"Tom van Stiphout" wrote:
> >> >
> >> >> On Sat, 30 Jan 2010 08:12:06 -0800, NEWER USER
> >> >> <(E-Mail Removed)> wrote:
> >> >>
> >> >> There is a better way to filter: rather than changing the
> >> >> RecordSource: set the Filter and FilterOn property.
> >> >>
> >> >> For readability change your MsgBox statement to something like:
> >> >> If (MsgBox(strMsg, vbYesNo or vbQuestion, "Warning") = vbNo) Then
> >> >>
> >> >> -Tom.
> >> >> Microsoft Access MVP
> >> >>
> >> >>
> >> >> >I have a toolbar that I want to assign to multiple forms that are viewed in
> >> >> >Datasheet view mode only. I have assigned a Function on the toolbar to run an
> >> >> >On Acxtion command. Each form has a different Recordsource (qryAll,
> >> >> >qryTotals, qryParts, etc). The following works well on one form only. As the
> >> >> >form and Recordsource change, I need to be able to recognize the current
> >> >> >Recordsouce so I don't have to create a separate toolbar for each form only
> >> >> >to change qryAll to qryTotals. Any help appreciated.
> >> >> >
> >> >> >Function QTag()
> >> >> > Dim strWhere As String
> >> >> > Dim strMsg As String
> >> >> >
> >> >> > With CodeContextObject
> >> >> > strWhere = "WHERE " & .Filter
> >> >> > End With
> >> >> >
> >> >> > strMsg = "FILTERED records will be tagged."
> >> >> > If (MsgBox(strMsg, 273, "Warning") <> 1) Then
> >> >> > 'Tag filtered records on form
> >> >> > DoCmd.CancelEvent
> >> >> > Exit Function
> >> >> >End If
> >> >> >
> >> >> >DoCmd.SetWarnings False
> >> >> >
> >> >> >'This is where I need to vary the recorsource below
> >> >> >DoCmd.RunSQL "UPDATE qryAll " & _
> >> >> > "SET qryAll.T = '-1'" & strWhere
> >> >> >
> >> >> >DoCmd.Requery
> >> >> >DoCmd.SetWarnings True
> >> >> >
> >> >> >End Fuction
> >> >> .
> >> >>
> >> .
> >>

> .
>

 
Reply With Quote
 
Tom van Stiphout
Guest
Posts: n/a
 
      1st Feb 2010
On Sun, 31 Jan 2010 13:05:01 -0800, NEWER USER
<(E-Mail Removed)> wrote:

Screen.ActiveControl will give you the active control. This may be in
a subform.
If it is in a subform then screen.ActiveControl.Parent.Name is not
equal to screen.ActiveControl.Name.

Can you take it from there?

-Tom.
Microsoft Access MVP



>Because this worked so well, I want to go one step further. Suppose I have
>a Tab Control with multiple subforms on a Main form. You taught me how to
>reference the Active Main form (below) and I now want to reference the active
>subform once I have clicked a page on the tab control. How might I do this
>without a specific subform name?
>
>My plan is to build one function and assign it to a command button on
>several subforms which can perform updates on the same field as I did
>earlier. I want to get away from using repetitive code only to change the
>current subform name in the update sql. Can this be done?
>
>dim frm as form
>set frm = screen.activeform
>
>"Tom van Stiphout" wrote:
>
>> On Sat, 30 Jan 2010 19:33:01 -0800, NEWER USER
>> <(E-Mail Removed)> wrote:
>>
>> You concatenate the correct sql statement. Something like:
>> dim sql as string
>> dim frm as form
>> set frm = screen.activeform
>> sql = "update " & frm.Recordsource & " set " & frm.recordsource & ".T
>> = '-1'" & strWhere
>> docmd.runsql sql
>>
>> This would of course assume T is a field in all these queries.
>>
>> -Tom.
>> Microsoft Access MVP
>>
>>
>>
>> >This is where I stumble. In my code below, what do I replace "qryAll" with?
>> >As other forms open the Recordsource will be a different query. The
>> >Recordsource will have to vary (qryTotals, qryParts, etc). Your help is
>> >appreciated.
>> >
>> >DoCmd.RunSQL "UPDATE qryAll " & _
>> > "SET qryAll.T = '-1'" & strWhere
>> >
>> >
>> >"Tom van Stiphout" wrote:
>> >
>> >> On Sat, 30 Jan 2010 12:13:01 -0800, NEWER USER
>> >> <(E-Mail Removed)> wrote:
>> >>
>> >> You're right, sorry about that. I thought you wanted to filter a form,
>> >> but you want to run an update query.
>> >>
>> >> The current form can be referenced using Screen.ActiveForm. The
>> >> current RecordSource is Screen.ActiveForm.RecordSource.
>> >>
>> >> -Tom.
>> >> Microsoft Access MVP
>> >>
>> >>
>> >>
>> >>
>> >> >I don't think we connected. Normally, I would place a command button on the
>> >> >form to run the Update SQL based on the open form and any filters that may be
>> >> >applied since opening the form. Howver, datasheet view doesn't allow for a
>> >> >command button, so I chose to place a toolbar assigned to the open form and
>> >> >run a Function (On Action property). If I were to place the toolbar on
>> >> >another form with a differ recorsource, I would have to create a new toolbar
>> >> >and function that references the current form. I want to have one toolbar on
>> >> >several different forms and have the Function recognize the Current Form and
>> >> >recordset. Can this be done?
>> >> >
>> >> >"Tom van Stiphout" wrote:
>> >> >
>> >> >> On Sat, 30 Jan 2010 08:12:06 -0800, NEWER USER
>> >> >> <(E-Mail Removed)> wrote:
>> >> >>
>> >> >> There is a better way to filter: rather than changing the
>> >> >> RecordSource: set the Filter and FilterOn property.
>> >> >>
>> >> >> For readability change your MsgBox statement to something like:
>> >> >> If (MsgBox(strMsg, vbYesNo or vbQuestion, "Warning") = vbNo) Then
>> >> >>
>> >> >> -Tom.
>> >> >> Microsoft Access MVP
>> >> >>
>> >> >>
>> >> >> >I have a toolbar that I want to assign to multiple forms that are viewed in
>> >> >> >Datasheet view mode only. I have assigned a Function on the toolbar to run an
>> >> >> >On Acxtion command. Each form has a different Recordsource (qryAll,
>> >> >> >qryTotals, qryParts, etc). The following works well on one form only. As the
>> >> >> >form and Recordsource change, I need to be able to recognize the current
>> >> >> >Recordsouce so I don't have to create a separate toolbar for each form only
>> >> >> >to change qryAll to qryTotals. Any help appreciated.
>> >> >> >
>> >> >> >Function QTag()
>> >> >> > Dim strWhere As String
>> >> >> > Dim strMsg As String
>> >> >> >
>> >> >> > With CodeContextObject
>> >> >> > strWhere = "WHERE " & .Filter
>> >> >> > End With
>> >> >> >
>> >> >> > strMsg = "FILTERED records will be tagged."
>> >> >> > If (MsgBox(strMsg, 273, "Warning") <> 1) Then
>> >> >> > 'Tag filtered records on form
>> >> >> > DoCmd.CancelEvent
>> >> >> > Exit Function
>> >> >> >End If
>> >> >> >
>> >> >> >DoCmd.SetWarnings False
>> >> >> >
>> >> >> >'This is where I need to vary the recorsource below
>> >> >> >DoCmd.RunSQL "UPDATE qryAll " & _
>> >> >> > "SET qryAll.T = '-1'" & strWhere
>> >> >> >
>> >> >> >DoCmd.Requery
>> >> >> >DoCmd.SetWarnings True
>> >> >> >
>> >> >> >End Fuction
>> >> >> .
>> >> >>
>> >> .
>> >>

>> .
>>

 
Reply With Quote
 
NEWER USER
Guest
Posts: n/a
 
      1st Feb 2010
I gave it my best and failed on several attempts Error messages from Type
Mismatch to Object Required. Here is what I have - Object Required Error
returned below.

Function QTagSub()

Dim sql As String
Dim strWhere As String
Dim strMsg As String
Dim frm As Form

With CodeContextObject
strWhere = "WHERE " & .Filter
End With

strMsg = "FILTERED records will be tagged."
If (MsgBox(strMsg, 273, "Warning") <> 1) Then
'Tag filtered records on form
DoCmd.CancelEvent
Exit Function
End If

DoCmd.SetWarnings False
Set frm = Screen.ActiveControl.Name
sql = "update " & frm.RecordSource & " set " & frm.RecordSource & ".T =
'-1'" & strWhere
DoCmd.RunSQL sql
DoCmd.Requery
DoCmd.SetWarnings True
End Function

"Tom van Stiphout" wrote:

> On Sun, 31 Jan 2010 13:05:01 -0800, NEWER USER
> <(E-Mail Removed)> wrote:
>
> Screen.ActiveControl will give you the active control. This may be in
> a subform.
> If it is in a subform then screen.ActiveControl.Parent.Name is not
> equal to screen.ActiveControl.Name.
>
> Can you take it from there?
>
> -Tom.
> Microsoft Access MVP
>
>
>
> >Because this worked so well, I want to go one step further. Suppose I have
> >a Tab Control with multiple subforms on a Main form. You taught me how to
> >reference the Active Main form (below) and I now want to reference the active
> >subform once I have clicked a page on the tab control. How might I do this
> >without a specific subform name?
> >
> >My plan is to build one function and assign it to a command button on
> >several subforms which can perform updates on the same field as I did
> >earlier. I want to get away from using repetitive code only to change the
> >current subform name in the update sql. Can this be done?
> >
> >dim frm as form
> >set frm = screen.activeform
> >
> >"Tom van Stiphout" wrote:
> >
> >> On Sat, 30 Jan 2010 19:33:01 -0800, NEWER USER
> >> <(E-Mail Removed)> wrote:
> >>
> >> You concatenate the correct sql statement. Something like:
> >> dim sql as string
> >> dim frm as form
> >> set frm = screen.activeform
> >> sql = "update " & frm.Recordsource & " set " & frm.recordsource & ".T
> >> = '-1'" & strWhere
> >> docmd.runsql sql
> >>
> >> This would of course assume T is a field in all these queries.
> >>
> >> -Tom.
> >> Microsoft Access MVP
> >>
> >>
> >>
> >> >This is where I stumble. In my code below, what do I replace "qryAll" with?
> >> >As other forms open the Recordsource will be a different query. The
> >> >Recordsource will have to vary (qryTotals, qryParts, etc). Your help is
> >> >appreciated.
> >> >
> >> >DoCmd.RunSQL "UPDATE qryAll " & _
> >> > "SET qryAll.T = '-1'" & strWhere
> >> >
> >> >
> >> >"Tom van Stiphout" wrote:
> >> >
> >> >> On Sat, 30 Jan 2010 12:13:01 -0800, NEWER USER
> >> >> <(E-Mail Removed)> wrote:
> >> >>
> >> >> You're right, sorry about that. I thought you wanted to filter a form,
> >> >> but you want to run an update query.
> >> >>
> >> >> The current form can be referenced using Screen.ActiveForm. The
> >> >> current RecordSource is Screen.ActiveForm.RecordSource.
> >> >>
> >> >> -Tom.
> >> >> Microsoft Access MVP
> >> >>
> >> >>
> >> >>
> >> >>
> >> >> >I don't think we connected. Normally, I would place a command button on the
> >> >> >form to run the Update SQL based on the open form and any filters that may be
> >> >> >applied since opening the form. Howver, datasheet view doesn't allow for a
> >> >> >command button, so I chose to place a toolbar assigned to the open form and
> >> >> >run a Function (On Action property). If I were to place the toolbar on
> >> >> >another form with a differ recorsource, I would have to create a new toolbar
> >> >> >and function that references the current form. I want to have one toolbar on
> >> >> >several different forms and have the Function recognize the Current Form and
> >> >> >recordset. Can this be done?
> >> >> >
> >> >> >"Tom van Stiphout" wrote:
> >> >> >
> >> >> >> On Sat, 30 Jan 2010 08:12:06 -0800, NEWER USER
> >> >> >> <(E-Mail Removed)> wrote:
> >> >> >>
> >> >> >> There is a better way to filter: rather than changing the
> >> >> >> RecordSource: set the Filter and FilterOn property.
> >> >> >>
> >> >> >> For readability change your MsgBox statement to something like:
> >> >> >> If (MsgBox(strMsg, vbYesNo or vbQuestion, "Warning") = vbNo) Then
> >> >> >>
> >> >> >> -Tom.
> >> >> >> Microsoft Access MVP
> >> >> >>
> >> >> >>
> >> >> >> >I have a toolbar that I want to assign to multiple forms that are viewed in
> >> >> >> >Datasheet view mode only. I have assigned a Function on the toolbar to run an
> >> >> >> >On Acxtion command. Each form has a different Recordsource (qryAll,
> >> >> >> >qryTotals, qryParts, etc). The following works well on one form only. As the
> >> >> >> >form and Recordsource change, I need to be able to recognize the current
> >> >> >> >Recordsouce so I don't have to create a separate toolbar for each form only
> >> >> >> >to change qryAll to qryTotals. Any help appreciated.
> >> >> >> >
> >> >> >> >Function QTag()
> >> >> >> > Dim strWhere As String
> >> >> >> > Dim strMsg As String
> >> >> >> >
> >> >> >> > With CodeContextObject
> >> >> >> > strWhere = "WHERE " & .Filter
> >> >> >> > End With
> >> >> >> >
> >> >> >> > strMsg = "FILTERED records will be tagged."
> >> >> >> > If (MsgBox(strMsg, 273, "Warning") <> 1) Then
> >> >> >> > 'Tag filtered records on form
> >> >> >> > DoCmd.CancelEvent
> >> >> >> > Exit Function
> >> >> >> >End If
> >> >> >> >
> >> >> >> >DoCmd.SetWarnings False
> >> >> >> >
> >> >> >> >'This is where I need to vary the recorsource below
> >> >> >> >DoCmd.RunSQL "UPDATE qryAll " & _
> >> >> >> > "SET qryAll.T = '-1'" & strWhere
> >> >> >> >
> >> >> >> >DoCmd.Requery
> >> >> >> >DoCmd.SetWarnings True
> >> >> >> >
> >> >> >> >End Fuction
> >> >> >> .
> >> >> >>
> >> >> .
> >> >>
> >> .
> >>

> .
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to save current filter of Table(ListObject) and to apply it la Smugliy Microsoft Excel Programming 4 20th Nov 2008 12:34 PM
Help - Apply Formatting To Current Row Heiko@Heiko.Edu Microsoft Excel Programming 6 5th Aug 2007 06:24 PM
Apply green to current date, red to 2 days overdue and none if pai =?Utf-8?B?dnBzY2hhcw==?= Microsoft Excel Worksheet Functions 19 7th Sep 2006 02:35 PM
Getting current view to apply to ALL folders =?Utf-8?B?QnJhZA==?= Microsoft Outlook Installation 1 7th Jun 2004 01:37 PM
Apply current view to all folders Tracy Rhodes Microsoft Outlook 1 9th Dec 2003 04:30 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:46 PM.