Results 1 to 3 of 3

Thread: [RESOLVED] query help

  1. #1

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604

    Resolved [RESOLVED] query help

    Hey all.
    Access database, working in a VBA macro.

    I'm trying to simplify a task by keeping as much as possible in database query and commands rather than doing this all in VBA.

    Background:
    I've built a term search and replace macro driven by an ever-growing data table of terms to search and terms to replace them with in a Word doc.

    I usually just query the whole thing:
    select * from tblTerms order by ID asc
    and perform recordset count iterations of search and replace.
    The long-term problem is that some search terms precede other terms that include the former term in the latter.
    Ex:
    tblTerms
    ID Search Replace
    1 dog canine
    2 doghouse canine residence

    just made up examples of course, but you can see that if dog is replaced by canine, then doghouse will never be found. The list is too long for me to reason out the terms and change them manually, so I need to automate finding any term such that a term with a higher ID number fully incorporates it, then ideally I can just switch their IDs.
    Don't worry about full word match and those problems (the actual data is in Chinese!).

    Something like:
    select * from tblTerms where (any other row like '% given row's term %' and ID > given row's id)
    then ultimately
    update tblTerms set Search = smaller search term, Replace = smaller replace term
    where ID = larger term's ID

    and of course

    update tblTerms set Search = larger search term, Replace = larger replace term
    where ID = smaller term's ID
    *******

    The easiest way I can think of to code it is just to pull the whole data table into an array in VBA, then go through the entire array for each term and switch as necessary, and keep recursing until no changes are made, but that would not be a very efficient code.

    Can any of this be moved into the query language?
    (As for stored procedures, I'm pretty good with them in SQL Server, but haven't used Access's internal tools very much at all).
    Thanks.
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

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

    Re: query help

    Is there a particular reason that you want to actually change the ID numbers around?

    The simplest solution would be to change the way you order the data when you use it, to do it based on the length of the item rather than the ID number:
    Code:
    select * from tblTerms order by Len(Search) desc
    This will take slightly longer to run than ordering by ID, but it will always return items in a way that ensures the longest match first (which isn't guaranteed with the "ID swap" method, as it needs to be re-run whenever something is added).

  3. #3

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604

    Re: query help

    I don't know si.
    I had that thought early on and somehow convinced myself it wouldn't solve the problem.
    Can't remember what I was thinking now.
    I guess you're right.
    I already wrote the clunky vba code to do the job, but I guess I can delete that now.
    Thanks.
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

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