Access query - calculated value or VBA? (1 Viewer)

bigal.nz

Registered User.
Local time
Tomorrow, 09:13
Joined
Jul 10, 2016
Messages
92
Hi All,

I have a query which gets a list of records from a table. Each record will, among other things, contain the details of a person and in most cases this will include a Social Security type number (SSN) for the format [0-9]{6-12} (thats all numeric length 6-12).

I need to have a calculated field which tells me, based on SSN how many previous records are in the database for that SSN. The issue is that sometime people are putting in NIL, Unknown, UNK etc.

So before preforming the calculation I need to check that whats in the SSN field is not null, and matches pattern. I don't think this can be all done in SQL?

Ultimately the query is sent to a report.

How can I achieve this in Access?

Thanks

Al
 

plog

Banishment Pending
Local time
Today, 16:13
Joined
May 11, 2011
Messages
11,638
How can I achieve this in Access?

What is 'this'? You've asked about 2 very different things--validating data and creating a query. which would you like hlep with and what have you tried?

If it's data validation, usually that is done on a form upon input. You catch and validate data prior to it being submitted. Once in there you assume its correct.
 

bigal.nz

Registered User.
Local time
Tomorrow, 09:13
Joined
Jul 10, 2016
Messages
92
What is 'this'? You've asked about 2 very different things--validating data and creating a query. which would you like hlep with and what have you tried?

If it's data validation, usually that is done on a form upon input. You catch and validate data prior to it being submitted. Once in there you assume its correct.

Hi,

Good point - let me clarify - I am not attempting to tackle validation yet - there is already a lot of data in the database which I need to deal with.

So the question is how to create this calculated field for each record in a result set. The logic iteself looks like:

1. Find all records matching some criteria which gives a result set (this bit works)
2. For each record in this result set look at the Table.SocialSecurityNumber
3. If SocialSecurityNumber matches regex then
4. Query database for count of existing records where SocialSecutiyNumber = Current SocialSecurityNumber in result set.

I hope this is clear?

I havent tried it yet, as at a high level I am not sure how to approach this problem? I dont think it can be done with a query alone?

Cheers

Al
 

plog

Banishment Pending
Local time
Today, 16:13
Joined
May 11, 2011
Messages
11,638
Your numbered objectives really don't make sense individually, to wit:

3. If SocialSecurityNumber matches regex then

There's nothing to do there. It's an incomplete sentence--all subject and no predicate.

Taken together it seems like you simply want a totals query to count all the SSN values. So that SQL would be this:

Code:
SELECT SocialSecurityNumber, COUNT(SocialSecurityNumber) AS Records
FROM YourInitialQuery
GROUP BY SocialSecurityNumber

That will tell you how many times a SocialSecurityNumber value appears in your initial query. If that's not what you hope to achieve, please demonstrate your issue with sample data.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 22:13
Joined
Feb 19, 2013
Messages
16,610
you should be able to do this in a query


SELECT *
FROM myTable
WHERE ftnregex(SSN)=true

this assumes ftnregex is a public function which inspects the SSN value passed to it and returns true if it matches the pattern

see this link for a potentially suitable function

https://stackoverflow.com/questions/8146485/returning-a-regex-match-in-vba-excel

its for excel, but should work in Access - you'll need to change the pattern to what you require and change the return value to a Boolean result
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 05:13
Joined
May 7, 2009
Messages
19,229
you can create a function to test
if ssno is valid or not.
copy and paste in standard module in vbe
the following code.

in your query you call it:

Code:
Public Function IsSSNoValid(ssNo As Variant) As Boolean
    Const Pattern As String = "[0-9]"
    Dim strPattern As String
    Dim byt As Byte
    ssNo = Trim(ssNo & "")
    ' if ssNo is blank or length is less
    ' than 6 return false
    If ssNo = "" Or Len(ssNo) < 6 Or Len(ssNo) > 12 Then _
        Exit Function
    ' build pattern for checking
    For byt = 1 To Len(ssNo)
        strPattern = strPattern & Pattern
    Next
    ' now check ssno agains our pattern
    ' [0-9][0-9]... (up to 6 or 12)
    IsSSNoValid = ssNo Like strPattern
End Function
 

Users who are viewing this thread

Top Bottom