Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

is there any way to set a forms recordsource before opening it in vba

Posted on 2009-04-14
22
Medium Priority
?
553 Views
Last Modified: 2013-11-28
Hi  there
Is there any way to set a form's recordsource property in Access using VBA before opening the specified form?
Thanks
0
Comment
Question by:caandal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 10
  • 2
22 Comments
 
LVL 1

Expert Comment

by:dprager
ID: 24145103

Me.formname.Form.RecordSource = "SELECT SQL Statement"
docmd.openform "formname"

thinking that should do the trick.
0
 

Author Comment

by:caandal
ID: 24145306
Hi dprager
Here is the code that is running but is producing the attached error.

stDocName = "Template1"
                Forms!Template1.Form.RecordSource = "vTemplate1"
                stLinkCriteria = "[Refid]=" & ReportCheck
                DoCmd.OpenForm stDocName, acNormal, , stLinkCriteria
                Forms![Template1]![FName] = "Cache"
Thanks
Error.jpg
0
 
LVL 1

Expert Comment

by:dprager
ID: 24145409
wich line gives the problem becaus the error is quit clear.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 65

Accepted Solution

by:
rockiroads earned 2000 total points
ID: 24145457
You have to first open the form eg

'Open Template1 for edit but hidden
DoCmd.OpenForm "Template1", acDesign, , , acFormEdit, acHidden
Forms!Template1.Form.RecordSource = "vTemplate1"
'Close form, save changes
DoCmd.Close acForm, "Template1", acSaveYes

'Open Form
stDocName = "Template1"
stLinkCriteria = "[Refid]=" & ReportCheck
DoCmd.OpenForm stDocName, acNormal, , stLinkCriteria
Forms![Template1]![FName] = "Cache"

0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24145465
dprager has shown you how to set the recordsource, I just told you that you need to open the form first.

What is vTemplate1, is this the name of a query? if so then it will be fine else do what dprager says, use sql or tablename
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24145469
urm vTemplate1 a query or table? thats what I meant
0
 

Author Comment

by:caandal
ID: 24145540
Hi Guys

I have no problem opening the form normally  But I need to be able to change the recordsource of the form before I open it.  The system is not finding the form Template1 becuase it is not open - hence my question
0
 

Author Comment

by:caandal
ID: 24145543
vTemplate1 is a sql view that I have an ODBC link to
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24145833
If you manually set the forms recordsource to vTemplate1, does it work fine?

Is there any difference between vTemplate1 and the current form's recordsource?

Did u try opening the form as hidden edit, then changing the recordsource, then closing it?
0
 

Author Comment

by:caandal
ID: 24145913
Hi there

Yes it works fine.  vTemplate1 returns all the records in the recordset the current record source is also a view but only returns a few rows based on the where clause in the query
0
 

Author Comment

by:caandal
ID: 24145941
never opened a form as hidden edit but it sounds good.  What do I have to specify in the code?  Will the form the retain that recordsource once I close it?
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24146401
I gave a code example earlier

it does retain that recordsource, yes. if you dont then, it needs to be reset, maybe on exit of form or after form called
0
 

Author Closing Comment

by:caandal
ID: 31570286
Star you are  - burning the candle at both ends - did not see it.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24146900
Cool. Bet your enjoying the cricket at the moment, whooping the aussies :)
0
 

Author Comment

by:caandal
ID: 24147301
Yip enjoying that I must admit - how're things on your side?
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24147404
All good man, thanks for asking :) Currently waiting for the IPL to start, looking forward to that.
0
 

Author Comment

by:caandal
ID: 24147533
I think that is going to be brilliant - all the damn tickets were sold out here in about 2 and half hours
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24148390
I was disappointed a little since there was an outside chance of being held in the UK. Still you have the better weather. 2.5 hours is incredible. Im hoping it is as good as it was last year. There is a danger of it being better than the 20/20 world cup! which incidentally is in UK and Im going too :)
0
 

Author Comment

by:caandal
ID: 24148835
Ahhh Noooo!
Refer to Beached whale at
http://www.youtube.com/results?search_type=&search_query=beached+whale&aq=0&oq=beached

You lucky Chap - I would love to be there
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24165889
Evens out I guess, u got IPL and we got 20/20. atmosphere should be good in both tournaments
first thing I noticd in that link was the fat woman getting stuck!
0
 

Author Comment

by:caandal
ID: 24166776
Cheers  go well enjoy
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24167029
Thanks :)
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question