473,386 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Query records where data values meet a certain criteria within string

23
In an Access Database for field2, the data values could consist of the following:

---field1-------------field2-------
Widget 1-----112398764567
Widget 2-----987611236789
Widget 3-----112345678901
Widget 4-----543287652345

I need a query that will check every 4 characters in the string of field 2 and look for all instances of '1223' OR '9876' are found. So my results would be Widget 1, Widget 2, and Widget 3 records.

Normally, one could right a simple search criteria such as:
where field2 like '*1123*' or '9876'.

However in my case, I need it to check every 4 characters for a match to prevent results from pulling in strings like this: 051123874567

I hope this makes sense. Any ideas would be much appreciated! =)
Mar 18 '10 #1

✓ answered by ADezii

I'm fairly sure that I know where the Overflow Error is happening. I'm going to modify the code, then send it back to you. Hopefully, the Error should be eliminated. Done, Download the Attachment and see if the Error is eliminated.

P.S. - Just for my own curiosity, how long does it take to process the 80,000 Records?

12 3111
ADezii
8,834 Expert 8TB
The SQL Gang will probably come up with a better answer, but this will work for now:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblTest.Field1
  2. FROM tblTest
  3. WHERE ((Mid([Field2],1,4)="1123" Or Mid([Field2],1,4)="9876")) OR ((Mid([Field2],5,4)="1123" 
  4. Or Mid([Field2],5,4)="9876")) OR ((Mid([Field2],9,4)="1123" Or Mid([Field2],9,4)="9876"));
Mar 18 '10 #2
KPR1977
23
Hi ADezii, thanks for the quick reply. The reason I'm wanting to avoid writing midstring functions is because the field I'm working with is over 1600 characters long. Maybe there is a VBA function that could accomplish this, but I'm too much of a novice to piece it together.
Mar 18 '10 #3
ADezii
8,834 Expert 8TB
Are you saying that Field2 can be up to 1,600 characters long and you wish to test each 4 character, consecutive block (1...4, 5..8, 1000...1004, etc.) against some value(s)?
Mar 19 '10 #4
KPR1977
23
@ADezii
This is precisely what I'm saying. ;-)
Mar 19 '10 #5
NeoPa
32,556 Expert Mod 16PB
Well, ADezii's your man then. This is a situation where creating a public function which is called from within SQL is perfect for the job.

I can see no alternative to looping through the string checking each section for matches. Each section check could use InStr() though, as long as it's formatted with separators.

EG. "1223,9876". That would ensure no overlapping (Unnecessary in this case, but in case your question has been simplified I suggest it anyway).
Mar 19 '10 #6
ADezii
8,834 Expert 8TB
I'm assuming that it is more complicated than that, since Criteria Strings can be found in multiple locations within the same String in consecutive 4-Block Segments. For instance '1223' can be found at Position 1...5 (Block 1), 1001...1004... (Block 250), 1237...1240 (Block 309), while 9876 can also be found at 1560...1563 (Block 390). There can be many, multiple matches in a single String, which in my mind will not allow the use of a Public Function in an SQL Statement, but I could be complicating things as usual. I'll work on the most complicated scenario, then work backtrack if necessary..
Mar 19 '10 #7
ADezii
8,834 Expert 8TB
  1. The following code will check every consecutive, 4-character block, in [Field2] for a possible match against either '1123' or '9876'. Should a match be found, the results will be written to a Table (tblResults) with the following data:
    1. Record Number where match was found.
    2. Character Position in [Field2] where match was found.
    3. The match string which was found.
    4. The value in [Field1] where the match was found.
  2. Code:
    Expand|Select|Wrap|Line Numbers
    1. Dim intLengthOfString As Integer
    2. Dim intCharCounter As Integer
    3. Dim intBlockNum As Integer
    4. Dim strBuild As String
    5. Dim MyDB As DAO.Database
    6. Dim rst_1 As DAO.Recordset
    7. Dim rst_2 As DAO.Recordset
    8. Dim intRecNum As Integer
    9.  
    10. CurrentDb.Execute "DELETE * FROM tblResults;", dbFailOnError
    11.  
    12. Set MyDB = CurrentDb
    13. Set rst_1 = MyDB.OpenRecordset("tblTest", dbOpenForwardOnly)
    14. Set rst_2 = MyDB.OpenRecordset("tblresults", dbOpenDynaset)
    15.  
    16. With rst_1
    17.   Do While Not .EOF
    18.    intRecNum = intRecNum + 1
    19.     For intCharCounter = 1 To Len(![Field2])
    20.       If intCharCounter = 1 Then
    21.         strBuild = Mid$(![Field2], intCharCounter, 4)
    22.           If strBuild = "1123" Or strBuild = "9876" Then
    23.             rst_2.AddNew
    24.               rst_2![Record#] = Format(intRecNum, "0000")
    25.               rst_2![Position] = Format(1, "0000")
    26.               rst_2![Match] = Mid$(![Field2], intCharCounter, 4)
    27.               rst_2![Field] = ![Field1]
    28.             rst_2.Update
    29.           End If
    30.             strBuild = ""
    31.       ElseIf intCharCounter Mod 4 = 0 And intCharCounter <> Len(![Field2]) Then
    32.         strBuild = Mid$(![Field2], intCharCounter + 1, 4)
    33.           If strBuild = "1123" Or strBuild = "9876" Then
    34.             rst_2.AddNew
    35.               rst_2![Record#] = Format(intRecNum, "0000")
    36.               rst_2![Position] = Format(intCharCounter + 1, "0000")
    37.               rst_2![Match] = Mid$(![Field2], intCharCounter + 1, 4)
    38.               rst_2![Field] = ![Field1]
    39.             rst_2.Update
    40.           End If
    41.             strBuild = ""
    42.       Else
    43.         'do nothing
    44.       End If
    45.     Next
    46.       .MoveNext
    47.   Loop
    48. End With
    49.  
    50. rst_2.Close
    51. rst_1.Close
    52. Set rst_1 = Nothing
    53. Set rst_2 = Nothing
    54.  
    55. DoCmd.OpenTable "tblResults", acViewNormal, acReadOnly
    56. DoCmd.Maximize
  3. tblTest with Test Data:
    Expand|Select|Wrap|Line Numbers
    1. Field1      Field2
    2. Widget 1    1123444488889876120998976
    3. Widget 2    98761123678911237678
    4. Widget 3    1123456789019876987634545
    5. Widget 4    543287652345
    6. Widget 5    112345678907123409875654
    7. Widget 6    77776987655431123876544123
    8. Widget 7    111111111123
    9. Widget 8    6555555550984567
    10. Widget 9    98769276523417865309087654231234567896721123
    11. Widget 10   1123
  4. Contents of tblResults after executing code:
    Expand|Select|Wrap|Line Numbers
    1. Record#    Field       Position    Match
    2. 0001       Widget 1    0001        1123
    3. 0001       Widget 1    0013        9876
    4. 0002       Widget 2    0001        9876
    5. 0002       Widget 2    0005        1123
    6. 0002       Widget 2    0013        1123
    7. 0003       Widget 3    0001        1123
    8. 0003       Widget 3    0013        9876
    9. 0003       Widget 3    0017        9876
    10. 0005       Widget 5    0001        1123
    11. 0007       Widget 7    0009        1123
    12. 0009       Widget 9    0001        9876
    13. 0009       Widget 9    0041        1123
    14. 0010       Widget 10   0001        1123
  5. This is all very preliminary, and probably not the optimal solution. I just want to make sure the concept is dead on! I'm sure that my colleagues will come up with some better, and more intuitive ideas on how to achieve the same results.
  6. DOWNLOAD the Test Database that I used for this Thread to get a much clearer picture of what is going on.
Attached Files
File Type: zip Test.zip (21.5 KB, 80 views)
Mar 19 '10 #8
KPR1977
23
Hey ADezii, first of all I test your code and it worked beautifully with a couple of different scenarios in my database. Major KUDOS to you and a big THANKS!!!

I do run into a snag when I attempt to execute this code on a table that holds over 80,000 records. It works fine when it only contains a few records, but this many records results in an "Overflow" error.

Not exactly sure why, but it might be something to do with the field being set to "Memo" instead of "Text", since Text only can hold 255 characters.

I wish I could attach the database so you could see what I mean, but even after compacting and closing it's over 600 megs. So except for the snag with this table (which very few users would probably ever encounter), the code is great!!!

Thanks again!!! :-)
Mar 19 '10 #9
ADezii
8,834 Expert 8TB
I'm fairly sure that I know where the Overflow Error is happening. I'm going to modify the code, then send it back to you. Hopefully, the Error should be eliminated. Done, Download the Attachment and see if the Error is eliminated.

P.S. - Just for my own curiosity, how long does it take to process the 80,000 Records?
Attached Files
File Type: zip Test_2.zip (18.4 KB, 92 views)
Mar 19 '10 #10
KPR1977
23
Great, I'll give it a shot. I have to go offline for now, but I'm hoping to test it tonight or sometime tomorrow. I will definitely let you know the outcome! ;-)
Mar 20 '10 #11
KPR1977
23
@ADezii
Well the verdict is in...ADezii is the VBA Master!!! Wow, your code totally ran with no hangups! To answer your question, it took a little less than 20 minutes to process that many records, but my connection is a little slow.

Anyway, you nailed it like always! Thanks again so much!!!

I have a separate issue with the same database I'm working on. Here's a link to the thread. VBA to change maximum number of locks allowed on a recordset. If anyone is able to figure it out, I know it would be you. Thanks again for everything!!!
Mar 20 '10 #12
NeoPa
32,556 Expert Mod 16PB
@ADezii
I think so I'm afraid. What I was suggesting was to work in the reverse direction from the usual way. It's unfortunate that the data worked with (from each record) is not in a more convenient format. If it had separator characters we could serch it using InStr(). It doesn't, so instead we create a string, with separators, which we check with each substring of the data in a loop. If a match is found anywhere in the process the function exits immediately returning True. If not then it processes to the end returning False.

This method is more flexible and can be used with very little change if the number of items to check against increases beyond the simple two that we start with here.

All that said, a working piece of code is a working piece of code, so another good solution from ADezii :)

I should add congratulations to KPR for expressing the situation so clearly in the first place. It is no accident that a solution was arrived at so straightforwardly in this case. A properly expressed question is so much easier to deal with.
Mar 20 '10 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Martin Lacoste | last post by:
Ok, before I headbutt the computer... don't know why when I add criteria in a query, I get an 'invalid procedure call'. I also don't know why after searching the help in access, the various access...
1
by: longtim | last post by:
I have been having endless difficulty creating reports/queries that set any relevent parameters from controls in forms. I am creating an application under access 2003 but will target access...
8
by: Maxi | last post by:
There is a lotto system which picks 21 numbers every day out of 80 numbers. I have a table (name:Lotto) with 22 fields (name:Date,P1,P2....P21) Here is the structure and sample data: ...
3
by: martlaco1 | last post by:
Trying to fix a query that (I thought) had worked once upon a time, and I keep getting a Data Type Mismatch error whenever I enter any criteria for an expression using a Mid function. Without the...
36
by: Liam.M | last post by:
hey guys, I have one last problem to fix, and then my database is essentially done...I would therefore very much appreciate any assistance anyone would be able to provide me with. Currently I...
2
by: Mark Roughton | last post by:
I have a form where the users need to view records for various criteria, one of which is a date field on which they may wish to view all related data for the selected date, for all dates upto and...
0
by: jon | last post by:
Hi there, I'm brand new to Access and may be trying to do too much too soon, but I wanted to get some expert advice on how the best way to go about what I am trying to accomplish would be. I...
5
by: DeanL | last post by:
Hi all, I'm trying to set up a query that runs from a command button on a form (simple enough so far), what I want the query to do is take values from the fields on the form (seven fields in...
1
by: Bhujanga | last post by:
I have some reports whose purpose is to show whether any records currently meet certain criteria, so of course the report is based on a query where that criteria is defined. If there don't happen to...
4
by: dizzydangler | last post by:
Hi all, I am a new Access user and just starting to get my head around some of the basic concepts, so please take it easy on me :) My company has been managing client records on excel, and I’m in...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.