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

x
?
Solved

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

Posted on 1998-09-10
3
Medium Priority
?
561 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 200 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
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…
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 …
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…

610 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