Solved Extract text from String ,Name and telephone number (1 Viewer)

ypma

Registered User.
Local time
Today, 16:37
Joined
Apr 13, 2012
Messages
643
More guidance is requested . I have a field in Select query solicitors, which produces the name of the solicitor and the telephone number .My requirement is to be able extract only the solicitors name . I have tried various combination using left function and is numeric to remove the numbers. The output format is always the same Watt Jones : 23473058. If more details require please advise .

Any assistance would be appreciated .

Regards Ypma
 

plog

Banishment Pending
Local time
Today, 10:37
Joined
May 11, 2011
Messages
11,643
I have a field in Select query solicitors, which produces the name of the solicitor and the telephone number

Why does the underlying table store your data like this? In a properly structured database every discrete piece of data goes into its own field, not jammed together.

Left() is only good if you know where the name ends. To get that you need another function: InSTr( https://www.techonthenet.com/access/functions/string/instr.php). Nest them and you have your desired data. Of course as said before, it shouldn't even be stored like this.
 

Gasman

Enthusiastic Amateur
Local time
Today, 16:37
Joined
Sep 21, 2011
Messages
14,263
No doubt the query combines both fields? 🙄
 

ypma

Registered User.
Local time
Today, 16:37
Joined
Apr 13, 2012
Messages
643
No doubt the query combines both fields? 🙄
Thank you Gasman, the field in the subject query solicitor, is produced from a combo box on the form, this combo box has a lookup table for its source .I fully except plog's comments , but this is the problem I am face with and hope you can tell if its possible to create another field in the query to only show the text ?

Regards Ypma
 

Gasman

Enthusiastic Amateur
Local time
Today, 16:37
Joined
Sep 21, 2011
Messages
14,263
You can create that field in the query as plog indicated using left() and Instr() functions.

I was envisaging your query to have SolName & ":" & SolTelNumber putting them into the one field for the query?
 

MarkK

bit cruncher
Local time
Today, 08:37
Joined
Mar 17, 2004
Messages
8,180
Use VBA.Split(), which splits the string at the delimiter you specify, and return the parts in a variant array.
Code:
debug.print split("Watt Jones : 23473058", " : ")(0)
 

ypma

Registered User.
Local time
Today, 16:37
Joined
Apr 13, 2012
Messages
643
Use VBA.Split(), which splits the string at the delimiter you specify, and return the parts in a variant array.
Code:
debug.print split("Watt Jones : 23473058", " : ")(0)
Mark k, Is it something like this Legal: split("solictors", " : ")(0) ? as I am not sure where to insert your solution .

Regards Ypma

Note, I am not a programmer ,but dip my feet in occasionally
 

CJ_London

Super Moderator
Staff member
Local time
Today, 16:37
Joined
Feb 19, 2013
Messages
16,607
you can't use the the split function in a query as you appear to be trying to do. You need to write a function in a general module - something like

Code:
function splitstr(s as string, delim as string, pos as integer) as string
   
    splitstr=split(s,delim)(pos)
    'edit you might want to remove extra spaces in which case use trim(split(s,delim)(pos))
end function

and in your query

legal:splitstr([solicitors],":",0)
 

ypma

Registered User.
Local time
Today, 16:37
Joined
Apr 13, 2012
Messages
643
you can't use the the split function in a query as you appear to be trying to do. You need to write a function in a general module - something like

Code:
function splitstr(s as string, delim as string, pos as integer) as string
 
    splitstr=split(s,delim)(pos)
    'edit you might want to remove extra spaces in which case use trim(split(s,delim)(pos))
end function

and in your query

legal:splitstr([solicitors],":",0)
Thank you CJ_London. I will implement you suggestion tomorrow, as I need my beauty sleep.

Regards Ypma
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 01:37
Joined
Jan 20, 2009
Messages
12,852
An alternative technique would be to use the following expressions in your query. No need for a custom function then.

Code:
SolicitorName: Trim(Left(fieldname, Instr(fieldname, ":") - 1))
Phone: Trim(Mid(fieldname, Instr(fieldname, ":") + 1, 20))

The third argument on Mid, (in this case, 20), needs to be arbitrarily large enough to capture what is after the colon.

By the way if you want to look for the last occurrence of a character to break up the string, then investigate the use of InstrRev().

Generally speaking, the performance of a user defined function will be inferior to using native SQL functions because the engine doesn't have to call out to the VBA project for the SQL function.

Although many of the functions in SQL share the same names as VBA functions, the engine doesn't actually use the VBA function but rather its own resource.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:37
Joined
May 7, 2009
Messages
19,230
Although many of the functions in SQL share the same names as VBA functions, the engine doesn't actually use the VBA function but rather its own resource.
Trim, Mid, Left, Instr is from VBA namespace. so it is calling a function.
the only function in SQL are Min, Max, First, Last?
 

ypma

Registered User.
Local time
Today, 16:37
Joined
Apr 13, 2012
Messages
643
An alternative technique would be to use the following expressions in your query. No need for a custom function then.

Code:
SolicitorName: Trim(Left(fieldname, Instr(fieldname, ":") - 1))
Phone: Trim(Mid(fieldname, Instr(fieldname, ":") + 1, 20))

The third argument on Mid, (in this case, 20), needs to be arbitrarily large enough to capture what is after the colon.

By the way if you want to look for the last occurrence of a character to break up the string, then investigate the use of InstrRev().

Generally speaking, the performance of a user defined function will be inferior to using native SQL functions because the engine doesn't have to call out to the VBA project for the SQL function.

Although many of the functions in SQL share the same names as VBA functions, the engine doesn't actually use the VBA function but rather its own resource.
Thank you Galaxiom: The SolicitorName expression you produced gave me exact result I needed . I have learnt something new , I intend to see if i can get CJ_London functions to work, to add to my knowledge , They say " a little knowledge is dangerous" !!
 

ypma

Registered User.
Local time
Today, 16:37
Joined
Apr 13, 2012
Messages
643
CJ_London . Thanks you for the function , It produced the correct result that I was after and I will retain it for further reference .

Regards Ypma
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 01:37
Joined
Jan 20, 2009
Messages
12,852
Trim, Mid, Left, Instr is from VBA namespace. so it is calling a function.
the only function in SQL are Min, Max, First, Last?
No. The names of the functions are the same in VBA and JET/ACE database engines but they are different objects. A query uses the engine functions by preference and they are far more efficient.

This is most evident when connecting to an Access database via ODBC which connects to the engine, not the project. A well known case is the Nz() function which is in VBA but not the engine and doesn't work properly from external connections where Iif() is typically used instead.

This page lists the functions in Jet/ACE. Notice that Nz() is not among them.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:37
Joined
May 7, 2009
Messages
19,230
"It is important to note that there are 2 type of functions in Access - functions used in SQL/Queries and functions used in VBA code. Some functions can be used as both, but some can not."

?which one?
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 01:37
Joined
Jan 20, 2009
Messages
12,852
"It is important to note that there are 2 type of functions in Access - functions used in SQL/Queries and functions used in VBA code. Some functions can be used as both, but some can not."
Quoting someone who doesn't understand the difference between the engine and the application isn't helpful.

There are two types of functions. Jet/ACE database engine functions and VBA functions. Many of the have the same names.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:37
Joined
May 7, 2009
Messages
19,230
Quoting someone who doesn't understand the difference between the engine and the application isn't helpfu
the quote comes from the page you post.
no further question, you have answered them all.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 01:37
Joined
Jan 20, 2009
Messages
12,852
no further question, you have answered them all.
I can see the page I linked was not ideal. Sorry about that.

Anyway I hope you now understand that the database engine functions are not the VBA functions.
 
Last edited:

CJ_London

Super Moderator
Staff member
Local time
Today, 16:37
Joined
Feb 19, 2013
Messages
16,607
Notice that Nz() is not among them.
it is there in the advanced/logical functions

just discovered that switch and choose also work in vba which might take away a few case statements.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 01:37
Joined
Jan 20, 2009
Messages
12,852
it is there in the advanced/logical functions
That was another thing I took on face value from the post on another site where I found the link. I suspect the list was expanded after that post was written.

I do know for sure that Nz() misbehaves if the database query using it is connected from Excel.
 

Users who are viewing this thread

Top Bottom