Results 1 to 5 of 5
  1. #1
    danny2000 is offline Novice
    Windows XP Access 2002
    Join Date
    Nov 2010
    Posts
    14

    Using a Design View Query in ADO?

    Hi

    Is it possibly to use a query created in Design View in Access in ADO?
    I'm beginning to wonder if it's possible.

    I know there are several alternatives though I'm specifically trying to use a design view query in ADO.

    By using the command object I have placed a reference to the query in the .commandText property though this gives me an error: 'Invalid SQL Statetement; expected Delete, Insert, Procedure, Select or Update.

    Is there a way of execting a Design View query using ADO. I've placed the very simple code below along with the project attached. It's attached to an event handling procedure.

    Code:
    Private Sub Combo2_AfterUpdate()
    
    Dim rst As Recordset
    Dim cmd As ADODB.Command
    
    Dim qrtest As String
    
    qrtest = "MyQuery"
    
    
    Set rst = New ADODB.Recordset
    Set cmd = New ADODB.Command
    
    With cmd
        .ActiveConnection = CurrentProject.Connection
        .CommandText = qrtest
        .CommandType = adCmdText
    End With
    
    Set rst = cmd.Execute
    
    Me.Text0 = rst.Fields(1)
    
    
    End Sub


  2. #2
    pkstormy's Avatar
    pkstormy is offline Access/SQL Server Expert
    Windows XP Access 2003
    Join Date
    Mar 2010
    Location
    Madison
    Posts
    682
    Is this what you're trying to do?

    https://www.accessforums.net/code-re...mple-7571.html

    If you want to show the results of a query within a subform on a mainform, basically:

    1. Create a subform in your main form.
    2. Set the sourceobject of the subform to: Query.MyQueryName (ie. instead of to another form name).

    But maybe I've misinterpreted your question.

  3. #3
    danny2000 is offline Novice
    Windows XP Access 2002
    Join Date
    Nov 2010
    Posts
    14
    Thanks for your response. I'm more interested in learning how to randomly reference a Query designed In Design View in ADO code. I know there are many alternatives and I also realise you can link a control to a query though there are occasions where I just want to utilize a query on some occasions.

    There must be a way.

    Thanks
    Danny2000

  4. #4
    pkstormy's Avatar
    pkstormy is offline Access/SQL Server Expert
    Windows XP Access 2003
    Join Date
    Mar 2010
    Location
    Madison
    Posts
    682
    Here's what I do to put a query into a recordset using ADO...

    dim rs as adodb.recordset
    set rs = new adodb.recordset
    dim strSQL as string
    strSQL = "Select * from MyQueryName"
    rs.open strSQL, currentproject.connection, adopendynamic, adlockoptimistic 'updateable recordset
    or
    rs.open strSQL, currentproject.connection, adopenkeyset, adlockreadonly 'non-updateable recordset but can use rs.recordcount
    if rs.eof and rs.bof then 'No records returned
    msgbox "No Records returned"
    rs.close
    set rs = nothing
    else
    'to save rs!SomeField to a field on the form
    me!MyFieldOnTheForm = rs!SomeField
    'to update rs!SomeField2
    rs!SomeField2 = "SomeValue"
    rs.update
    rs.close
    set rs = nothing
    end if

    I'm guessing your code might work if you changed it to this

    qrytest = "Select * from MyQuery"

    I'm not sure what you mean though by 'randomly reference a query'.

  5. #5
    danny2000 is offline Novice
    Windows XP Access 2002
    Join Date
    Nov 2010
    Posts
    14
    Thanks again for coming back. All I meant for randomly accessing a query was that if I needed to reused on in my code I could add it in. Either way, I shouldn't have put that part in as it's immaterial to the problem at hand.

    Using strSQL = "Select * from MyQueryName" where the Query name is used instead of a table name doesn't work? Or if it does, I'm doing something wrong. I've reproduced the case as per your example and actually have to access the table directly. I thought a source couldn't utilize a query name though I may be wrong on that. Nothing wrong with that but I really want to know if it's possible to use a Design View Query in ADO code as I haven't seen an example yet.

    The project you've provided the link to above doesn't once use a query name in the code using the method above though I don't doubt that it can be done.

    Thanks again for your help on this.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Why does my Form do this? Design View
    By yes sir in forum Access
    Replies: 4
    Last Post: 12-02-2010, 10:37 PM
  2. Disabling Design View
    By DonnyArt in forum Access
    Replies: 1
    Last Post: 06-29-2010, 08:08 AM
  3. Query Design View Takes a Long Time to Open
    By jackthedog in forum Queries
    Replies: 0
    Last Post: 12-22-2009, 03:27 PM
  4. Can you disable design view?
    By nkenney in forum Forms
    Replies: 1
    Last Post: 04-23-2009, 05:08 AM
  5. Defaulting to Design View
    By cgolds in forum Access
    Replies: 0
    Last Post: 06-05-2007, 02:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums