473,385 Members | 1,588 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,385 software developers and data experts.

"On Open" Event; applying a query to open a form

kcdoell
230 100+
Hello:

I have a form that I want to open using a filter that I have created. I have done this usually by pointing the record source of the form to the query/filter that I created. In this new project, I want to open the form using the "On Open" event, via the properties of the form, and leave my record source pointing to the original table that was used to set it up.

My problem is that I am not too sure what the vba coding would be. This particular form has a subform that displays data. I later want to place a command button on the form that will change the sort of the displayed dataset if the user indicates what sort they want the table/dataset to be in.

Does anybody have any ideas they could give me?

Thanks,

Keith.
Feb 6 '08 #1
7 2658
MMcCarthy
14,534 Expert Mod 8TB
Not sure I fully understand what you are looking for but ...

Me.filter = "[Fieldname]=" & someValue
Me.filterOn
Feb 6 '08 #2
kcdoell
230 100+
Thanks for the reply!

I tried to apply the Me.filter idea by putting the following code in the "on Open" property of the form:

Private Sub Form_Load()
Me.Filter = "QryForecastform"
End Sub

Can I reference a query (like I am doing) because it currently does not work? I basically just want the form to initially load up using a filter that I created in Access called "QryForecastform".

Any ideas?

Thanks

Keith.
Feb 7 '08 #3
MMcCarthy
14,534 Expert Mod 8TB
Thanks for the reply!

I tried to apply the Me.filter idea by putting the following code in the "on Open" property of the form:

Private Sub Form_Load()
Me.Filter = "QryForecastform"
End Sub

Can I reference a query (like I am doing) because it currently does not work? I basically just want the form to initially load up using a filter that I created in Access called "QryForecastform".

Any ideas?

Thanks

Keith.
Thats not a filter its a query so you would reset the record source of the form.

Private Sub Form_Load()
Me.RecordSource = "QryForecastform"
End Sub
Feb 7 '08 #4
kcdoell
230 100+
Thanks, that worked but I discovered that it only worked if I put the code in both the Main form and the Sub form (when I created the form I have a subform embedded in it). Not too sure why....

I have another quick question that is related; on the form I want to change the way the subform is displaying the info (the sort order). That is to say that the subform is in a datasheet view and I want to give the user the ability to see the same information in different sort orders (By Name, Location, Product, etc).

I placed a combo box on my form called "cobSort" and in there is a drop down list to choose from (Name, Location, Product, etc). Then I put a command button called "cmdReSort_Tbl" and on the onclick property I put the following code:

Private Sub cmdReSort_Tbl_Click()
'When user makes a selection on the Sort Table dropdown menu the table will requery
' via that sort parameter

If cobSort = "Name" Then
Me.RecordSource = "UWQryFormSort"
End If

End Sub

_______________


You can see that I have another query called "UWQryFormSort" that is exactly the same as my first one except I put this one in ascending order under the field called "Name". When I clicked on the button it does not change the sort order of the table though I did confirm that my query does.

What am I doing wrong? I thought that the Me.RecordSource method would work in this new event.

Thanks again,

Keith.
Feb 7 '08 #5
jaxjagfan
254 Expert 100+
Thanks, that worked but I discovered that it only worked if I put the code in both the Main form and the Sub form (when I created the form I have a subform embedded in it). Not too sure why....

I have another quick question that is related; on the form I want to change the way the subform is displaying the info (the sort order). That is to say that the subform is in a datasheet view and I want to give the user the ability to see the same information in different sort orders (By Name, Location, Product, etc).

I placed a combo box on my form called "cobSort" and in there is a drop down list to choose from (Name, Location, Product, etc). Then I put a command button called "cmdReSort_Tbl" and on the onclick property I put the following code:

Private Sub cmdReSort_Tbl_Click()
'When user makes a selection on the Sort Table dropdown menu the table will requery
' via that sort parameter

If cobSort = "Name" Then
Me.RecordSource = "UWQryFormSort"
End If

End Sub

_______________


You can see that I have another query called "UWQryFormSort" that is exactly the same as my first one except I put this one in ascending order under the field called "Name". When I clicked on the button it does not change the sort order of the table though I did confirm that my query does.

What am I doing wrong? I thought that the Me.RecordSource method would work in this new event.

Thanks again,

Keith.
If using the combo to define the column to sort - should be something like this:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdReSort_Tbl_Click()
  2. Dim strCtl
  3. strCtl = Me.cobSort
  4. Docmd.GoToControl strCtl
  5. DoCmd.RunCommand acCmdSortAscending
  6. End Sub
  7.  
Feb 7 '08 #6
kcdoell
230 100+
I receive an error message (runtime 2109) indicating that "There is no field name 'Name' in the current record"

Could that be happening because the code is tied to the form and not the subform that is displaying the table?

Keith.
Feb 7 '08 #7
kcdoell
230 100+
You guys were great.... I played around with it and got it to work. All I needed to do was change my subform's default view from a datasheet to a continuous form and then I embedded my cobSort in the header of the subform. I also got rid of the command button and put the code in the after update event of my cobSort control to make it look cleaner and to save a click.

Thanks a lot!

:-)

Keith.
Feb 7 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: John Baker | last post by:
Hi: Does "On Open" code execute before or after related data is loaded? I want to test before the form appears on the screen to see if there is any data in the queryresult, and if there is not...
7
by: Aaron G via AccessMonster.com | last post by:
Wanted to share a solution to something which I didn't find on the net: EVERY form in my Microsoft Access 2002 database gave an error any time any code was to be called: form OnOpen, button...
2
by: Serge Klokov | last post by:
Hi! 1. Please, help with example "paint on form by mouse" 2. Below is my example, but it clear the line after each Refresh()... how to fix? 3. How to draw the line in Mouse_Move event? ...
15
by: Billy | last post by:
Anyone know if this a bug in VB.NET 2002 and how to overcame that situation? I have a MDI form from where I call MDI child form like that: Dim frm As New frmChild() frm.MdiParent = Me...
6
by: Grumpy Aero Guy | last post by:
I would like to add functionality to a form that contains a check box with which the end user can check it so that this particular form won't appear again. I. e., a "Don't show this form again"...
1
by: S. van Beek | last post by:
Dear reader, First of all a happy new year. In Report event there is the "On No Date" event but in a Form this event is not available.
2
by: jerry.ranch | last post by:
I'm starting to learn about the tab control. How would I write an on open event procedure in VBA, that upon opening of the form, a specific tab opens (say tab 1)? Thanks Jerry
3
by: RR | last post by:
I have a button on a form (form A) that opens another form. The form that opens (form B) has a listbox that is populated with a call to a function in the "on current" event. When form B with the...
3
by: Joanne | last post by:
I have searched through previous threads regarding this error. I went back through all my code to make sure I closed and set to nothing all open db's and rs's. However, I still have one computer...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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.