Solved

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

Posted on 1998-09-10
3
532 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now