Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Trouble creating a RecordSource for an unbound subform

Posted on 2011-02-20
4
Medium Priority
?
490 Views
Last Modified: 2013-11-28
I have looked at many posts here and on other sites and still cannot resolve my problem. I get multiple error messages when trying to dynamically add a recordsource to an unbound subform. Here is my latest attempt:

    Dim strUser As String
    Dim frm As Form
    strUser = Forms.frmLogin.txtPassword
   
    MsgBox strUser
    Me.frmSelectWeek.Form.RecordSource = ""
   
    Me.frmSelectWeek.Form.Visible = False
   
    'DoCmd.SetWarnings False
    DoCmd.DeleteObject acTable, "tblSelectWeek_Temp"
       
    DoCmd.OpenQuery "qrySelectWeek"
   
    Dim strSQL, strSQL1 As String
    Dim strTableName As String
   
    strTableName = strUser & "_ACTIVITY"
    strSQL = "CREATE TABLE " & strTableName & "(ACTDATE Date, CLIENT  Int, ACTIVITY Int, COMPLETE date, HOURS Int, COMMENT char(225)) "

    strSQL1 = "INSERT INTO " & strTableName & "( ACTDATE, CLIENT, ACTIVITY, COMPLETE, HOURS, COMMENT ) " & _
    "SELECT tblLog.ACTDATE, lkpClient.CLIENT, lkpActivity.ACTIVITY, tblLog.COMPLETE, tblLog.HOURS, tblLog.COMMENT " & _
    "FROM (lkpActivity INNER JOIN tblLog ON lkpActivity.ACTIVITYID = tblLog.ACTIVITYID) INNER JOIN lkpClient ON tblLog.CLIENTID = lkpClient.CLIENTID " & _
    "WHERE (((tblLog.ACTDATE) Between [forms].[frmActivityLog].[txtBegDate] And [forms].[frmActivityLog].[txtEndDate]) AND ((tblLog.USERID)=[forms].[frmLogin].[txtPassword]))"

    DoCmd.RunSQL strSQL
    DoCmd.RunSQL strSQL1

    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset

    Set cn = CurrentProject.Connection
    Set rs = New ADODB.Recordset

    rs.Open strTableName, cn

        Me.frmSelectWeek.Form.RecordSource = rs '------------- Error Here - Type Mismatch
        Me.frmSelectWeek.Form.CLIENT.ControlSource = "CLIENT"
        Me.frmSelectWeek.Form.ACTDATE.ControlSource = "ACTDATE"
        Me.frmSelectWeek.Form.ACTIVITY.ControlSource = "ACTIVITY"
        Me.frmSelectWeek.Form.COMPLETE.ControlSource = "COMPLETE"
        Me.frmSelectWeek.Form.HOURS.ControlSource = "HOURS"
        Me.frmSelectWeek.Form.COMMENT.ControlSource = "COMMENT"
               
    Me!frmSelectWeek.Form.Requery
    Me!frmSelectWeek.Form.Visible = True
0
Comment
Question by:RR345
[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
  • 2
  • 2
4 Comments
 
LVL 85
ID: 34939178
If you're going to use a Recordset for the Form, you must set the Recordset property, not the Recordsource property:

Set Me.frmSelectWeek.Form.RecordSET = rs

Note also that you need to set the CursorLocation before you apply the Recordset:

rs.CursorLocation = adUseClient

Now set the Recordset.

0
 

Author Comment

by:RR345
ID: 34943121
Makes mores sense and now getting error Operation is Not allowed when the object is open. Let me expand more on what I am doing. I have a Tab Control on an unbound form. In one of the Tabs, I would like the user to select a date range and the information will show in datasheet view. I put my code on an onclick event of a command button. Any idea?
0
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 1200 total points
ID: 34943668
Try setting the ControlSource of your controls in design view - OR - setting the Recordset AFTER you set the ControlSources. I don't use the Recordset approach much anymore, but it'll work.

Also, if this is going to a local Access table, you can simply create the table as you're doing and then just set the RecordSOURCE of your subform like this:

Me.YourSubform.Form.RecordSOURCE = "SELECT * FROM YourTablename"

About the only reason to use a RecordSET when doing this is if you must get data from an external source. For example, if you're running a local Access app (on your dekstop) and you need to pull relevant information from a SQL Server on your network, you can build a connection to the server, open a recordset, and then SET the form's Recordset property to your new recordset. Otherwise you're much better off using the RecordSOURCE method.
0
 

Author Closing Comment

by:RR345
ID: 34943871
I had to play with it as I am using a temporary table named with a variable (User Login) for the RecordSource. So, final: Me.subform.Form.RecordSource = "SELECT * FROM " & strTableName

Thank you
0

Featured Post

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
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.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

721 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