Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HasData 9 36
Reset Active Directory Password via MS Access 9 52
How can I create a table in a linked database, in VB? 7 17
Two list boxes - best structure 3 31
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…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

910 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

22 Experts available now in Live!

Get 1:1 Help Now