-
5
- #1
For some time, I've wondered why there weren't more aggregate functions like DLookup. Common needs continuously arise in our apps to concatenate fields or values or to find and replace or strip characters from strings. I figure that Microsoft is using much the same code as the rest of us, so I came up with the following functions that perform like the aggregates.
Pasting them into their own modules will make them available throughout the database.
ConcatField() strings together the values in a single field from a table or saved query.
ConcatRows() strings together the values in different fields from a single record.
StripChar() finds a specific character or string within a string and removes it or replaces it with a specified character or string.
With the ConcatField function, the three parts you would provide are:
"The name of the field",
"comma or other separator",
"the name of your table or saved query".
_____________________________
With ConcatRow, you would provide the names of the fields you want concatenated, the separator, the table or saved query and any criteria. The criteria follows the rules of criteria in a DLookup function. If you omit the criteria, the first record only is returned.
Example: In query design view,
returns each record's property ID#, the name of the property and the street address using semicolons as separators.
______________________________________
The StripChar function simply finds a specified character/s in a string and replaces it with the character/s you specify. If you don't specify a new character, the character (and/or space) is removed.
EG:
returns phone numbers without the 312 area code so it isn't dialed.
changes the 312 area code to the newer 847.
An extra FYI...
tells you the number of spaces in MyString.
_______________________________________
It's been a lot of fun working on these and I hope someone finds them useful.
John
Use what you have,
Learn what you can,
Create what you need.
Pasting them into their own modules will make them available throughout the database.
ConcatField() strings together the values in a single field from a table or saved query.
ConcatRows() strings together the values in different fields from a single record.
StripChar() finds a specific character or string within a string and removes it or replaces it with a specified character or string.
Code:
Public Function ConcatField(MyFld As String, MyBreak As String, TblQryName As String) As String
Dim MyString As String, strSQL As String
Dim db As Database, rst As Recordset
strSQL = "SELECT [" & MyFld & "] FROM " & TblQryName & ";"
Set db = CurrentDb
Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)
rst.MoveFirst
Do Until rst.EOF
MyString = MyString & rst.Fields(0) & MyBreak
rst.MoveNext
Loop
ConcatField = Left(MyString, Len(MyString) - Len(MyBreak))
End Function
With the ConcatField function, the three parts you would provide are:
"The name of the field",
"comma or other separator",
"the name of your table or saved query".
_____________________________
Code:
Public Function ConcatRow(MyFld As String, MyBreak As String, _
TblQryName As String, Optional strCriteria As String) _
As String
Dim MyString As String, strSQL As String, intF As Integer
Dim db As Database, rst As Recordset, FString As String
Dim FirstFld As String
If InStr(MyFld, ",") <> 0 Then
FirstFld = Left(MyFld, InStr(MyFld, ",") - 1)
Else
FirstFld = MyFld
End If
If strCriteria = "" Then
strSQL = " SELECT " & MyFld & " FROM " & TblQryName _
& " WHERE " & TblQryName & "." & FirstFld & " = " & FirstFld & ";"
Else
strSQL = " SELECT " & MyFld & " FROM " & TblQryName & " WHERE " & strCriteria & ";"
End If
Set db = CurrentDb
Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)
For intF = 0 To rst.Fields.Count - 1
MyString = MyString & rst.Fields(intF) & MyBreak
Next intF
ConcatRow = Left(MyString, Len(MyString) - Len(MyBreak))
End Function
With ConcatRow, you would provide the names of the fields you want concatenated, the separator, the table or saved query and any criteria. The criteria follows the rules of criteria in a DLookup function. If you omit the criteria, the first record only is returned.
Example: In query design view,
Code:
Expr1: concatrow("[prpid],[prpname],[prpstreet]","; ","tblproperties","[prpid] = " & [prpid])
______________________________________
Code:
Public Function StripChar(MyChar As String, _
MyString As Variant, Optional NewChar As String) As String
Dim NewVal As String
NewVal = Nz(MyString, "There is no text to evaluate.")
Do
If InStr(NewVal, MyChar) = 0 Then
Exit Do
Else
NewVal = Left(NewVal, InStr(NewVal, MyChar) - Len(MyChar)) _
& NewChar & Mid(NewVal, InStr(NewVal, MyChar) + Len(MyChar))
End If
Loop
StripChar = NewVal
End Function
The StripChar function simply finds a specified character/s in a string and replaces it with the character/s you specify. If you don't specify a new character, the character (and/or space) is removed.
EG:
Code:
StripChar("(312)", [custPhone])
Code:
StripChar("(312)", [custPhone], "(847)")
An extra FYI...
Code:
Len(MyString)-len(StripChar(" ",MyString))
_______________________________________
It's been a lot of fun working on these and I hope someone finds them useful.
John
Use what you have,
Learn what you can,
Create what you need.