개요
어셈블리 목록을 분해하거나 분해하려고합니다. 우리는 디자인 프로그램에 의해 생성 된 입력 테이블로 시작하고 있습니다. 형식이 고정되어 있습니다.MS Access 2010 vba 쿼리
주석 열의 각 어셈블리를 해당 어셈블리와 관련된 구조 번호가있는 새 테이블로 분류하려고합니다.
새 테이블에 대한 입력으로 50 개 열을 모두 선택하여 쿼리를 만들려고했지만 열 이름을 기반으로 동적으로 쿼리를 만드는 'VBA 코드'를 찾았습니다.
코드가 작동하는 것처럼 보였지만 첫 번째 구조 번호에서 어셈블리 항목을 가져 와서 구조 번호의 나머지 부분에서 반복했습니다.
나는 너무 가깝다. 그러나 여전히 해결책에서 멀리 떨어져있다 - 누군가가 내 질문을 수정하는 방법에 대한 아이디어가 있다면 나는 모두 귀를 기울인다.
미리 감사드립니다.
M.
입력 테이블의 샘플 데이터
StructureNumber Structure Comment 2 Structure Comment 3 Structure Comment 4 Structure Comment 5 Structure Comment 6 Structure Comment 7 Structure Comment 8 Structure Comment 9 Structure Comment 10
26 1-S80-H2 1-TS-15PG 1-TMF-CB 1-TM-124TDS 1-TM-9D(22) 1-TM-103 22-TG-1G 11-TG-21C 11-TA-5L
27 1-S90-H4* 1-TBP-161A 1-C9-3A(12) 1-TMF-4B 1-TM-9D(2) 1-TM-101 2-TG-1G 1-TG-21C 1-TA-5L
28 1-S90-H5* 1-TBP-161A 1-C9-3A(12) 1-TMF-4B 1-TM-9D 1-TM-101 2-OPT-D 1-TM-N * BURY 12.5 FT
29 2-S105-H1* 1-TH-10PV4XX-SP 1-C9-3A(12)D 1-TMF-4B 1-TM-9F 2-TM-101 1-OPT-D 1-OPT-2D 1-TM-N
30 3-S90-H2 1-TH-15PDX 1-TMF-112T 1-TM-9L 3-TM-101 1-OPT-D 1-OPT-2D 1-TM-N
출력 표 SAMPLE
ID StrNum Assembly Qty
22033 26 S80-H2 1.00
22067 26 TS-15PG 1.00
22101 26 TMF-CB 1.00
22135 26 TM-124TDS 1.00
22169 26 TM-9D(22) 1.00
22203 26 TM-103 1.00
22237 26 TG-1G 22.00
22271 26 TG-21C 11.00
22305 26 TA-5L 11.00
22339 26 OPT-D 1.00
22373 26 TM-N 1.00
22034 27 S80-H2 1.00
22068 27 TS-15PG 1.00
22102 27 TMF-CB 1.00
22136 27 TM-124TDS 1.00
22170 27 TM-9D(22) 1.00
22204 27 TM-103 1.00
22238 27 TG-1G 22.00
22272 27 TG-21C 11.00
22306 27 TA-5L 11.00
22340 27 OPT-D 1.00
22374 27 TM-N 1.00
22035 28 S80-H2 1.00
22069 28 TS-15PG 1.00
22103 28 TMF-CB 1.00
22137 28 TM-124TDS 1.00
22171 28 TM-9D(22) 1.00
22205 28 TM-103 1.00
22239 28 TG-1G 22.00
22273 28 TG-21C 11.00
22307 28 TA-5L 11.00
22341 28 OPT-D 1.00
22375 28 TM-N 1.00
VBA 코드는
Option Compare Database
Function TransposeTable()
Dim rsMySet As DAO.Recordset
Dim strSQL As String
Dim OutputTable As String
Dim InputTable As String
Dim i As Integer
InputTable = "MatrixDataset"
OutputTable = "TabularDataset"
'Open the original matrix-style dataset
Set rsMySet = CurrentDb.OpenRecordset(InputTable)
'Start the count at the position number of the first column-oriented field
'Remember that Recordsets start at 0
'For j = 1 To rsMySet.RecordCount - 1
For i = 1 To rsMySet.Fields.Count - 1
'Use the recordset field.name property to build out the SQL string for the current field
strSQL = "INSERT INTO TabularDataset ([StrNum],[Assembly]) " & _
"SELECT [MatrixDataset].[StructureNumber] as StrNum," & _
"'" & rsMySet.Fields(i).Value & "'" & " AS Assembly " & "FROM MatrixDataset WHERE " & _
"'" & rsMySet.Fields(i).Value & "'" & " <> '';"
'Execute the SQL string
CurrentDb.Execute strSQL
'Move to the next column-oriented field
Next i
' Now we need to update the assembly to pull the quantity from
' the front of the field and place it in the Qty field
' UPDATE OutputTable SET Qty = left(Assy,instr(Assy,"-"-1))
strSQL = "UPDATE " & "`" & OutputTable & "` as ot" & " SET ot.Qty = left(ot.Assembly,instr(ot.Assembly,'-')-1);"
CurrentDb.Execute strSQL
strSQL = "UPDATE " & "`" & OutputTable & "` as ot" & " SET ot.Assembly = right(ot.Assembly,len(ot.Assembly)-instr(ot.Assembly,'-'));"
CurrentDb.Execute strSQL
'strSQL = "SELECT Assembly, Qty FROM `" & OutputTable & "` GROUP BY Assembly ORDER BY Assembly;"
'CurrentDb.Execute strSQL
End Function
나의 빈약 한 ACCESS VBA 스킬은 겨자를 자르지는 못했지만, 나는 항상 배워야한다. 아주 좋아. 감사! – yosso