473,394 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

set Form properties if Form is not open

I did a few searches, but couldn't come up with a clear answer to my
question. So here it is: If I am in an open form (MyForm1), is it
possible to set the RecordSource of an un-opened form (MyForm2). I
want to use the OnClose Event of MyForm1. But I do not want to have
the MyForm2 open when this happens. My wish it to have the
RecordSource of MyForm2 permanently changed by the OnClose Event of
MyForm1. Can this be done ?

Thanks for any suggestions, Scott

Mar 23 '06 #1
15 6432
ko******@yahoo.com wrote:
I did a few searches, but couldn't come up with a clear answer to my
question. So here it is: If I am in an open form (MyForm1), is it
possible to set the RecordSource of an un-opened form (MyForm2). I
want to use the OnClose Event of MyForm1. But I do not want to have
the MyForm2 open when this happens. My wish it to have the
RecordSource of MyForm2 permanently changed by the OnClose Event of
MyForm1. Can this be done ?

Thanks for any suggestions, Scott

A couple of options.

Docmd.Openform "FormName"
Forms!FormName.Form.Recordsource = whatever

You could also open the form in design mode, hidden. Modify the
recordsource, and close/save it. Then open it.

Mar 23 '06 #2
<ko******@yahoo.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
I did a few searches, but couldn't come up with a clear answer to my
question. So here it is: If I am in an open form (MyForm1), is it
possible to set the RecordSource of an un-opened form (MyForm2). I
want to use the OnClose Event of MyForm1. But I do not want to have
the MyForm2 open when this happens. My wish it to have the
RecordSource of MyForm2 permanently changed by the OnClose Event of
MyForm1. Can this be done ?

Thanks for any suggestions, Scott

I would not do this by altering the design view. I would store this string
as some form of setting - perhaps in a settings table or perhaps as a
database property or even a registry entry. Then each time the form opened,
it would look up it's value and set the recordsource accordingly.
To the end user, this would look like the design had changed each time the
form was closed, it would just be implemented differently.
Mar 23 '06 #3
Wow ! That was quick. Thanks, I will try that. ~Scott

Mar 23 '06 #4
No, you can't do what you want.

Salad has given you some alternatives.

Another would be to write the RecordSource to a Table, and have MyForm2 open
the Table from code in its Open event and reset its own RecordSource. That
may not work so well, depending on the details of your application, if you
have multiple users (though there are probably workarounds for most of those
potential problems, too.)

Another approach would be to use the name of a Query as the Record Source,
and instead of changing the RecordSource, replace the SQL property of that
Query. That, effectively, permanently changes the RecordSource without ever
opening the Form, either in Form or Design View.

What kind of changes are you making to the RecordSource? Generally the only
kind that make sense are those selecting or llimiting which Records are to
be processed. Certainly, for a bound Form to work, both the old and new
RecordSources would have to have Fields with the same names.

The only way to permanently change the RecordSource is to do it with the
Form open in Design View (but could certainly be hidden) then close the
Form.

Larry Linson
Microsoft Access MVP

<ko******@yahoo.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
I did a few searches, but couldn't come up with a clear answer to my
question. So here it is: If I am in an open form (MyForm1), is it
possible to set the RecordSource of an un-opened form (MyForm2). I
want to use the OnClose Event of MyForm1. But I do not want to have
the MyForm2 open when this happens. My wish it to have the
RecordSource of MyForm2 permanently changed by the OnClose Event of
MyForm1. Can this be done ?

Thanks for any suggestions, Scott

Mar 23 '06 #5
I like the option of opening the form in Design view something like
this:
DoCmd.OpenForm MyForm1, , , , , acHidden
Forms!MyForms.Form.RecordSource = "newQry"
DoCmd.Close acForm, MyForm1, acSaveYes
Would that work ?
The recordsource is a Query that limits a users view of previous year's
data. I suppose I could change the SQL property for the query, but not
sure how to make that change permanent either.

~Scott

Mar 23 '06 #6
<ko******@yahoo.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
I like the option of opening the form in Design view something like
this:
DoCmd.OpenForm MyForm1, , , , , acHidden
Forms!MyForms.Form.RecordSource = "newQry"
DoCmd.Close acForm, MyForm1, acSaveYes
Would that work ?
The recordsource is a Query that limits a users view of previous year's
data. I suppose I could change the SQL property for the query, but not
sure how to make that change permanent either.

~Scott


If you used the function below, you could write something like:

If Not DefineQuery("MyQuery","SELECT * FROM MyTable WHERE MyYear<2006") Then
Beep
End If

So you always have the same query, named MyQuery, you just change its
definition.
Public Function DefineQuery(strQuery As String, _
strSQL As String) As Boolean

On Error GoTo Err_Handler

Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef

Set dbs = CurrentDb

Set qdf = dbs.QueryDefs(strQuery)

qdf.SQL = strSQL

DefineQuery = True

Exit_Handler:

If Not qdf Is Nothing Then
Set qdf = Nothing
End If

If Not dbs Is Nothing Then
Set dbs = Nothing
End If

Exit Function

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Function
Mar 23 '06 #7
I don't recall ever using acHidden, but I have, at times used
DoCmd.Echo False before my code to freeze the display, run the code, and
DoCmd.Echo True after the code to put things back to normal.

If you do that, be very, very sure that the code runs without error.
Otherwise the screen won't be responding and you will have the Devil of a
time getting back to normal. When I use it, I run and test and test and test
without setting Echo off -- and still, sometimes, I worry.

Larry Linson
Microsoft Access MVP
Larry Linson
Microsoft Access MVP
<ko******@yahoo.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
I like the option of opening the form in Design view something like
this:
DoCmd.OpenForm MyForm1, , , , , acHidden
Forms!MyForms.Form.RecordSource = "newQry"
DoCmd.Close acForm, MyForm1, acSaveYes
Would that work ?
The recordsource is a Query that limits a users view of previous year's
data. I suppose I could change the SQL property for the query, but not
sure how to make that change permanent either.

~Scott

Mar 24 '06 #8
DFS
Larry Linson wrote:
I don't recall ever using acHidden, but I have, at times used
DoCmd.Echo False before my code to freeze the display, run the code,
and DoCmd.Echo True after the code to put things back to normal.

If you do that, be very, very sure that the code runs without error.
Otherwise the screen won't be responding and you will have the Devil
of a time getting back to normal. When I use it, I run and test and
test and test without setting Echo off -- and still, sometimes, I
worry.
No need to worry. Just add Application.Echo True into the error handling.

Larry Linson
Microsoft Access MVP
Larry Linson
Microsoft Access MVP
<ko******@yahoo.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
I like the option of opening the form in Design view something like
this:
DoCmd.OpenForm MyForm1, , , , , acHidden
Forms!MyForms.Form.RecordSource = "newQry"
DoCmd.Close acForm, MyForm1, acSaveYes
Would that work ?
The recordsource is a Query that limits a users view of previous
year's data. I suppose I could change the SQL property for the
query, but not sure how to make that change permanent either.

~Scott

Mar 24 '06 #9

<ko******@yahoo.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
I like the option of opening the form in Design view something like
this:
DoCmd.OpenForm MyForm1, , , , , acHidden
Forms!MyForms.Form.RecordSource = "newQry"
DoCmd.Close acForm, MyForm1, acSaveYes
Would that work ?
The recordsource is a Query that limits a users view of previous year's
data. I suppose I could change the SQL property for the query, but not
sure how to make that change permanent either.

~Scott


If you change the SQL property of a Query at runtime, it stays changed
(unlike changes to the design of a Form). Just to make certain my memory
wasn't playing any tricks on me, I just tried and verified that.

What, particularly, determines the year you want displayed? Is it always the
year prior to the current year, or is it always the current year, or do you
allow the user to specify it somehow?

Larry Linson
Microsoft Access MVP
Larry Linson
Microsoft Access MVP
Mar 24 '06 #10
"DFS" <nospam@dfs_.com> wrote
No need to worry. Just add Application.Echo True
into the error handling.


Yes, that is the solution, but as I sit there and the code takes longer than
I remembered it taking, I wonder, "Hmm. Did I add it?" And, even more to the
point, "Did something happen so my code is in an unending loop?"

Larry Linson
Microsoft Access MVP
Mar 24 '06 #11
ko******@yahoo.com wrote:
I like the option of opening the form in Design view something like
this:
DoCmd.OpenForm MyForm1, , , , , acHidden
Forms!MyForms.Form.RecordSource = "newQry"
DoCmd.Close acForm, MyForm1, acSaveYes
Would that work ?
The recordsource is a Query that limits a users view of previous year's
data. I suppose I could change the SQL property for the query, but not
sure how to make that change permanent either.

~Scott

I normally don't change the recordsource except in specific situations.
Here is what I do. I open the form and pass an argument.
DoCmd.OpenForm "SomeForm", , , , , , "Test"

In the OnOpen event for the form you can check the argument. Let's say
50%+ of the time you are going to use the recordset used when the form
was designed. Then you could check for arguments and update the
recordsource. Let's say the recordsource is currently Query1. You
could do something like this if "Test" is passed.
If Me.OpenArgs = "Test" Then Me.Recordsource = Query2
This changes the recordsource only if the argument is Test.

For whatever reason...I prefer setting/changing my recordsource in the form.
Mar 24 '06 #12
Thanks for all the suggestions. I wish my Access knowledge was
sufficient to figure out how to make this easy. But I am still
struggling. I have the following code behind a button that I can't get
to work. It hangs up on the Forms! argument, giving me this error:
"Type declaration character does not match declared data type."

'>>> Code behind On-Click button of Form1
Dim MyForm As String, MySource As String
Dim MyEdit As Boolean, MyDelete As Boolean

MyTarget = chr(34) & Me.FormName & chr(34)
MySource = Me.DataSource
MyEdit = Me.EditOption
MyDelete = Me.Add_Delete

DoCmd.OpenForm MyForm, , , , , acHidden
Forms!(MyTarget).Form.RecordSource = MySource
Forms!(MyTarget).Form.AllowEdits = MyEdit
Forms!(MyTarget).Form.AllowAdditions = MyDelete
Forms!(MyTarget).Form.AllowDeletions = MyDelete
DoCmd.Close acForm, MyForm, acSaveYes

My fear is that I am way off base with this approach. Any thoughts ?
Should I be declaring MyTarget as a Form object?
-Scott

Mar 27 '06 #13
ko******@yahoo.com wrote in news:1143486125.365016.79760
@u72g2000cwu.googlegroups.com:
Thanks for all the suggestions. I wish my Access knowledge was
sufficient to figure out how to make this easy. But I am still
struggling. I have the following code behind a button that I can't get
to work. It hangs up on the Forms! argument, giving me this error:
"Type declaration character does not match declared data type."

'>>> Code behind On-Click button of Form1
Dim MyForm As String, MySource As String
Dim MyEdit As Boolean, MyDelete As Boolean

MyTarget = chr(34) & Me.FormName & chr(34)
MySource = Me.DataSource
MyEdit = Me.EditOption
MyDelete = Me.Add_Delete

DoCmd.OpenForm MyForm, , , , , acHidden
Forms!(MyTarget).Form.RecordSource = MySource
Forms!(MyTarget).Form.AllowEdits = MyEdit
Forms!(MyTarget).Form.AllowAdditions = MyDelete
Forms!(MyTarget).Form.AllowDeletions = MyDelete
DoCmd.Close acForm, MyForm, acSaveYes

My fear is that I am way off base with this approach. Any thoughts ?
Should I be declaring MyTarget as a Form object?
-Scott


I haven't used this really clumsy Forms!(SomeFormName) for many years and
have no idea why it's recommended by seemingly all. Regardless, if MyTarget
is a string, as it seems to be, then the chr(34)'s in
MyTarget = chr(34) & Me.FormName & chr(34)
are redundant.

--
Lyle Fairfield
Mar 27 '06 #14
I have tried it with and without the forced quotation marks, but I get
the same error. -Scott

Mar 28 '06 #15
Any other thoughts on this ?
I have the following code behind a button that I can't get to work. It
hangs up on the Forms! argument, giving me this error:
"Type declaration character does not match declared data type."

'>>> Code behind On-Click button of Form1
Dim MyForm As String, MySource As String
Dim MyEdit As Boolean, MyDelete As Boolean
MyTarget = Me.FormName
MySource = Me.DataSource
MyEdit = Me.EditOption
MyDelete = Me.Add_Delete
DoCmd.OpenForm MyForm, , , , , acHidden
Forms!(MyTarget).Form.RecordSource = MySource
Forms!(MyTarget).Form.AllowEdits = MyEdit
Forms!(MyTarget).Form.AllowAdditions = MyDelete
Forms!(MyTarget).Form.AllowDeletions = MyDelete
DoCmd.Close acForm, MyForm, acSaveYes

Should I be declaring MyTarget as a Form object? Is there a more
elegant way to get this to work ?
-Scott

Mar 30 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nancy | last post by:
Hi, I am working on a project where I have to make a form list static, i.e. put in links so that users can choose a local file. As it stands, it's looking for a php file. Here's an example of the...
2
by: ColinWard | last post by:
Hi. I have a form which has as its recordsource an SQL string. The SQL String is as follows: SELECT * from CONTACTS where false. this ensures that there is no data loaded in the form when the...
25
by: Lyn | last post by:
Hi, I am working on a genealogy form. The only table (so far) lists everybody in the family, one record per person. Each record has an autonum ID. The parent form (frmMainForm) displays the...
19
by: Raposa Velha | last post by:
Hello to all! Does any of you want to comment the approach I implement for instantiating a form? A description and an example follow. Cheers, RV jmclopesAThotmail.com replace the AT with the...
1
by: Susan Bricker | last post by:
Greetings. I am trying to position opened forms so that they are cascaded on the screen. I have discovered the movesize action (for the DoCmd) and Move property of a form (for Acc 2002/2003). ...
3
by: James | last post by:
Has anyone written a utility to convert a C# form to C++.net? i.e. to convert "using System.Data" to "using namespace System::Data" etc
2
by: tractng | last post by:
Guys, I really need help with this. I need to creat a login page for the website. Its an existing site that connects to SQL 7 with a local user in the database called 'maya'. It was...
6
by: dbuchanan | last post by:
VS2005 I've been reading all the help I can on the topic (MSDN, other) but I can't make sense of this. Desired behavior; The user is to choose from the displayed list of the databound combobox...
6
by: Brian Blair | last post by:
I have created a input form that enters a number in a talble. If I open the form again it enters a new record instead of editing the existing record. It seems like it should be very basic but I...
12
by: Rob | last post by:
Let's say you open Form1 that contains TabControl1 There are several tabs on TabControl1 Now you open a new Form2 that contains a User Control How can you determine the Selected tab in Form1...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.