Solved

How do I open a querydef object as a dynaset in MSAccess

Posted on 1998-09-10
3
548 Views
Last Modified: 2012-08-13
See below, I'm trying to get the results of the query in to alist box on a form, and cannot set the rowsource to because the object is a querydef and will not let me do this:
Set b = db.QueryDefs("a")
Set rs = b.OpenRecordset("Source", dbOpendynaset)


Dim db As DATABASE
Dim rs, rs1 As Recordset
Dim qdf As QueryDef
Dim b As QueryDef

Set db = CurrentDb
db.QueryDefs.Delete ("a")
Set qdf = db.CreateQueryDef("a")
st = CDate(Forms![FrDistyNotSell]![tbStartDt])
et = CDate(Forms![FrDistyNotSell]![tbEndDt])

SQLBET = "PARAMETERS [st] DATETIME, [et] DATETIME;"
SQLBET = SQLBET & "Select [TestAll].[ResellerID], [TestAll].[ResellerName], [TestAll].[Address1], [TestAll].[Address2], [TestAll].[City], [TestAll].[Zip], [TestAll].[Country] " & _
        "From [TestAll]" & _
        "Where [TestAll].DistyID = """ & [Forms]![FrDistyNotSell]![cbDisty] & """ and [TestAll].Division = """ & [Forms]![FrDistyNotSell]![cbDivision] & """ and [TestAll].[TransDate] Between [st] And [et];"
qdf.SQL = SQLBET
qdf.PARAMETERS("st") = st
qdf.PARAMETERS("et") = et

Set b = db.QueryDefs("a")
Set rs = b.OpenRecordset()
rs.MoveFirst
Do Until rs.EOF
    t = rs!ResellerID
    rs.MoveNext
Loop
lbResellerNotSell.ColumnCount = 7
lbResellerNotSell.ColumnHeads = True
lbResellerNotSell.RowSource = Source
lbResellerNotSell.Requery
0
Comment
Question by:bhanly
[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
3 Comments
 

Expert Comment

by:coggie
ID: 1961754
Hi there,

Rather than set the "RowSource = Source"  why don't you try to build a string of the recordset and set the 'Row Source Type = Value List' and set the 'Row Source =' the string you have just build.  The string is seperated using ; and you will have to set the Column Count to the correct number of columns.

Let me know if this helps!

Regards,

Coggie
0
 
LVL 5

Accepted Solution

by:
kulikuli earned 50 total points
ID: 1961755
I think coggie's answer is ok for limited situations.
What you want to do is have a parameterized query in the rowsource while not using parameter default values. Access does not allow you to have a recordset as a rowsource.
You should create an SQL-string that you put in the RowSource property. The SQL-string should contain no parameters (delete the PARAMETER clause). Instead use fixed values or refer to controls on a form.

To refer to (invisible) controls on your form where your data is put for the date domain:
Replace:
    and [TestAll].[TransDate] Between [st] And [et];"
with:
   and [TestAll].[TransDate] Between [Forms]![FrDistyNotSell]![MyInvisbleControl1] And [Forms]![FrDistyNotSell]![MyInvisbleControl2];"

0
 

Expert Comment

by:coggie
ID: 1961756
Hi again,

Well kulikuli is right as my solution only alows 255 chars in the string but you could use this:

Not a good example but this gives you more control, and is similar to how acces fills the combo box:-

The following example uses a static array to store the names of the databases in the current directory. To call this function, enter ListMDBs as the RowSourceType property setting and leave the RowSource property setting blank.

Function ListMDBs (fld As Control, id As Variant, row As Variant, col As Variant, code As Variant) As Variant
      Static dbs(127), Entries
      Dim ReturnVal
      ReturnVal = Null
      Select Case code
            Case LB_INITIALIZE            ' Initialize.
                  Entries = 0
                  dbs(Entries ) = Dir("*.MDB")
                  Do Until dbs(Entries) = "" Or Entries >= 127
                        Entries = Entries+1
                        dbs(Entries) = Dir
                  Loop
                  ReturnVal = Entries
            Case LB_OPEN                        ' Open.
                  ReturnVal = Timer            ' Generate unique ID for control.

Case LB_GETROWCOUNT            ' Get number of rows.
                  ReturnVal = Entries
            Case LB_GETCOLUMNCOUNT            ' Get number of columns.
                  ReturnVal = 1
            Case LB_GETCOLUMNWIDTH            ' Column width.
                  ReturnVal = -1                  ' -1 forces use of default width.
            Case LB_GETVALUE                  ' Get data.
                  ReturnVal = dbs(row)
            Case LB_END                              ' End.
                  For Entries = 0 to 127
                        dbs(Entries) = ""
                  Next
      End Select
      ListMDBs = ReturnVal
End Function

PS sorry if the formatting is off!

Regards,

Coggie
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

728 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