Solved

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

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

840 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