PC Review


Reply
Thread Tools Rate Thread

Assign control source in code

 
 
hollyylloh
Guest
Posts: n/a
 
      23rd Apr 2010
I have been assigning the control source to my reports in code like this:

If Me!ClientID = "ABC" Then
DoCmd.OpenReport "rptABC", acViewDesign
Reports!rptABC.RecordSource = "qryABC"
DoCmd.Save acReport, "rptABC"
DoCmd.OpenReport "rptABC", acViewPreview
Else...

However, I need to convert the database to an mde file, thus removing the
ability to go into design view. Is there another way to assign the control
source of a report in code? This way will not work with an mde file because
it tries to enter design view.

Thank you in advance.
 
Reply With Quote
 
 
 
 
Stuart McCall
Guest
Posts: n/a
 
      23rd Apr 2010
"hollyylloh" <(E-Mail Removed)> wrote in message
news:35109FF8-CB03-424F-B22F-(E-Mail Removed)...
>I have been assigning the control source to my reports in code like this:
>
> If Me!ClientID = "ABC" Then
> DoCmd.OpenReport "rptABC", acViewDesign
> Reports!rptABC.RecordSource = "qryABC"
> DoCmd.Save acReport, "rptABC"
> DoCmd.OpenReport "rptABC", acViewPreview
> Else...
>
> However, I need to convert the database to an mde file, thus removing the
> ability to go into design view. Is there another way to assign the control
> source of a report in code? This way will not work with an mde file
> because
> it tries to enter design view.
>
> Thank you in advance.


You could change the SQL string in qryABC instead:

With CurrentDb.QueryDefs!qryABC
.SQL = "<whatever>"
End With

then open the report as usual.


 
Reply With Quote
 
Dirk Goldgar
Guest
Posts: n/a
 
      23rd Apr 2010
"hollyylloh" <(E-Mail Removed)> wrote in message
news:35109FF8-CB03-424F-B22F-(E-Mail Removed)...
>I have been assigning the control source to my reports in code like this:
>
> If Me!ClientID = "ABC" Then
> DoCmd.OpenReport "rptABC", acViewDesign
> Reports!rptABC.RecordSource = "qryABC"
> DoCmd.Save acReport, "rptABC"
> DoCmd.OpenReport "rptABC", acViewPreview
> Else...
>
> However, I need to convert the database to an mde file, thus removing the
> ability to go into design view. Is there another way to assign the control
> source of a report in code? This way will not work with an mde file
> because
> it tries to enter design view.



Pass the desired recordsource to the report via OpenArgs:

DoCmd.OpenReport "rptABC", acViewPreview, _
OpenArgs:="qryABC"

In the report's Open event, get the value of OpenArgs and assign it to the
RecordSource property:

Private Sub Report_Open(Cancel As Integer)

Dim stArgs As string

strArgs = Me.OpenArgs & vbNullString

If Len(strArgs) > 0 Then
Me.RecordSource = strArgs
End If

End Sub


--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)

 
Reply With Quote
 
hollyylloh
Guest
Posts: n/a
 
      23rd Apr 2010
Dirk,

Thank you so much! That worked and is so much simpler.

"Dirk Goldgar" wrote:

> "hollyylloh" <(E-Mail Removed)> wrote in message
> news:35109FF8-CB03-424F-B22F-(E-Mail Removed)...
> >I have been assigning the control source to my reports in code like this:
> >
> > If Me!ClientID = "ABC" Then
> > DoCmd.OpenReport "rptABC", acViewDesign
> > Reports!rptABC.RecordSource = "qryABC"
> > DoCmd.Save acReport, "rptABC"
> > DoCmd.OpenReport "rptABC", acViewPreview
> > Else...
> >
> > However, I need to convert the database to an mde file, thus removing the
> > ability to go into design view. Is there another way to assign the control
> > source of a report in code? This way will not work with an mde file
> > because
> > it tries to enter design view.

>
>
> Pass the desired recordsource to the report via OpenArgs:
>
> DoCmd.OpenReport "rptABC", acViewPreview, _
> OpenArgs:="qryABC"
>
> In the report's Open event, get the value of OpenArgs and assign it to the
> RecordSource property:
>
> Private Sub Report_Open(Cancel As Integer)
>
> Dim stArgs As string
>
> strArgs = Me.OpenArgs & vbNullString
>
> If Len(strArgs) > 0 Then
> Me.RecordSource = strArgs
> End If
>
> End Sub
>
>
> --
> Dirk Goldgar, MS Access MVP
> Access tips: www.datagnostics.com/tips.html
>
> (please reply to the newsgroup)
>

 
Reply With Quote
 
hollyylloh
Guest
Posts: n/a
 
      23rd Apr 2010
Stuart,

Interesting, so in this way I create one query and then store the multiple
SQL strings that I need in code, correct?

I am curious what are the advantages, disadvantages of each of these two
approaches (Stuarts and Dirks).

Thank you!

"Stuart McCall" wrote:

> "hollyylloh" <(E-Mail Removed)> wrote in message
> news:35109FF8-CB03-424F-B22F-(E-Mail Removed)...
> >I have been assigning the control source to my reports in code like this:
> >
> > If Me!ClientID = "ABC" Then
> > DoCmd.OpenReport "rptABC", acViewDesign
> > Reports!rptABC.RecordSource = "qryABC"
> > DoCmd.Save acReport, "rptABC"
> > DoCmd.OpenReport "rptABC", acViewPreview
> > Else...
> >
> > However, I need to convert the database to an mde file, thus removing the
> > ability to go into design view. Is there another way to assign the control
> > source of a report in code? This way will not work with an mde file
> > because
> > it tries to enter design view.
> >
> > Thank you in advance.

>
> You could change the SQL string in qryABC instead:
>
> With CurrentDb.QueryDefs!qryABC
> .SQL = "<whatever>"
> End With
>
> then open the report as usual.
>
>
> .
>

 
Reply With Quote
 
Stuart McCall
Guest
Posts: n/a
 
      23rd Apr 2010
"hollyylloh" <(E-Mail Removed)> wrote in message
news:141A1F59-3E34-4E63-8A24-(E-Mail Removed)...
> Stuart,
>
> Interesting, so in this way I create one query and then store the multiple
> SQL strings that I need in code, correct?


Yep, that's one way to do it. Just knowing of the technique could be useful
to you sometime.

>
> I am curious what are the advantages, disadvantages of each of these two
> approaches (Stuarts and Dirks).


I don't think one or the other has the advantage. Just two ways to achieve
the same goal.

>
> Thank you!
>
> "Stuart McCall" wrote:
>
>> "hollyylloh" <(E-Mail Removed)> wrote in message
>> news:35109FF8-CB03-424F-B22F-(E-Mail Removed)...
>> >I have been assigning the control source to my reports in code like
>> >this:
>> >
>> > If Me!ClientID = "ABC" Then
>> > DoCmd.OpenReport "rptABC", acViewDesign
>> > Reports!rptABC.RecordSource = "qryABC"
>> > DoCmd.Save acReport, "rptABC"
>> > DoCmd.OpenReport "rptABC", acViewPreview
>> > Else...
>> >
>> > However, I need to convert the database to an mde file, thus removing
>> > the
>> > ability to go into design view. Is there another way to assign the
>> > control
>> > source of a report in code? This way will not work with an mde file
>> > because
>> > it tries to enter design view.
>> >
>> > Thank you in advance.

>>
>> You could change the SQL string in qryABC instead:
>>
>> With CurrentDb.QueryDefs!qryABC
>> .SQL = "<whatever>"
>> End With
>>
>> then open the report as usual.
>>
>>
>> .
>>



 
Reply With Quote
 
hollyylloh
Guest
Posts: n/a
 
      24th Apr 2010
Thanks Stuart, I will add that to my arsenal. I think the example Dirk gave
me makes better sense with this particular task. I look forward to using your
method in the future.

"Stuart McCall" wrote:

> "hollyylloh" <(E-Mail Removed)> wrote in message
> news:141A1F59-3E34-4E63-8A24-(E-Mail Removed)...
> > Stuart,
> >
> > Interesting, so in this way I create one query and then store the multiple
> > SQL strings that I need in code, correct?

>
> Yep, that's one way to do it. Just knowing of the technique could be useful
> to you sometime.
>
> >
> > I am curious what are the advantages, disadvantages of each of these two
> > approaches (Stuarts and Dirks).

>
> I don't think one or the other has the advantage. Just two ways to achieve
> the same goal.
>
> >
> > Thank you!
> >
> > "Stuart McCall" wrote:
> >
> >> "hollyylloh" <(E-Mail Removed)> wrote in message
> >> news:35109FF8-CB03-424F-B22F-(E-Mail Removed)...
> >> >I have been assigning the control source to my reports in code like
> >> >this:
> >> >
> >> > If Me!ClientID = "ABC" Then
> >> > DoCmd.OpenReport "rptABC", acViewDesign
> >> > Reports!rptABC.RecordSource = "qryABC"
> >> > DoCmd.Save acReport, "rptABC"
> >> > DoCmd.OpenReport "rptABC", acViewPreview
> >> > Else...
> >> >
> >> > However, I need to convert the database to an mde file, thus removing
> >> > the
> >> > ability to go into design view. Is there another way to assign the
> >> > control
> >> > source of a report in code? This way will not work with an mde file
> >> > because
> >> > it tries to enter design view.
> >> >
> >> > Thank you in advance.
> >>
> >> You could change the SQL string in qryABC instead:
> >>
> >> With CurrentDb.QueryDefs!qryABC
> >> .SQL = "<whatever>"
> >> End With
> >>
> >> then open the report as usual.
> >>
> >>
> >> .
> >>

>
>
> .
>

 
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
Re: Assign control source to popup form Allen Browne Microsoft Access Forms 1 2nd Feb 2010 04:39 PM
Assign control source to unbound text box TeeSee Microsoft Access Forms 7 13th Jan 2009 12:54 AM
How to add fields and text boxes and assign control source to the. SuperNerd Microsoft Access VBA Modules 1 19th Sep 2008 06:16 PM
How to assign a control source to a unbound text box Faraa.Oh@gmail.com Microsoft Access 1 3rd Jun 2008 11:00 PM
Assign the Control Source for a Label and a Text Box Dynamically =?Utf-8?B?Um9u?= Microsoft Access Form Coding 2 25th Sep 2006 04:18 PM


Features
 

Advertising
 

Newsgroups
 


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