Results 1 to 6 of 6
  1. #1
    Juan4412 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2010
    Posts
    209

    WildCards in VB Code

    Are there wildcards in VB? For example, I want to run the following code....


    Code:
    DoCmd.DeleteObject acQuery, [Zip]
    But the program name that I am referencing is on the form, and I want to delete any query that is associated with that zip. The query will definately have the zip in the name, but it may begin with query_, qry_, Q_, zip_Query, etc. Is there a wildcard that I could use to isolate the zip from my form, regardless of what comes before or after it on the query?
    Last edited by Juan4412; 06-25-2011 at 09:23 PM.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Dim tbl As Object
    For Each tbl In CurrentDb.TableDefs
    If tbl.Name Like "*" & [Zip] & "*" Then DoCmd.DeleteObject acTable, tbl.Name
    Next

    Taken from this thread:
    http://www.eggheadcafe.com/microsoft...ect-macro.aspx
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    June's code is close, but I think you need to go with QUERYDEF instead of TABLEDEF.

    And are objects included in DEF collections? Not sure about that, but I would assume they are. It's better obviously to dim a var as QUERYDEF/TABLEDEF rather than just an OBJECT though. Objects should really be used for vars that can't be dimmed into a collection. An example would be the FILE SYSTEM OBJECT.

    and the criteria should probably be this:

    Code:
    If tbl.Name Like "*Zip*" Then 
       DoCmd.DeleteObject acQuery, tbl.Name

  4. #4
    Juan4412 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2010
    Posts
    209
    So simply surrounding the known portion of the name in "*" [known portion] "*" will find/perform the requested action?

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Whoops! I tested the code against table and forgot to test query.

    This works when the string 'Zip' is in the name.
    Dim qry As Object
    For Each qry In CurrentDb.QueryDefs
    If qry.Name Like "*Zip*" Then DoCmd.DeleteObject acTable, qry.Name
    Next

    If you are matching a zip code in the query name, e.g. Q99504, and need to reference textbox entry on form then need "*" & Me.Zip & "*"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    Juan4412 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2010
    Posts
    209
    That code worked to perfection!

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

Similar Threads

  1. VBA Wildcards
    By dssrun in forum Programming
    Replies: 11
    Last Post: 03-31-2011, 08:44 AM
  2. SQL Wildcards
    By sandlucky in forum Queries
    Replies: 4
    Last Post: 03-28-2011, 03:31 AM
  3. Using wildcards with between workaround
    By rushforth in forum Queries
    Replies: 2
    Last Post: 11-10-2010, 02:12 PM
  4. Using wildcards (*) in SQL
    By SIGMA248 in forum Queries
    Replies: 1
    Last Post: 07-22-2010, 08:44 PM
  5. Wildcards?!
    By esx_raptor in forum Access
    Replies: 3
    Last Post: 02-19-2010, 03:22 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