Re: Can SQL statement using a query rather than a table?
- From: "Brendan Reynolds" <brenreyn@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 5 Aug 2005 22:48:11 +0100
The square brackets are not required unless there are spaces or other
problematic characters in the name. If you think you need them in your
dynamic SQL statement, you need to keep them within the quotes ...
Set rs1 = dbs.OpenRecordset("SELECT * FROM [" & QueryName & "] ORDER BY
Field1")
--
Brendan Reynolds (MVP)
"Jim in Northwest" <JiminNorthwest@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message news:03F8BED6-45AA-44F3-B36D-66B6A23C6B85@xxxxxxxxxxxxxxxx
> Hi, Brendan:
>
> [] works fine for the query in the SQL Statement. In my application, the
> query name is actually an arugment passing through a general procedure.
>
> For example:
> Public sub MyProcedure (QueryName as String)
> .....
> Set rs1 = dbs.OpenRecordset _
> ("SELECT * FROM " & [QueryName] & " ORDER BY Field1")
> .....
>
> or I tried:
> .......
> Dim aa as string
> aa = QueryName
>
> Set rs1 = dbs.OpenRecordset _
> ("SELECT * FROM " & [aa] & " ORDER BY Field1")
>
> Non of them seems working. What syntex should they be?
> Many thanks.
>
>
>
> --
> Jim in Northwest
>
>
> "Brendan Reynolds" wrote:
>
>>
>> The example below works for me, using the 'Quarterly Orders by Product'
>> cross-tab query from Northwind. As I said, if you still can't get it to
>> work, try posting the SQL for the 'MyQuery' query. Does that query work
>> on
>> it's own?
>>
>> Public Sub TestSubX()
>>
>> Dim db As DAO.Database
>> Dim rst As DAO.Recordset
>> Dim strSQL As String
>>
>> strSQL = "SELECT * FROM [Quarterly Orders by Product] ORDER BY
>> CustomerID"
>> Set db = CurrentDb
>> Set rst = db.OpenRecordset(strSQL)
>> Debug.Print rst.Fields("CustomerID")
>> rst.Close
>>
>> End Sub
>>
>> --
>> Brendan Reynolds (MVP)
>>
>> "Jim in Northwest" <JiminNorthwest@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
>> message news:08306A97-A5D9-443D-A349-C66EE676222E@xxxxxxxxxxxxxxxx
>> > Thanks for your suggestions!
>> > I am using the following statement:
>> > Set rs1 = dbs.OpenRecordset _
>> > ("SELECT MyQuery.* FROM MyQuery ORDER BY MyQuery.Field1")
>> >
>> > The error message I got is
>> > Runtime error 3131 - Syntex error in FORM Clause
>> >
>> > If I replace the query with a tablename, everthing works fine. I am
>> > using
>> > Access 2002 and MyQuery may be a cross-tab query.
>> > Any more suggestions?
>> > Thanks!
>> >
>> > --
>> > Jim in Northwest
>> >
>> >
>> > "Brendan Reynolds" wrote:
>> >
>> >> Provided that the query 'MyQueryName' includes the field 'field1' in
>> >> its
>> >> SELECT clause, it should work, yes.
>> >>
>> >> If you still can't get it to work, try posting the SQL for the
>> >> 'MyQueryName'
>> >> query.
>> >>
>> >> --
>> >> Brendan Reynolds (MVP)
>> >>
>> >>
>> >> "Jim in Northwest" <JiminNorthwest@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
>> >> message news:1CE21964-CB28-4BF3-9E95-35BDAE2A5E59@xxxxxxxxxxxxxxxx
>> >> > We can do the following to open a query as recordset:
>> >> >
>> >> > Set rs = dbs.OpenRecordset("MyQueryName")
>> >> >
>> >> > Can we do the following to sort a field inside the query?
>> >> >
>> >> > Set rs = dbs.OpenRecordset("select * from MyQueryName order by
>> >> > field1")
>> >> >
>> >> > I didn't have mush luck for the last sort SQL statement to open the
>> >> > recordset. Any suggestion?
>> >> > Thanks
>> >> > --
>> >> > Jim in Northwest
>> >>
>> >>
>> >>
>>
>>
>>
.
- Follow-Ups:
- Re: Can SQL statement using a query rather than a table?
- From: Jim in Northwest
- Re: Can SQL statement using a query rather than a table?
- References:
- Can SQL statement using a query rather than a table?
- From: Jim in Northwest
- Re: Can SQL statement using a query rather than a table?
- From: Brendan Reynolds
- Re: Can SQL statement using a query rather than a table?
- From: Jim in Northwest
- Re: Can SQL statement using a query rather than a table?
- From: Brendan Reynolds
- Re: Can SQL statement using a query rather than a table?
- From: Jim in Northwest
- Can SQL statement using a query rather than a table?
- Prev by Date: Re: Access is just to hard to understand
- Next by Date: Re: Update new values to combo box
- Previous by thread: Re: Can SQL statement using a query rather than a table?
- Next by thread: Re: Can SQL statement using a query rather than a table?
- Index(es):
Relevant Pages
|