Results 1 to 7 of 7

Thread: How to refer to QueryTable by name

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    382

    How to refer to QueryTable by name

    I have a question about qy.WorkbookConnection and qy.Name where qy is defined as QueryTable. The qy.WorkbookConnection is 'con2' and the qy.Name is 'Query from MS Access Database_5'.

    How can I change the SQL for this specific qy.WorkbookConnection or qy.Name? I can loop through all of the worksheets and all of the querytables searching for the specific connection or name, but I'd like to refer to the WorkbookConnection (or Name) directly by its name. Hope this isn't too confusing.

    Thanks

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to refer to QueryTable by name

    Is this VB6 or some form of VBA?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    382

    Re: How to refer to QueryTable by name

    Vba.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to refer to QueryTable by name

    Thread moved from the 'VB6' forum to the 'Office Development/VBA' forum (while VBA and VB6 have some similarities, they are not the same thing)

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to refer to QueryTable by name

    this should work to set a specific query table, by name, to an object
    vb Code:
    1. For Each q In Sheets(1).QueryTables
    2.     If q.Name = myName Then Exit For
    3. Next
    if the for each does not find a table by that name then q will be a nothing object on completion, else it will contain the querytable object
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    382

    Re: How to refer to QueryTable by name

    Thanks for reply. But I was asking if there was a way to directly refer to the QueryTable by name rather than looping through all the QueryTable's and checking their names.

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to refer to QueryTable by name

    no, index only
    you could store the names in a collection
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width