I have an Access 2007 Database with the following fields:
Counter (entries are 1 or 2) 1 is for a Single Person while 2 is for a Couple
Fullname: For a single this looks like: Henderson, Sarah
Fullname: For a Couple this looks like: Smithman, Harry & Diana
I want to be able to parse the fullname field to get
Lastname: Henderson
Firstname: Sarah
and
Lastname: Smithman
Firstname: Harry Note: No &
Spouse: Diana Note: No &
The ampersand (&) seems to be a problem for me in parsing the fullname field
Any help would be greatly appreciated
You can use multiple Split for this:
Lastname = Split(Fullname, ", ")(0)
Firstname = Split(Split(Fullname, ", ")(1)," & ")(0)
Spouse = Split(Split(Fullname, ", ")(1) & " & "," & ")(1)
Related
I am trying to concatenate a list of attributes for same products which are in different rows.
For example:
Column A (fruit_name) has fruit names and Column B (fruit_colors) has colors.
I want fruit color on the same row as the fruit.
|**fruit_name** | **fruit_colors** |
|----------------|------------------|
|Apple |Red |
|Apple |Yellow |
|Apple |Green |
|Apple |White |
|Banana |Red |
|Banana |Yellow |
|Banana |Green |
|Banana |White |
|Plum |White |
|Plum |Bluish |
|Plum |Purple |
The result should be:
|**name** | **colors** |
|----------------|---------------------------|
|Apple | Red, Yellow, Green, White |
|Banana | Red, Yellow, Green, White |
|Plum | White, Bluish, Purple |
This is what I have:
Set fruit_name = rstsource.Fields("fruits")
Set source_fruit = rstsource.Fields("fruits_list_type")
rstsource.MoveFirst
count = rstsource.RecordCount
counter = 0
fruit_name = source_fruit
result = source_table
Do
Do
counter = counter + 1
result = result & ", " & source_table
rstsource.MoveNext
Loop Until counter = count Or fruit_name <> source_fruit
rstdest.AddNew
rstdest.Fields("names") = fruit_name
rstdest.Fields("colors") = result
rstdest.Update
fruit_name = source_fruit
result = " "
Loop Until rstsource.EOF
This is the result - Some has comma on the front.
Banana - White, White
Apple - ,Yelow, Red
Banana- ,Red
Banana - White, White
Apple , Green
Plum - ,Green
Plum - ,Red
Banana - ,Red
At the end there is a
Run time error 3021.
I would have a read and download of Allen Browne's Concat function http://allenbrowne.com/func-concat.html - It will do exactly what you want.
This will be for report or display purposes only - you shouldn't store the data like this.
How to create this query to combine values?
I have a table with the following structure and values:
EventID PersonName
----------- ------------
1 John
1 Peter
1 Sylvia
2 John
2 Sylvia
3 Peter
3 June
I'd like to run a query and get results in the following format:
EventID PersonNames
-------- ---------------
1 John, Peter, Sylvia
2 John, Sylvia
3 Peter, June
Is there a query that will accomplish this?
Concatenate fields in same table
Author(s) Dev Ashish
(Q) I need to concatenate a field in the format "Value1; Value2; Value3" etc. for each unique value of another field in the same table. How can I do this?
(A) Using the fConcatFld function, in the Northwind database, the following query should return a concatenated list of all CustomerIDs if you group by ContactTitle.
SELECT ContactTitle, fConcatFld("Customers","ContactTitle","CustomerID","string",[ContactTitle]) AS CustomersFROM CustomersGROUP BY ContactTitle;
'************ Code Start **********
'This code was originally written by Dev Ashish
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Dev Ashish
'
Function fConcatFld(stTable As String, _
stForFld As String, _
stFldToConcat As String, _
stForFldType As String, _
vForFldVal As Variant) _
As String
'Returns mutiple field values for each unique value
'of another field in a single table
'in a semi-colon separated format.
'
'Usage Examples:
' ?fConcatFld(("Customers","ContactTitle","CustomerID", _
' "string","Owner")
'Where Customers = The parent Table
' ContactTitle = The field whose values to use for lookups
' CustomerID = Field name to concatenate
' string = DataType of ContactTitle field
' Owner = Value on which to return concatenated CustomerID
'
Dim lodb As Database, lors As Recordset
Dim lovConcat As Variant, loCriteria As String
Dim loSQL As String
Const cQ = """"
On Error GoTo Err_fConcatFld
lovConcat = Null
Set lodb = CurrentDb
loSQL = "SELECT [" & stFldToConcat & "] FROM ["
loSQL = loSQL & stTable & "] WHERE "
Select Case stForFldType
Case "String":
loSQL = loSQL & "[" & stForFld & "] =" & cQ & vForFldVal & cQ
Case "Long", "Integer", "Double": 'AutoNumber is Type Long
loSQL = loSQL & "[" & stForFld & "] = " & vForFldVal
Case Else
GoTo Err_fConcatFld
End Select
Set lors = lodb.OpenRecordset(loSQL, dbOpenSnapshot)
'Are we sure that duplicates exist in stFldToConcat
With lors
If .RecordCount <> 0 Then
'start concatenating records
Do While Not .EOF
lovConcat = lovConcat & lors(stFldToConcat) & "; "
.MoveNext
Loop
Else
GoTo Exit_fConcatFld
End If
End With
'That's it... you should have a concatenated string now
'Just Trim the trailing ;
fConcatFld = Left(lovConcat, Len(lovConcat) - 2)
Exit_fConcatFld:
Set lors = Nothing: Set lodb = Nothing
Exit Function
Err_fConcatFld:
MsgBox "Error#: " & Err.Number & vbCrLf & Err.Description
Resume Exit_fConcatFld
End Function
'************ Code End **********
Copy and paste the fConcatFld( ) function into a code module. Change the
following VBA line of code:
lovConcat = lovConcat & lors(stFldToConcat) & "; "
to:
lovConcat = lovConcat & lors(stFldToConcat) & ", "
... then save it and compile the code.
Next, create a new query and open the query in SQL View and paste the
following SQL statement into the SQL View pane:
SELECT EventID, fConcatFld("MyTable","EventID","PersonName","Long", EventID)
AS PersonNames
FROM MyTable
GROUP BY EventID;
... and replace "MyTable" with the name of your table. If the "EventID"
data type isn't Long, then you'll need to replace this in the SQL statement,
too, with whatever data type your field is using.
Save and run the query. Voila! Comma separated list.
Have been trying to insert a Street, Suburb and a postcode into a table but keep getting
Run-time error '13'
Type mismatch
Here is the code:
Sub arrayData()
Dim CustomerNames() As Variant
Dim num As Long, dbs As Database, InsertReocrd As String
Dim CustomerID As Long, num1 As Long
Dim CustomerName As String
Dim Street As String, Suburb As String, Postcode As Integer
Set dbs = CurrentDb()
CustomerID = 0
For num1 = 0 To 50000
CustomerID = CustomerID + 1
CustomerNames = Array(...)
Street = Array("Short", "Lygon", "Flinders", "Elizabeth", "King") //ERROR OCURRS HERE
Suburb = Array("Sandringham", "Brighton", "St Kilda", "Melbourne", "Carlton") //ERROR OCURRS HERE
Postcode = Array("3165", "3298", "3145", "3144", "3000") //ERROR OCURRS HERE
num = Int((250 - 0 + 1) * Rnd + 0)
CustomerName = CustomerNames(num)
InsertRecord = "INSERT INTO CUSTOMER (CustomerID , CustomerName, StreetName, Suburb) VALUES (" & "'" & CustomerID & "'" & "," _
& "'" & CustomerName & "'" & "'" & StreetName & "'" & ")"
dbs.Execute InsertRecord
Debug.Print CustomerID, CustomerName, Street, Suburb
Next
End Sub
Variables that should hold arrays need to be declared as Variant, not as String.
So:
Dim Street As Variant, Suburb As Variant, Postcode As Variant
Note that your INSERT statement is missing the value for Suburb.
Using DAO.Recordset.AddNew etc. would probably be faster and better readable.
As shown yesterday, simplify your concatenation, and you also need and array for the CustomerID and a loop as well:
For n = 1 To 5
InsertRecord = "INSERT INTO CUSTOMER (CustomerID , CustomerName, StreetName, Suburb) VALUES (" & CustomerID(n) & ",'" & CustomerName(n) & "','" & StreetName(n) & "','" & Suburb(n) & "')"
If you really wish to insert 50000 customers this way(?) (you wouldn't), follow the advice from André and use DAO.
Sorry if this doesn't exactly fit the topic. Just in case any other poor fool spends hours trying to figure out why they're getting a 'Type Mismatch' on a VBA recordset.addnew, check the [Default Values] of the fields/columns in the table. I changed a TEXT field to a DATE field, but neglected to remove the "" in [Default Value]. (trying to set any text value to a date field is a no-no).
This is my table:
SubjectCode | Unit | Instructor
IS 26 3 James
IS 26 3 James
The result should be:
SubjectCode | Unit | Instructor
IS 26 3 James
My code:
msql = "SELECT Sum(Unit) AS Unit" & _
" From classprogram " & _
" WHERE Instructor= '" & cboInstructor.Text & "'and Semester='" & cboSemester.Text & "'" & _
" GROUP BY CONCAT_WS(' ', Instructor, SubjectCode);"
I want the answer of that is 3.
How can I proceed further?
This is my code in ms access in giving the full name with null values
lname for last name
fname for first name
mname for middle name (change into initial)
x for name extensions like Jr., Sr., III ect.
this is the code.
[lname] & ", " & [fname] & " " & IIf(IsNull([x]),"",([x]) & ", ") & " " & IIf(IsNull(Left([mname],1)),"",(Left([mname],1)) & ".")
but when i try it to mysql it does not give any data. anyone is free to edit or reconstruct my code to fit the qualifications in mysql query. SALAMAT, MABUHAY
The table name is water and it has two field fname and serial.
In the field fname suppose a value 'bill' has 3 serial 1, 2, and 3. now I need when I type bill in textbox1 and click search button, the combobox1 should show the serials of 'bill' are 1,2 and 3.
I'm using ms access as db and I'm using this code but it not working.
Is there any other way to solve this?
Set rs4 = cn.Execute("select fname, serial from water where fname = '" & Text1.Text & "'")
rs4.MoveFirst
Do While Not rs4.EOF
Combo1.AddItem rs4("serial")
rs4.MoveNext
Loop
You could have a problem when someone enters a single-quote in to the text box. You should do something like this:
Set rs4 = cn.Execute("select fname, serial from water where fname = '" & Replace(Text1.Text, "'", "''") & "'")
Another problem you may have is if there is no match found. You're not checking to see if there is any data in the recordset object.
Combo1.Clear
Set rs4 = cn.Execute("select fname, serial from water where fname = '" & Replace(Text1.Text, "'", "''") & "'")
If not rs4.Eof Then
rs4.MoveFirst
Do While Not rs4.EOF
Combo1.AddItem rs4("serial")
rs4.MoveNext
Loop
End If
I'm not really sure what the problem is, but your loop should look something more like this:
rs4.MoveFirst
Do While Not rs4.EOF
Combo1.AddItem rs4("holding")
rs4.MoveNext
Loop
Have you considered telling the compiler more explicitly what you want, something more like:
Set rs4 = cn.Execute("select fname, serial from water where fname = '" & Text1.Text & "'")
With rs4
Do Until .EOF
Combo1.AddItem CStr(!serial.Value)
.MoveNext
Loop
End With