473,396 Members | 2,013 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,396 software developers and data experts.

Counting a particular name in a text field with several names.

I have a text field that lists names seperated by the underscore (_). There may be up to five names in the filed. (Pat_Bob_Jay) How can I get a count a particular name (Pat)that is in that text field for the master Query.
Feb 20 '15 #1
7 991
Seth Schrock
2,965 Expert 2GB
It is not very easy when you have it designed with multiple names in a single field. You should look at Database Normalization and Table Structures. If (and only if) you can't do anything about having multiple names, then there is an option, but not one that can be 100% accurate 100% of the time. For example, if you search a string for "Pat" it will find "Patty", "Patricia", "Patrick", etc. If you start the list of names with an underscore and end the list with an underscore (ie "_Pat_Bob_Jay_"), then you can find it because you can search for "_Pat_". Anyway, here is the solution that I would recommend if you can't do anything about the list:
Expand|Select|Wrap|Line Numbers
  1. Public Function NameCount(strSearch As String) As Integer
  2. Dim intCount As Integer
  3. Dim intStart As Integer
  4.  
  5. intCount = 0
  6. intStart = 0
  7.  
  8. Do While InStr(intStart, Me.TextBox_Name, strSearch) <> 0
  9.     intCount = intCount + 1
  10.     intStart = InStr(intStart, Me.TextBox_Name, strSearch) + 1
  11. Loop
  12.  
  13. NameCount = intCount
  14. End Function
When you call this function, then you would just pass the name that you are searching for as the functions argument.
Feb 20 '15 #2
Rabbit
12,516 Expert Mod 8TB
Prepend and append an underscore to the value and then you can use the LIKE operator.
Feb 20 '15 #3
Seth Schrock
2,965 Expert 2GB
@Rabbit Can you count the occurrences of the name using the LIKE operator?
Feb 20 '15 #4
Rabbit
12,516 Expert Mod 8TB
I am making the assumption that the name only appears once per row.
Feb 20 '15 #5
Seth Schrock
2,965 Expert 2GB
Maybe I'm misreading the question, but "How can I get a count a particular name (Pat)that is in that text field for the master Query." sounds like there might be the same name twice.

@Global1003 can you specify what exactly you are wanting?
Feb 21 '15 #6
jforbes
1,107 Expert 1GB
If you are looking to get a count within a String, you can use the following neat trick:
Expand|Select|Wrap|Line Numbers
  1. Public Function occurranceCount(ByRef sFindIn As String, ByRef sFind As String) As Long
  2.     Dim iFindLen As Integer
  3.     iFindLen = Len(sFind)
  4.     If iFindLen > 0 Then
  5.         occurranceCount = (Len(sFindIn) - Len(Replace(sFindIn, sFind, ""))) / iFindLen 
  6.     End If
  7. End Function
  8.  
It replaces the value you are looking for in the String, then compares the length of the result to the original string and sees what the difference happens to be.

EDIT
Found a RegEx solution for count within a line:
Expand|Select|Wrap|Line Numbers
  1. Public Function countRXMatches(ByVal SourceString As String, ByVal Pattern As String)
  2.     Dim oMatch As Object
  3.     Dim RegExp As Object
  4.  
  5.     Set RegExp = CreateObject("vbscript.regexp")     
  6.     With RegExp
  7.         .MultiLine = True
  8.         .IgnoreCase = True
  9.         .Global = True
  10.         .Pattern = Pattern
  11.         countRXMatches = .Execute(SourceString).Count
  12.     End With
  13. End Function
Feb 21 '15 #7
NeoPa
32,556 Expert Mod 16PB
I'm pretty sure the question is to count the number of rows that include the string. Rabbit's suggestion, though a little short on padding, is perfect for this.

Unfortunately, for it to work reliably, you'd need SQL similar to :
Expand|Select|Wrap|Line Numbers
  1. ...
  2. WHERE ('_' & [SearchedField] & '_' Like '*_' & 'Pat' & '_*')
In this example the name 'Pat' has been used as a literal string but other references could also be use, such as a reference to a control on a form or even a field in a record.

NEVERTHELESS:
Don't be tempted to ignore Seth's point about Normalisation just because it's possible to work without it. It sounds like the design you're working with is crying out to be done properly.
Feb 24 '15 #8

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

Similar topics

2
by: Mark | last post by:
I am trying to populate a field in a SQL table based on the values returned from using substring on a text field. Example: Field Name = RecNum Field Value = 024071023 The 7th and 8th...
3
by: Roy Adams | last post by:
Hi I'm reposting this question because for some reason can't post follow up question to this thread. What I'm trying to do is put the value and text from a a select in to a text field and to a...
4
by: Chuck | last post by:
Hello, I have an Access XP database that has several fields. One of the fields is a text field and has an account number that is preceeded by a text character, like: F102354. Every account...
4
by: N. Graves | last post by:
Hello; I have a table that is equipment. This table has a filed called Owner and that Owner filed is Looked up from a table called employee. and the query request the serial number of the...
6
by: dixie | last post by:
I have a text field on a form which has names with a comma between them like this: 'J. Smith, A. Jones, A. Man, J. Johns'. I am trying to find a procedure that will count the number of people in...
2
by: jtvc | last post by:
I'm trying to insert a record on an sql server database table that has among others a text type field. The insert happens without errors and all the fields show the correct information except for...
4
by: justin tyme | last post by:
Hello Experts! I would like to combine (which may not be the correct technical term) two text fields from the same table in a query. Specifically, text field A and text field B are both lists of...
3
by: prabodhtiwari | last post by:
function submitPartsForm(str) { var count=document.getElementsByName("partId"); for(var i=0;i<count.length;i++) { document.mylist.myNum.value= document.getElementsByName("partNum")...
3
by: pollygw | last post by:
I have a page that dynamically adds rows to a table and the user can also delete any of the rows in no specific order. When the form is submitted I need to do some validation. I can't loop through...
2
by: radforms | last post by:
Hi All, just wondering someone out there could help me solve a little problem in my mysql database: Table 1 Name: wp_wpsc_product_files Fields: ID, Name, Description, Additional_Description ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.