Microsoft Access: String and Data Manipulation Programming Examples
Option Compare Database ' Purpose: To retrieve a desired string from a field in a table Sub
Multiple_InStr()
'
Purpose: To trim a field in Table1. In this example, it places the
trimmed string into 2nd field, and also count the number of spaces that
are removed and print the counts into a 3rd field. Dim conn As ADODB.Connection Dim rs As New ADODB.Recordset Dim ssql As String Dim originalLength As Integer Set conn = CurrentProject.Connection ssql = "Select * From Table1" rs.Open ssql, conn, adOpenKeyset, adLockOptimistic Do Until rs.EOF originalLength = Len(rs.Fields("Field_BeforeTrim")) rs.Fields("Field_AfterTrim") = Trim(rs.Fields("Field_BeforeTrim")) rs.Fields("Counter_SpacesRemoved") = originalLength - Len(rs.Fields("Field_AfterTrim")) rs.MoveNext Loop rs.Close Set rs = Nothing Set conn = Nothing End Sub
'
Purpose: To remove the first space that is found in a string. Function Remove_First_Space(full_string As String) As String Dim str_length As Integer Dim pos As Integer str_length = Len(full_string) For pos = 1 To str_length If Mid(full_string, pos, 1) = " " Then ' Change to -2, for example, if like to have one space + one more character removed, moving leftward. remove_first_space=Left(full_string, pos-1)& Right(full_string, Len(full_string)-pos) Exit For End If Next pos End Function
'
Or use the function in Design Query and Make-Table Query, by using an
SQL string in example below.
'
Purpose: To find a text in a string, from reverse right-to-left, and
replace a found text with another text in a field name "Field_Rojak".
(The Replace function includes 1 sub-string search & up to 10
substitutions).
' Purpose: Use InStr
function to locate the last occurrence of the search string, then
replace it with the desired value. Loop ' Now use Replace with knowing where last occurence starts new_string = Left(original_string, last_found_position - 1) & _ Replace(original_string, search_string, replace_string, last_found_position) End If MsgBox new_string End Sub
' Purpose: To return the last text, word, one or a continuation of
numbers, in the strings of the table field. ' Purpose: To count in a table's field on how many upper case and lower case characters, numbers, and other characters.Sub ImpossibleCounting() Dim data As String Dim data_length As Integer, char_loop As Integer, number_count As Integer Dim upper_case_count As Integer, lower_case_count As Integer, other_count As Integer Dim conn As ADODB.Connection Dim rst As New ADODB.Recordset Dim sql As String Dim originalLength As Integer Set conn = CurrentProject.Connection sql = "Select * From Table1" rst.Open sql, conn, adOpenKeyset, adLockOptimistic number_count = 0 upper_case_count = 0 lower_case_count = 0 other_count = 0 Do Until rst.EOF data = rst.Fields("Field1") data_length = Len(data) For char_loop = 1 To data_length ' Asc function returns an integer representing the character code corresponding to the first letter in a string. Select Case Asc(Mid(data, char_loop, 1)) Case 48 To 57 number_count = number_count + 1 Case 65 To 90 upper_case_count = upper_case_count + 1 Case 97 To 122 lower_case_count = lower_case_count + 1 Case Else other_count = other_count + 1 End Select Next char_loop rst.MoveNext Loop Debug.Print number_count & " numbers found" Debug.Print upper_case_count & " upper case letters found" Debug.Print lower_case_count & " lower case letters found" Debug.Print other_count & " other characters found" rst.Close Set rst = Nothing Set conn = Nothing End Sub ' Purpose: basic sample expressions that you can use to extract a portion of a text string.
"William Gates" - Returned by Expression: William
as First Name
Expr: Left([Names],InStr(1,[Names]," ")-1) "Gates, William " - Returned by Expression: William as First Name Expr: Right(Trim([Names]),Len(Trim([Names]))-InStr(1, [Names]," ")) "William H. Gates" - Returned by Expression: William as First Name Expr: Left([Names],InStr(1,[Names]," ")-1) "William Gates" - Returned by Expression: Gates as Last Name Expr: Right(Trim([Names]),Len(Trim([Names]))-InStr(1, [Names]," ")) "Gates, William " - Returned by Expression: Gates as Last Name Expr: Left([Names],InStr(1,[Names],",")-1) "William H. Gates" - Returned by Expression: Gates as Last Name Expr: Right(Trim([Names]),Len(Trim([Names]))-InStr(InStr (1, [Names]," ")+1,[Names]," ")) "William Gates" or "William H. Gates" - Returned by Expression: Gates as Last Name Expr: IIf(InStr(InStr([names]," ")+1,[names]," ") <>0, Right([names],Len([names])-InStr(InStr([names]," ")+1, _ [names]," ")),Right([names],Len([names])-InStr([names]," "))) "William H. Gates" - Returned by Expression: H. as Middle Initial Expr: Trim(Mid([Names],InStr(1,[Names]," ")+1,InStr(InStr (1, [Names], " ")+1,[Names]," ")-InStr(1,[Names]," "))) "Gates, William P." - Returned by Expression: H. as Middle Initial Expr: Right(Trim([Names]),Len(Trim([Names]))-InStr (InStr(1, [Names]," ")+1,[Names]," ")) "William Gates" or "William H. Gates" - Returned by Expression: P. or blank as Middle Initial Expr: Trim(Mid([names], InStr(1, [names], " ") + 1, IIf(InStr(InStr(1, [names], " ") + 1, [names], " ") = 0, 0, _ InStr(InStr(1, [names], " ") + 1, [names], " ") - InStr(1, [names], " ")))) ZipCode, "98052-6399" - Returned by Expression: 98052 as ZipCode Expr: Left([ZipCode],5) Area Code & Phone
(425) 635-7050 - Returned by Expression: 425 as Area Code
Area Code & Phone (425) 635-7050 - Returned by Expression:
635-7050 as Phone
Expr: Right(Trim([Phone]),8)
|
|||||||||||||||||||||||||||
|