Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set partial string to a var and deleted from main string 1

Status
Not open for further replies.

biry

Technical User
Nov 5, 2004
127
0
0
CA
hi, was wondering how a variable can be set to certain characters of a string while deleting it from the string

I have this string where the info is formatted like this:
some_text_always_different (different_numbers) (more text)

e.g.
BERMAN MICHAEL BRUCE (0001266261) (Reporting)

i would like to display the string without the (0001266261) aspect while setting the (0001266261) to another variable
so the string displays like:
BERMAN MICHAEL BRUCE (Reporting)

and i can still write, the deleted brackets with numbers, to the page.
 
ASSUMING THAT THIS STRING IS IN A VARIABLE:

Code:
strNumber = Mid(variable, InStr(variable, "(") + 1)
strNumber = Left(strNumber, InStrRev(strNumber, "(") - 1)
strNumber = Trim(Replace(strNumber, "(", "")) 'in case we pick up the first "(" &
strNumber = Trim(Replace(strNumber, ")", "")) 'to make sure we get rid of the ")"

strText = Replace(variable, "(" & strNumber & ")", "")
 
thanks pkailas,

is there a way to make it more dynamic for the "variable"
as i have to use this for many variables, starting at variable49 and ending with variable1076 (incrementing ever 13, like so: variable49, variable62, variable75, ... variable1076
 
Code:
Private strText as String 
Private strNumber as String

Public Function RemoveNumber(ByVal varText As String) As String
   strNumber = Mid(variable, InStr(variable, "(") + 1)
   strNumber = Left(strNumber, InStrRev(strNumber, "(") - 1)
   strNumber = Trim(Replace(strNumber, "(", "")) 'in case we pick up the first "(" &
   strNumber = Trim(Replace(strNumber, ")", "")) 'to make sure we get rid of the ")"

   strText = Replace(variable, "(" & strNumber & ")", "")
End Function

You would then use this by:

Code:
Call RemoveNumber(variable)

it will return your number in the strNumber variable and the text in the strText variable.

 
i'm getting error:
Expected end of statement

Private strText as String
----------------^

i think there is a client / server side mix up? hmm...
 
Well, the private declaration should be at the beginning of your coding right under "Option Explicit"

I use Private over Dim if I'm using more than one form. Using Dim will make it a "global or Public" variable accessable by all forms thus violating "encapsulation". Making it a "Private" variable lets it be accessable to all procdures in that one form.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top