I have a table with a column that stores time blocks separated by semi-colons (;). I would like to create a row for each time block, for example, given
I'd like to create a row for each time block
Please let me know if this possible in Access.
Edit
I tried using this query
SELECT * INTO ImportedData
FROM (
SELECT [SourceData].[Time block], [SourceData].[Work History Id],[SourceData].[Operation Code]
FROM SourceData
WHERE InStr([SourceData].[Time block], ';') = 0
UNION ALL
SELECT Left([SourceData].[Time block], InStr([SourceData].[Time block], ';') - 1),[SourceData].[Work History Id], [SourceData].[Operation Code]
FROM SourceData
WHERE InStr([SourceData].[Time block], ';') > 0
UNION ALL
SELECT Mid([SourceData].[Time block], InStr([SourceData].[Time block], ';') + 1), [SourceData].[Work History Id], [SourceData].[Operation Code]
FROM SourceData
WHERE InStr([SourceData].[Time block], ';') > 0) AS CleanedUp;
and I also tried this VBA code with no luck.
Public Sub addToTable()
Dim rstObj As DAO.Recordset, dbObj As DAO.Database
Dim InsertSQL As String
Set dbObj = CurrentDb()
Set rstObj = dbObj.OpenRecordset("Query1")
Do While Not rstObj.EOF
Dim memArr() As String
memArr = Split(rstObj.Fields("Time block"), ",")
For i = 0 To UBound(memArr)
InsertSQL = "SELECT*INTO ImportedData(Time block, Work History ID) VALUES(""" & rstObj.Fields("Time block") & """, """ & memArr(i) & """)"
DoCmd.RunSQL (InsertSQL)
Next
rstObj.MoveNext
Loop
End Sub
Found the answer here [enter link description here][1]
[1]: http://www.access-programmers.co.uk/forums/showthread.php?t=239727 for anyone that may b have the same requirements, i would hope people would actually help instead of just marking down answers or correcting the way the question is asked.
Related
I have query in MS ACCESS 2016 that I need to add autonumber to. Basically, the query output results in one column of data and I want to add a new column to this output with numbers starting from 1 to whatever the row count of the query output will be. I dont want to create a new table from my query output though. Is there a way to do this in a query? I managed to add a new ID column that just has a scalar value, however what I need is auto increment here. Any thoughts?
SELECT qry.ColA, 1 AS ID
FROM qry;
Yes, use the RowNumber function from my article:
Sequential Rows in Microsoft Access
Full code at GitHub: VBA.RowNumbers
' Builds consecutive row numbers in a select, append, or create query
' with the option of a initial automatic reset.
' Optionally, a grouping key can be passed to reset the row count
' for every group key.
'
' Usage (typical select query):
' SELECT RowNumber(CStr([ID])) AS RowID, *
' FROM SomeTable
' WHERE (RowNumber(CStr([ID])) <> RowNumber("","",True));
'
' Usage (with group key):
' SELECT RowNumber(CStr([ID]), CStr[GroupID])) AS RowID, *
' FROM SomeTable
' WHERE (RowNumber(CStr([ID])) <> RowNumber("","",True));
'
' The Where statement resets the counter when the query is run
' and is needed for browsing a select query.
'
' Usage (typical append query, manual reset):
' 1. Reset counter manually:
' Call RowNumber(vbNullString, True)
' 2. Run query:
' INSERT INTO TempTable ( [RowID] )
' SELECT RowNumber(CStr([ID])) AS RowID, *
' FROM SomeTable;
'
' Usage (typical append query, automatic reset):
' INSERT INTO TempTable ( [RowID] )
' SELECT RowNumber(CStr([ID])) AS RowID, *
' FROM SomeTable
' WHERE (RowNumber("","",True)=0);
'
' 2018-08-23. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function RowNumber( _
ByVal Key As String, _
Optional ByVal GroupKey As String, _
Optional ByVal Reset As Boolean) _
As Long
' Uncommon character string to assemble GroupKey and Key as a compound key.
Const KeySeparator As String = "¤§¤"
' Expected error codes to accept.
Const CannotAddKey As Long = 457
Const CannotRemoveKey As Long = 5
Static Keys As New Collection
Static GroupKeys As New Collection
Dim Count As Long
Dim CompoundKey As String
On Error GoTo Err_RowNumber
If Reset = True Then
' Erase the collection of keys and group key counts.
Set Keys = Nothing
Set GroupKeys = Nothing
Else
' Create a compound key to uniquely identify GroupKey and its Key.
' Note: If GroupKey is not used, only one element will be added.
CompoundKey = GroupKey & KeySeparator & Key
Count = Keys(CompoundKey)
If Count = 0 Then
' This record has not been enumerated.
'
' Will either fail if the group key is new, leaving Count as zero,
' or retrieve the count of already enumerated records with this group key.
Count = GroupKeys(GroupKey) + 1
If Count > 0 Then
' The group key has been recorded.
' Remove it to allow it to be recreated holding the new count.
GroupKeys.Remove (GroupKey)
Else
' This record is the first having this group key.
' Thus, the count is 1.
Count = 1
End If
' (Re)create the group key item with the value of the count of keys.
GroupKeys.Add Count, GroupKey
End If
' Add the key and its enumeration.
' This will be:
' Using no group key: Relative to the full recordset.
' Using a group key: Relative to the group key.
' Will fail if the key already has been created.
Keys.Add Count, CompoundKey
End If
' Return the key value as this is the row counter.
RowNumber = Count
Exit_RowNumber:
Exit Function
Err_RowNumber:
Select Case Err
Case CannotAddKey
' Key is present, thus cannot be added again.
Resume Next
Case CannotRemoveKey
' GroupKey is not present, thus cannot be removed.
Resume Next
Case Else
' Some other error. Ignore.
Resume Exit_RowNumber
End Select
End Function
I have a function I am trying to do for a database I am working on for my job. I'm not the most proficient with Access so I apologize if I am not wording this in the best way.
What I am trying to do is create a query/macro that will mimic the behavior as shown
and result into this:
The logic is as follows
1) for each record - take the LEN of the string in StdName. Take that number of characters and UPDATE that to the Name field. The remaining characters after the LEN is moved to the 'SuffixString' Field
2)for each record - count the number of occurrences of the string in the 'StdName' field for any records ON OR BEFORE the index number and UPDATE the 'Name' field with whatever is in there already and concatenate with "_n" where n is the occurence
example: index 1 - has one occurrence of 'Car1' in the StdName Field between record 1 and record 1. index 1 'Name' is changed to Car1_1
example: index 2 - has two occurrences of 'Car1' in the StdName Field between record 1 and record 2. index 2 'Name' is changed to Car1_2
example: index 6 - has one occurrence of 'Car3" in the StdName Field between record 1 and record 6. index 6 'Name' is changed to Car3_1
Can something like this be done with an access query? I've never developed in Access before and my boss really wants to see this function kept inside access instead of being moved in an out of excel.
(I have step 1 setup this way to later put in logic where StdName does not match Name. example: "Car1_1" for Name and StdName "Car2". I realize I could just Concatenate StdName with the function in step 2 in this example i described, but I have a real world purpose of doing it this way)
This will be done on an MDB format
Thank you
You can use my RowCounter function:
SELECT RowCounter(CStr([Index]),False,[StdName])) AS RowID, *
FROM YourTable
WHERE (RowCounter(CStr([Index]),False) <> RowCounter("",True));
or:
SELECT [StdName] & "_" & CStr(RowCounter(CStr([Index]),False,[StdName]))) AS RankedName, *
FROM YourTable
WHERE (RowCounter(CStr([Index]),False) <> RowCounter("",True));
Edit - to update:
UPDATE s_before
SET [Name] = [StdName] & "_" & CStr(RowCounter(CStr([Index]),False,[StdName]))
WHERE (RowCounter(CStr([Index]),False) <> RowCounter("",True));
Code:
Public Function RowCounter( _
ByVal strKey As String, _
ByVal booReset As Boolean, _
Optional ByVal strGroupKey As String) _
As Long
' Builds consecutive RowIDs in select, append or create query
' with the possibility of automatic reset.
' Optionally a grouping key can be passed to reset the row count
' for every group key.
'
' Usage (typical select query):
' SELECT RowCounter(CStr([ID]),False) AS RowID, *
' FROM tblSomeTable
' WHERE (RowCounter(CStr([ID]),False) <> RowCounter("",True));
'
' Usage (with group key):
' SELECT RowCounter(CStr([ID]),False,CStr[GroupID])) AS RowID, *
' FROM tblSomeTable
' WHERE (RowCounter(CStr([ID]),False) <> RowCounter("",True));
'
' The Where statement resets the counter when the query is run
' and is needed for browsing a select query.
'
' Usage (typical append query, manual reset):
' 1. Reset counter manually:
' Call RowCounter(vbNullString, False)
' 2. Run query:
' INSERT INTO tblTemp ( RowID )
' SELECT RowCounter(CStr([ID]),False) AS RowID, *
' FROM tblSomeTable;
'
' Usage (typical append query, automatic reset):
' INSERT INTO tblTemp ( RowID )
' SELECT RowCounter(CStr([ID]),False) AS RowID, *
' FROM tblSomeTable
' WHERE (RowCounter("",True)=0);
'
' 2002-04-13. Cactus Data ApS. CPH
' 2002-09-09. Str() sometimes fails. Replaced with CStr().
' 2005-10-21. Str(col.Count + 1) reduced to col.Count + 1.
' 2008-02-27. Optional group parameter added.
' 2010-08-04. Corrected that group key missed first row in group.
Static col As New Collection
Static strGroup As String
On Error GoTo Err_RowCounter
If booReset = True Then
Set col = Nothing
ElseIf strGroup <> strGroupKey Then
Set col = Nothing
strGroup = strGroupKey
col.Add 1, strKey
Else
col.Add col.Count + 1, strKey
End If
RowCounter = col(strKey)
Exit_RowCounter:
Exit Function
Err_RowCounter:
Select Case Err
Case 457
' Key is present.
Resume Next
Case Else
' Some other error.
Resume Exit_RowCounter
End Select
End Function
I have an access 2013 table that houses one field with comma separated values. I have created a second table that I need to parse the results into with a structure like so
uPPID number
value1 short text
value2 short text
value3 short text
value4 short text
I am dynamically creating the table so it will always have enough "value" fields to accommodate for the number that will be parsed out. Sample data is like such:
uppID values
aeo031 boat, goat, hoat, moat
And I would want the field mappings to go like such
uPPID = aeo031
value1 = boat
value2 = goat
value3 = hoat
value4 = moat
How can access vba parse out a csv list from one field to many?
There are probably faster/better solutions than the follwing VBA loop that inserts records one by one in the destination table. But for instance it does the job.
TableCSV is the name of the source table
TableFields is the name of the destination table
The constant maxValues specifies the number of fields values available
The query composes dynamically the INSERT INTO statement after composing the values fields; it completes it to provide all the columns, and adds the surrounding quotes '...'. (p.s. it could be simplified if we can insert without specifying all column values..)
.
Sub splitTable()
Const maxValues As Long = 4 ' <-- Set to number of value fields in destination table
Dim query As String, values As String, rs
Set rs = CurrentDb.OpenRecordset("TableCSV")
Do Until rs.EOF
values = rs!values ' next we add commas to provide all fields
values = values & String(maxValues - UBound(Split(values, ",")) - 1, ",")
values = "'" & Replace(values, ",", "','") & "'" ' 'a', 'b', '', '' etc
query = "INSERT INTO TableFields VALUES (" & rs!uPPID & "," & values & ")"
Debug.Print query
CurrentDb.Execute query
rs.moveNext
Loop
End Sub
I am trying to get some values - columns as rows and viceversa - from MySQL to show on a DataGridView. I have this code, that should be run in MySQL-
SET #header = CONCAT('SELECT \'sl\', ',
(SELECT GROUP_CONCAT(CONCAT(' \'', sl, '\'')) FROM cars where sl=1),
' LIMIT 0, 0');
SET #a = -1;
SET #line1 = CONCAT(
'SELECT \'Plate\',',
(
SELECT GROUP_CONCAT(
CONCAT(' (SELECT Plate FROM cars LIMIT ',
#a:=#a+1,
', 1)')
)
FROM cars where sl=1
));
SET #a := -1;
SET #line2 = CONCAT(
'SELECT \'Brand\',',
(
SELECT GROUP_CONCAT(
CONCAT(' (SELECT Brand FROM cars LIMIT ',
#a:=#a+1,
', 1)')
)
FROM cars where sl=1
));
SET #query = CONCAT('(',
#header,
') UNION (',
#line1,
') UNION (',
#line2,
')'
);
PREPARE my_query FROM #query;
EXECUTE my_query;
Now when I try to run this via the ExecuteNonQuery command by saving all this code in a string, I get a MySQLException error- Fatal error encountered during command execution.
I have tried to split the code into separate strings, but the same error popped. Also tried to increase the CommandTimeout, but nothing worked.
Is there any special way to run these statements? Or is there any problem with the code? Please note that this runs effectively without any error on the command line client.
PS: The code is used from Q# 3288014 - Thanks to Anax
EDIT:
I found a work around to the same thing, but all done in VB.
Dim sa() As String = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Dim sa2() As String = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
connect()
Dim reader As MySqlDataReader
execstr = "describe cars"
Dim cmd As New MySqlCommand(execstr, connection)
reader = cmd.ExecuteReader()
Dim i As Integer = 0
While reader.Read
sa(i) = reader.GetString(0)
i = i + 1
End While
reader.Close()
connection.Close()
connect()
execstr = "select*from cars where sl=1;"
Dim cmd2 As New MySqlCommand(execstr, connection)
reader = cmd2.ExecuteReader()
While reader.Read
For i1 As Integer = 0 To sa.Length - 1
sa2(i1) = reader.GetString(i1)
Next
End While
reader.Close()
connection.Close()
reader.Close()
connection.Close()
Dim t As New DataTable
t.Columns.Add(sa(0))
t.Columns.Add(sa2(0))
For y As Integer = 1 To sa.Length - 1
t.Rows.Add(sa(y), sa2(y))
Next
DataGridView1.DataSource = t
Funny to find that what all can be done in a string in MySQL, takes so much code in VB.
You can't do that in a single or series of ExecuteNonQuery command(s). This example was showing the code you would put in a stored procedure on mysql. The you call the stored procedure using a DataAdapter or DataReader.
I have a MySQL stored procedure generating a resultset. The query uses GROUP_CONCAT to select rows as columns.
SQL Code snippet
SELECT CONCAT(
' SELECT MDE_ID, MDE_CreatedOn, MDE_LastEditedOn '
, GROUP_CONCAT(CONCAT(
' ,t', MDF_ID, '.MDEMDF_Value AS `', REPLACE(MDF_Label, '`', '``'), '`'
) SEPARATOR ' ')
Following, I want to sort the recordset on a column.
SQL = "CALL moduleentry_select(" & prjid & ",0,26,0)"
set rs = Server.CreateObject("ADODB.Recordset")
rs.CursorLocation = 3
rs.LockType = 3
rs.Open SQL, Conn
rs.Sort = "volgnummer"
I have verified and the column is in the recordset.
I get the following error:
Microsoft Cursor Engine error '80040e04'
Row handle is invalid.
default.asp, line 176
Line 176 is rs.Sort = "volgnummer"
edit
The column 'Volgnummer' doesn't actually exist in the database. It is selected dynamically via a pivoting principle, using GROUP_CONCAT. Referring to this SA answer: joined table rows into columns with column titles from original rows