MS Access Update table with 7 Collections Or 1 Array? - ms-access

So trying to keep this section very brief:
I have two tables. Tbl1 has the financial accounts by year for each company in seperate rows.
Table2 has each company only once and all the financial data is now in one row.
How do i do that? Currently attempting it with Collections have a second attempt going with Arrays.
Hi guys so i have two tables Figs1 and Sabi. Figs1 is set up like: NIF,PeriodEnding, Materials, Depreciation, Non-Trading Income, Total Interest, Pretax Profit, TotalEmpRemu.
So you'll get repeating Company IDs with each each of their financials as the rows.
In Sabi it changes to each company has 1 row and all that data is in columns e.g. PeriodEnding_Latest, PeriodEnding -1, PeriodEnding -2 etc. till -6. I have made a collection for each column in Figs1 and i want to update the table Sabi in the correct order.
So PeriodEnding collection will have {(31/12/2018), (31/12/2017), (31/12/2016), (31/12/2015), (31/12/2014), (31/12/2013)}
Those values need to go to PeriodEnding_Latest, PeriodEnding -1, PeriodEnding -2 etc.
I have the update SQL Statement and filled it with variables:
SQL = "UPDATE SabiFigures1 SET SabiFigures1.[Closing Date Last avail yr] = '& DateFiled1 &', SabiFigures1.[Closing Date Year - 1] = '& DateFiled2 &', " & _
"SabiFigures1.[Closing Date Year - 2] = '& DateFiled3 &', SabiFigures1.[Closing Date Year - 3] = '& DateFiled4 &', SabiFigures1.[Closing Date Year - 4] = '& DateFiled5 &, " & _
"SabiFigures1.[Closing Date Year - 5] = '& DateFiled6 &', SabiFigures1.[Material costs th EUR Last avail yr] = '2933', SabiFigures1.[Material costs th EUR Year - 1] " & _
"= '2791', SabiFigures1.[Material costs th EUR Year - 2] = '3721', SabiFigures1.[Material costs th EUR Year - 3] = '3021', SabiFigures1.[Material costs th EUR Year - 4] " & _
"= '3005', SabiFigures1.[Material costs th EUR Year - 5] = '1890', SabiFigures1.[Depreciation th EUR Last avail yr] = '49', SabiFigures1.[Depreciation th EUR Year - 1] = " & _
"'52', SabiFigures1.[Depreciation th EUR Year - 2] = '47', SabiFigures1.[Depreciation th EUR Year - 3] = '42', SabiFigures1.[Depreciation th EUR Year - 4] = '54', " & _
"SabiFigures1.[Depreciation th EUR Year - 5] = '63', SabiFigures1.[Financial revenue th EUR Last avail yr] = Null, SabiFigures1.[Financial revenue th EUR Year - 1] " & _
"= Null, SabiFigures1.[Financial revenue th EUR Year - 2] = Null, SabiFigures1.[Financial revenue th EUR Year - 3] = Null, SabiFigures1.[Financial revenue th EUR Year " & _
"- 4] = Null, SabiFigures1.[Financial revenue th EUR Year - 5] = Null, SabiFigures1.[Financial expenses th EUR Last avail yr] = Null, SabiFigures1.[Financial expenses " & _
"th EUR Year - 1] = Null, SabiFigures1.[Financial expenses th EUR Year - 2] = Null, SabiFigures1.[Financial expenses th EUR Year - 3] = Null, " & _
"SabiFigures1.[Financial expenses th EUR Year - 4] = Null, SabiFigures1.[Financial expenses th EUR Year - 5] = Null, SabiFigures1." & _
"[P/L before tax th EUR Last avail yr] = '407', SabiFigures1.[P/L before tax th EUR Year - 1] = '252', SabiFigures1.[P/L before tax th EUR Year - 2] " & _
"= '1076', SabiFigures1.[P/L before tax th EUR Year - 3] = '597', SabiFigures1.[P/L before tax th EUR Year - 4] = '329', SabiFigures1.[P/L before tax th EUR Year - 5] = " & _
"'102', SabiFigures1.[Cost of employees th EUR Last avail yr] = '1226', SabiFigures1.[Cost of employees th EUR Year - 1] = '1205', SabiFigures1.[Cost of employees th EUR Year - 2] " & _
"= '1310', SabiFigures1.[Cost of employees th EUR Year - 3] = '1157', SabiFigures1.[Cost of employees th EUR Year - 4] = '1319', SabiFigures1.[Cost of employees th EUR Year - 5] = '1342' " & _
"WHERE (((SabiFigures1.[NIF Code])='A01011550'));"
db.Execute SQL
The code pretty much goes to first table: Figs1 and get the first regnumber it will then go to Sabi where i have prepopulated the unqiue NIFs. If it finds a correspondencing NIF in Figs1 and Sabi then it should fill out the variables from the collection however, i dont know how to do variable "variables". E.g. the base structure of the variable should be DateFiled but as it loops through the collection of PeriodEndings it should change from DateFiled1 till Datefiled6.
Set rsFigs1 = CurrentDb.OpenRecordset("Select * FROM Figs1Ready ORDER BY NIF, PeriodEnding DESC;")
If Not (rsFigs1.EOF And rsFigs1.BOF) Then
rsFigs1.MoveFirst
Do Until rsFigs1.EOF = True
NIF = rsFigs1!NIF
Set rsFormat = CurrentDb.OpenRecordset("Select * FROM SabiFigures1;")
If Not (rsFormat.EOF And rsFormat.BOF) Then
rsFormat.MoveFirst
Do Until rsFormat.EOF = True
nIFF = rsFormat![NIF Code]
If NIF = nIFF Then
Set qdfDef = CurrentDb.QueryDefs("PopulateSabiFigures1")
qdfDef.Parameters("NIF: ").Value = nIFF
Set rstDef = qdfDef.OpenRecordset()
Set PeriodEnding = RSToColl(rstDef, "PeriodEnding")
Set Materials1 = RSToColl(rstDef, "Materials")
Set Depreciation1 = RSToColl(rstDef, "Depreciation")
Set NonTrading1 = RSToColl(rstDef, "Non-Trading Income")
Set TotalInterest = RSToColl(rstDef, "Total_Interest_Charges")
Set Pretax = RSToColl(rstDef, "Pretax_Profit")
Set TotalRemu = RSToColl(rstDef, "Total_Empl_Remu_000")
For i = 1 To 6
VariableName = "DateFiled" & i
Next i
For Each Period In PeriodEnding
Debug.Print TypeName(Period)
Next
End If
SQL = 'The massive SQL statement above
db.Execute SQL
rsFormat.MoveNext
Loop
End If
rsFigs1.MoveNext
Loop
End If
rstDef.Close
Set rstDef = Nothing
rsFormat.Close
Set rsFormat = Nothing
rsFigs1.Close
Set rsFigs1 = Nothing
Maybe i'm looking at the problem in the wrong way any help and pointers would be appreciated. Thanks in advance. Added some pictures hopefully it helps
To Gustav who first suggested an Array i can do something like this:
This will bring back every year(row) individually. I would like to update a row in one go to save on time instead updating each yearly value. Pretty much updating each row 6 times which would take a lot longer I imagine.
Set rsFormat = CurrentDb.OpenRecordset("Select * FROM SabiFigures1;")
If Not (rsFormat.EOF And rsFormat.BOF) Then
rsFormat.MoveFirst
Do Until rsFormat.EOF = True
nIFF = rsFormat![NIF Code]
Set qdfDef = CurrentDb.QueryDefs("PopulateSabiFigures1")
qdfDef.Parameters("NIF: ").Value = nIFF
Set rstDef = qdfDef.OpenRecordset()
rstDef.MoveLast
rstDef.MoveFirst
varRecord = rstDef.GetRows(rstDef.RecordCount)
For intI = 0 To 5 'UBound(varRecord, 2)
For intJ = 0 To UBound(varRecord, 1)
Debug.Print varRecord(intJ, intI)
Next intJ
Next intI
'whole row updated here after each value of the array is passed to variables for each year and financial value.
rstDef.Close
Set rstDef = Nothing
rsFormat.MoveNext
Loop
End If

Hi there so i figured it out. The code is very bulky so anyone who can make it a bit more streamlined and flexible e.g. it the columns i need changed would be much appreciated.
Dim db As DAO.Database
Set db = CurrentDb
Dim rsFormat As DAO.Recordset
Dim rsFigs1 As Object
Dim qdfDef As DAO.QueryDef
Dim rstDef As Object
Dim varRecord As Variant
Dim NIF As String
Dim nIFF As String
Dim intI As Integer
Dim intJ As Integer
Dim RegNum, LatestDate, Date1, Date2, Date3, Date4, Date5, LatestMaterial, Material1, Material2, Material3, Material4, Material5, LatestDepreciation, Depreciation1, Depreciation2 As String
Dim Depreciation3, Depreciation4, Depreciation5, LatestTrading, Trading1, Trading2, Trading3, Trading4, Trading5, LatestTotalInterest, TotalInterest1, TotalInterest2, TotalInterest3 As String
Dim TotalInterest4, TotalInterest5, LatestPreTaxProfit, PreTaxProfit1, PreTaxProfit2, PreTaxProfit3, PreTaxProfit4, PreTaxProfit5, LatestTotEmpRem, TotEmpRem1, TotEmpRem2, TotEmpRem3 As String
Dim TotEmpRem4, TotEmpRem5, SQL As String
Set rsFormat = CurrentDb.OpenRecordset("Select [NIF Code] FROM SabiFigures1;")
If Not (rsFormat.EOF And rsFormat.BOF) Then
rsFormat.MoveFirst
Do Until rsFormat.EOF = True
nIFF = rsFormat![NIF Code]
Set qdfDef = CurrentDb.QueryDefs("PopulateSabiFigures1")
qdfDef.Parameters("NIF: ").Value = nIFF
Set rstDef = qdfDef.OpenRecordset()
rstDef.MoveLast
rstDef.MoveFirst
varRecord = rstDef.GetRows(rstDef.RecordCount)
For intI = 0 To 5
For intJ = 0 To UBound(varRecord, 1)
Debug.Print varRecord(intJ, intI)
On Error Resume Next
If intI = 0 Then
If intJ = 0 Then
RegNum = varRecord(intJ, intI)
ElseIf intJ = 1 Then
LatestDate = varRecord(intJ, intI)
ElseIf intJ = 2 Then
LatestMaterial = varRecord(intJ, intI)
ElseIf intJ = 3 Then
LatestDepreciation = varRecord(intJ, intI)
ElseIf intJ = 4 Then
LatestTrading = varRecord(intJ, intI)
ElseIf intJ = 5 Then
LatestTotalInterest = varRecord(intJ, intI)
ElseIf intJ = 6 Then
LatestPreTaxProfit = varRecord(intJ, intI)
ElseIf intJ = 7 Then
LatestTotEmpRem = varRecord(intJ, intI)
Else
MsgBox "Error in Loop"
Exit Sub
End If
ElseIf intI = 1 Then
If intJ = 0 Then
RegNum = varRecord(intJ, intI)
ElseIf intJ = 1 Then
Date1 = varRecord(intJ, intI)
ElseIf intJ = 2 Then
Material1 = varRecord(intJ, intI)
ElseIf intJ = 3 Then
Depreciation1 = varRecord(intJ, intI)
ElseIf intJ = 4 Then
Trading1 = varRecord(intJ, intI)
ElseIf intJ = 5 Then
TotalInterest1 = varRecord(intJ, intI)
ElseIf intJ = 6 Then
PreTaxProfit1 = varRecord(intJ, intI)
ElseIf intJ = 7 Then
TotEmpRem1 = varRecord(intJ, intI)
Else
MsgBox "Error in Loop"
Exit Sub
End If
ElseIf intI = 2 Then
If intJ = 0 Then
RegNum = varRecord(intJ, intI)
ElseIf intJ = 1 Then
Date2 = varRecord(intJ, intI)
ElseIf intJ = 2 Then
Material2 = varRecord(intJ, intI)
ElseIf intJ = 3 Then
Depreciation2 = varRecord(intJ, intI)
ElseIf intJ = 4 Then
Trading2 = varRecord(intJ, intI)
ElseIf intJ = 5 Then
TotalInterest2 = varRecord(intJ, intI)
ElseIf intJ = 6 Then
PreTaxProfit2 = varRecord(intJ, intI)
ElseIf intJ = 7 Then
TotEmpRem2 = varRecord(intJ, intI)
Else
MsgBox "Error in Loop"
Exit Sub
End If
ElseIf intI = 3 Then
If intJ = 0 Then
RegNum = varRecord(intJ, intI)
ElseIf intJ = 1 Then
Date3 = varRecord(intJ, intI)
ElseIf intJ = 2 Then
Material3 = varRecord(intJ, intI)
ElseIf intJ = 3 Then
Depreciation3 = varRecord(intJ, intI)
ElseIf intJ = 4 Then
Trading3 = varRecord(intJ, intI)
ElseIf intJ = 5 Then
TotalInterest3 = varRecord(intJ, intI)
ElseIf intJ = 6 Then
PreTaxProfit3 = varRecord(intJ, intI)
ElseIf intJ = 7 Then
TotEmpRem3 = varRecord(intJ, intI)
Else
MsgBox "Error in Loop"
Exit Sub
End If
ElseIf intI = 4 Then
If intJ = 0 Then
RegNum = varRecord(intJ, intI)
ElseIf intJ = 1 Then
Date4 = varRecord(intJ, intI)
ElseIf intJ = 2 Then
Material4 = varRecord(intJ, intI)
ElseIf intJ = 3 Then
Depreciation4 = varRecord(intJ, intI)
ElseIf intJ = 4 Then
Trading4 = varRecord(intJ, intI)
ElseIf intJ = 5 Then
TotalInterest4 = varRecord(intJ, intI)
ElseIf intJ = 6 Then
PreTaxProfit4 = varRecord(intJ, intI)
ElseIf intJ = 7 Then
TotEmpRem4 = varRecord(intJ, intI)
Else
MsgBox "Error in Loop"
Exit Sub
End If
ElseIf intI = 5 Then
If intJ = 0 Then
RegNum = varRecord(intJ, intI)
ElseIf intJ = 1 Then
Date5 = varRecord(intJ, intI)
ElseIf intJ = 2 Then
Material5 = varRecord(intJ, intI)
ElseIf intJ = 3 Then
Depreciation5 = varRecord(intJ, intI)
ElseIf intJ = 4 Then
Trading5 = varRecord(intJ, intI)
ElseIf intJ = 5 Then
TotalInterest5 = varRecord(intJ, intI)
ElseIf intJ = 6 Then
PreTaxProfit5 = varRecord(intJ, intI)
ElseIf intJ = 7 Then
TotEmpRem5 = varRecord(intJ, intI)
Else
MsgBox "Error in Loop"
Exit Sub
End If
Else
MsgBox "Error in Loop"
Exit Sub
End If
Next intJ
Next intI
SQL = "UPDATE SabiFigures1 SET SabiFigures1.[Closing Date Last avail yr] = '" & LatestDate & "', SabiFigures1.[Closing Date Year - 1] = '" & Date1 & "', " & _
"SabiFigures1.[Closing Date Year - 2] = '" & Date2 & "', SabiFigures1.[Closing Date Year - 3] = '" & Date3 & "', SabiFigures1.[Closing Date Year - 4] = '" & Date4 & "', " & _
"SabiFigures1.[Closing Date Year - 5] = '" & Date5 & "', SabiFigures1.[Material costs th EUR Last avail yr] = '" & LatestMaterial & "', SabiFigures1.[Material costs th EUR Year - 1] " & _
"= '" & Material1 & "', SabiFigures1.[Material costs th EUR Year - 2] = '" & Material2 & "', SabiFigures1.[Material costs th EUR Year - 3] = '" & Material3 & "', SabiFigures1.[Material costs th EUR Year - 4] " & _
"= '" & Material4 & "', SabiFigures1.[Material costs th EUR Year - 5] = '" & Material5 & "', SabiFigures1.[Depreciation th EUR Last avail yr] = '" & LatestDepreciation & "', SabiFigures1.[Depreciation th EUR Year - 1] = " & _
"'" & Depreciation1 & "', SabiFigures1.[Depreciation th EUR Year - 2] = '" & Depreciation2 & "', SabiFigures1.[Depreciation th EUR Year - 3] = '" & Depreciation3 & "', SabiFigures1.[Depreciation th EUR Year - 4] = '" & Depreciation4 & "', " & _
"SabiFigures1.[Depreciation th EUR Year - 5] = '" & Depreciation5 & "', SabiFigures1.[Financial revenue th EUR Last avail yr] = '" & LatestTrading & "', SabiFigures1.[Financial revenue th EUR Year - 1] " & _
"= '" & Trading1 & "', SabiFigures1.[Financial revenue th EUR Year - 2] = '" & Trading2 & "', SabiFigures1.[Financial revenue th EUR Year - 3] = '" & Trading3 & "', SabiFigures1.[Financial revenue th EUR Year " & _
"- 4] = '" & Trading4 & "', SabiFigures1.[Financial revenue th EUR Year - 5] = '" & Trading5 & "', SabiFigures1.[Financial expenses th EUR Last avail yr] = '" & LatestTotalInterest & "', SabiFigures1.[Financial expenses " & _
"th EUR Year - 1] = '" & TotalInterest1 & "', SabiFigures1.[Financial expenses th EUR Year - 2] = '" & TotalInterest2 & "', SabiFigures1.[Financial expenses th EUR Year - 3] = '" & TotalInterest3 & "', " & _
"SabiFigures1.[Financial expenses th EUR Year - 4] = '" & TotalInterest4 & "', SabiFigures1.[Financial expenses th EUR Year - 5] = '" & TotalInterest5 & "', SabiFigures1." & _
"[P/L before tax th EUR Last avail yr] = '" & LatestPreTaxProfit & "', SabiFigures1.[P/L before tax th EUR Year - 1] = '" & PreTaxProfit1 & "', SabiFigures1.[P/L before tax th EUR Year - 2] " & _
"= '" & PreTaxProfit2 & "', SabiFigures1.[P/L before tax th EUR Year - 3] = '" & PreTaxProfit3 & "', SabiFigures1.[P/L before tax th EUR Year - 4] = '" & PreTaxProfit4 & "', SabiFigures1.[P/L before tax th EUR Year - 5] = " & _
"'" & PreTaxProfit5 & "', SabiFigures1.[Cost of employees th EUR Last avail yr] = '" & LatestTotEmpRem & "', SabiFigures1.[Cost of employees th EUR Year - 1] = '" & TotEmpRem1 & "', SabiFigures1.[Cost of employees th EUR Year - 2] " & _
"= '" & TotEmpRem2 & "', SabiFigures1.[Cost of employees th EUR Year - 3] = '" & TotEmpRem3 & "', SabiFigures1.[Cost of employees th EUR Year - 4] = '" & TotEmpRem4 & "', SabiFigures1.[Cost of employees th EUR Year - 5] = '" & TotEmpRem5 & "' " & _
"WHERE (((SabiFigures1.[NIF Code])='" & RegNum & "'));"
db.Execute SQL
SQL = ""
RegNum = ""
LatestDate = ""
Date1 = ""
Date2 = ""
Date3 = ""
Date4 = ""
Date5 = ""
LatestMaterial = ""
Material1 = ""
Material2 = ""
Material3 = ""
Material4 = ""
Material5 = ""
LatestDepreciation = ""
Depreciation1 = ""
Depreciation2 = ""
Depreciation3 = ""
Depreciation4 = ""
Depreciation5 = ""
LatestTrading = ""
Trading1 = ""
Trading2 = ""
Trading3 = ""
Trading4 = ""
Trading5 = ""
LatestTotalInterest = ""
TotalInterest1 = ""
TotalInterest2 = ""
TotalInterest3 = ""
TotalInterest4 = ""
TotalInterest5 = ""
LatestPreTaxProfit = ""
PreTaxProfit1 = ""
PreTaxProfit2 = ""
PreTaxProfit3 = ""
PreTaxProfit4 = ""
PreTaxProfit5 = ""
LatestTotEmpRem = ""
TotEmpRem1 = ""
TotEmpRem2 = ""
TotEmpRem3 = ""
TotEmpRem4 = ""
TotEmpRem5 = ""
'For intI = 6 To UBound(varRecord, 2)
' For intJ = 0 To UBound(varRecord, 1)
' Debug.Print varRecord(intJ, intI)
' Next intJ
'Next intI
rstDef.Close
Set rstDef = Nothing
rsFormat.MoveNext
Loop
End If

Related

Find and replace subscripts and superscripts in MS Excel

I would like to find all numbers and formulas which contain subscripts and superscripts in excel cells and replace it with html tags for subscripts and superscripts.
Eg. cell containing a2 + (b3 - c) would be replaced as:
a<sup>2</sup> + (b<sup>3</sup> - c)
Thanks a lot.
Try this one:
Sub test()
Dim ColNo, RowNo As Long
Dim NewStr As String
Set ws1 = Worksheets("q")
Set ws2 = Worksheets("q-a")
ws1.Activate
With ws1
RowNo = .UsedRange.SpecialCells(xlCellTypeLastCell).Row
ColNo = .UsedRange.SpecialCells(xlCellTypeLastCell).Column
For i = 1 To ColNo
For j = 1 To RowNo
l = 1
NewStr = .Cells(j, i).Value2
For k = 1 To Len(.Cells(j, i).Value2) - 1
If .Cells(j, i).Characters(k + 1, 1).Font.Superscript = True Then
NewStr = Mid(NewStr, 1, k - 1 + l) & "<sup>" & Mid(.Cells(j, i) _
.Value2, k + 1, 1) & "</sup>" & Mid(.Cells(j, i).Value2, _
k + 2, Len(.Cells(j, i).Value2) - (k + 1))
l = l + 11
ElseIf .Cells(j, i).Characters(k + 1, 1).Font.Subscript = True Then
NewStr = Mid(NewStr, 1, k - 1 + l) & "<sub>" & Mid(.Cells(j, i)._
Value2, k + 1, 1) & "</sub>" & Mid(.Cells(j, i).Value2, _
k + 2, Len(.Cells(j, i).Value2) - (k + 1))
l = l + 11
End If
Next
l = 1
For k = 1 To Len(NewStr) - 1
If InStr(k, NewStr, "</sup><sup>", vbBinaryCompare) = k Then
NewStr = Mid(NewStr, 1, k - 1) & Mid(NewStr, k + 11, Len(NewStr) - (k + 10))
ElseIf InStr(1, NewStr, "</sub><sub>", vbBinaryCompare) <> 0 Then
NewStr = Mid(NewStr, 1, k - 1) & Mid(NewStr, k + 11, Len(NewStr) - (k + 10))
End If
Next
ws2.Cells(j, i).Value2 = NewStr
NewStr = ""
Next
Next
End With
End Sub
According with the new information, I just make it simpler.
Hope it helps
As far as I try, that one works always but fail on the first character. So if the firs character is Subscript or Superscript, it will fail.
The code it is for the superscript case. For the subscript case, just change .Font.Subscript by .Font.Superscript and whatever code on html (<sup> on the superscript).
Sub test()
Dim ColNo, RowNo As Long
Dim Pos(500) As Integer
Dim Str(500) As String
Dim sType(500) as String
Dim NewStr As String
Set ws1 = Worksheets("Hoja1")
Set ws2 = Worksheets("Hoja2")
ws1.Activate
With ws1
RowNo = .UsedRange.SpecialCells(xlCellTypeLastCell).Row
ColNo = .UsedRange.SpecialCells(xlCellTypeLastCell).Column
l = 2
For i = 1 To ColNo
For j = 1 To RowNo
Pos(1) = 1
For k = 1 To Len(.Cells(j, i).Value2) - 1
If .Cells(j, i).Characters(k + 1, 1).Font.Superscript = True Then
Pos(l) = k + 1
sType(l) = "Sup"
l = l + 1
ElseIf .Cells(j, i).Characters(k + 1, 1).Font.Subscript = True Then
Pos(l) = k + 1
sType(l) = "Sub"
l = l + 1
End If
Next
For k = 1 To l - 1
If Pos(k + 1) > Pos(k) Then
If sType(l + 1) = "Sup" Then
Str(2 * k) = Mid(.Cells(j, i).Value2, Pos(k), Pos(k + 1) - Pos(k))
Str(2 * k - 1) = Mid(.Cells(j, i).Value2, Pos(k + 1), 1)
Str(2 * k - 1) = "<sup>" & Str(2 * k - 1) & "</sup>"
NewStr = NewStr & Str(2 * k) & Str(2 * k - 1)
ElseIf sType(l + 1) = "Sub" Then
Str(2 * k) = Mid(.Cells(j, i).Value2, Pos(k), Pos(k + 1) - Pos(k))
Str(2 * k - 1) = Mid(.Cells(j, i).Value2, Pos(k + 1), 1)
Str(2 * k - 1) = "<sub>" & Str(2 * k - 1) & "</sub>"
NewStr = NewStr & Str(2 * k) & Str(2 * k - 1)
End If
End If
Next
If NewStr <> "" Then
NewStr = NewStr + Mid(.Cells(j, i).Value2, _
Pos(l - 1), Len(.Cells(j, i).Value2) - Pos(l - 1))
Else
NewStr = .Cells(j, i).Value2
End If
ws2.Cells(j, i).Value2 = NewStr
NewStr = ""
For k = 1 To l - 1
Pos(k) = 0
sType(k) = ""
Str(2 * k) = ""
Str(2 * k - 1) = ""
Next
l = 2
Next
Next
End With
End Sub
Hope it helps

How to use subquery in SELECT clause?

I have two table in my database main_item_stock and stock_history
I want to create a report to show that how many stock in, stock out and wastage for each item in main_item_stock, for every action that user do will insert a record into stock_history.
main_item_stock
-----------------------------------------------
stkitm_id stkitm_code stkitm_name stkitm_qty
-----------------------------------------------
1 S001 Apple 10.000
2 S002 Orange 5.000
-----------------------------------------------
stock_history
-------------------------------------------------------------------------------------
stkhis_id sktitm_id stkitm_code stkitm_name stkhis_type stkhis_qty created_date
1 1 S001 Apple Wastage 1.000 2017/03/13
2 2 S002 Orange Stock Out 0.500 2017/03/12
3 2 S002 Orange Stock In 0.100 2017/03/13
-------------------------------------------------------------------------------------
Result that I want:-
----------------------------------------------------------------------------------------------
stkitm_code stkitm_name created_date item_onhand item_stockin item_stockout item_wastage
----------------------------------------------------------------------------------------------
S001 Apple 2017/03/13 9.000 0 0 1
S002 Orange 2017/03/12 4.500 0 0.500 0
S002 Orange 2017/03/13 4.600 0.100 0 0
*item_onhand = main_item_stock.stkitm_qty
I have tried below query but stockin,stockout,wastage always return same value
query = "SELECT main_item_stock.stkitm_code,main_item_stock.stkitm_name,main_item_stock.stkitm_qty,
(SELECT SUM(stkhis_qty) FROM stock_history WHERE stkhis_type = 'Stock Out'"
If dtp_from.Checked Then
query &= " AND DATE(stock_history.created_date) >= '" & Format(CDate(dtp_from.Text), "yyyy-MM-dd") & "'
AND DATE(stock_history.created_date) <= '" & Format(CDate(dtp_to.Text), "yyyy-MM-dd") & "'"
End If
query &= ") AS 'item_stockout',
(SELECT SUM(stkhis_qty) FROM stock_history WHERE stkhis_type = 'Wastage'"
If dtp_from.Checked Then
query &= " AND DATE(stock_history.created_date) >= '" & Format(CDate(dtp_from.Text), "yyyy-MM-dd") & "'
AND DATE(stock_history.created_date) <= '" & Format(CDate(dtp_to.Text), "yyyy-MM-dd") & "'"
End If
query &= ") AS 'item_wastage',
(SELECT SUM(stkhis_qty) FROM stock_history WHERE stkhis_type = 'Stock In'"
If dtp_from.Checked Then
query &= " AND DATE(stock_history.created_date) >= '" & Format(CDate(dtp_from.Text), "yyyy-MM-dd") & "'
AND DATE(stock_history.created_date) <= '" & Format(CDate(dtp_to.Text), "yyyy-MM-dd") & "'"
End If
query &= ") AS 'item_stockin'
FROM main_item_stock
LEFT JOIN stock_history
ON main_item_stock.stkitm_id = stock_history.stkitm_id"
query &= " GROUP BY stock_history.stkitm_id"
How can I change the query and get the result that I want?
UPDATE:
I have tried another query, this time the return data is correct but can I use this table to create the report that I want?
SELECT SUM(stkhis_qty),stkitm_name,stkhis_type,DATE(created_date)
FROM stock_history
WHERE DATE(created_date) >= '2017/03/12'
GROUP BY stkitm_code,stkhis_type,DATE(created_date)
result:-
How can I changed image1 table to image2? The report need to have all these column
UPDATE:
This is main_item_stock
This is stock_history
Result should look like picture below, both type also can.
*the last record date on the picture is wrong should be 3/17/2017
This should get you pretty close...
*edited to reflect the change of items on hand rather than the current total number of items on hand.
SELECT
sh.stkitm_code,
sh.stkitm_name,
SUM(IF(stkhis_type = 'Stock In',
sh.stkhis_qty,
-1 * sh.stkhis_qty)) AS change_of_item_onhand,
SUM(IF(stkhis_type = 'Stock In',
sh.stkhis_qty,
0)) AS item_stockin,
SUM(IF(stkhis_type = 'Stock Out',
sh.stkhis_qty,
0)) AS item_stockout,
SUM(IF(stkhis_type = 'Wastage',
sh.stkhis_qty,
0)) AS item_wastage,
sh.created_date
FROM stock_history sh
LEFT JOIN main_item_stock mis ON mis.stkitm_code = sh.stkitm_code
WHERE sh.created_date > "<Oldest date>"
AND sh.created_date < "<Youngest date>"
GROUP BY stkitm_code
Which should result in output that looks like:
Although you'd probably want to group by the stkitm_code and date.
Alternatively, removing the "SUM"s and "GROUP BY" will itemize it, but the item_onhand column won't be cumulative (there might be a slick way to do that though)
SELECT
sh.stkitm_code,
sh.stkitm_name,
IF(stkhis_type = 'Wastage' OR
stkhis_type = 'Stock Out',
mis.stkitm_qty - sh.stkhis_qty,
mis.stkitm_qty + sh.stkhis_qty) AS item_onhand,
IF(stkhis_type = 'Stock In',
sh.stkhis_qty,
0) AS item_stockin,
IF(stkhis_type = 'Stock Out',
sh.stkhis_qty,
0) AS item_stockout,
IF(stkhis_type = 'Wastage',
sh.stkhis_qty,
0) AS item_wastage,
sh.created_date
FROM stock_history sh
LEFT JOIN main_item_stock mis ON mis.stkitm_code = sh.stkitm_code
WHERE sh.created_date > "<Oldest date>"
AND sh.created_date < "<Youngest date>"
Which will end up looking like this:
Hope this helps!

select query to sum average fields from table1 and count field from table

Table: scores
Judge No Name Casual Barong Talent Swimsuit Formal
Judge1 Ginoo 1 John 85 85 85 85 85
Judge2 Ginoo 1 John 84 86 88 82 83
Judge3 Ginoo 1 John 90 86 84 87 87
Judge1 Ginoo 2 David 85 85 85 85 85
Judge2 Ginoo 2 David 89 81 83 84 85
Judge3 Ginoo 2 David 87 84 83 87 88
Table: textvote
No Sender
Ginoo 1 9307895654
Ginoo 1 9566551234
Ginoo 1 9232235643
Ginoo 2 9225557878
Query Result
Rank No Name Casual Barong Talent Swimsuit Formal Textvote Total
Champion Ginoo 1 John 86.33 85.67 85.67 84.67 85.00 93.75 86.73
1stRunup Ginoo 2 David 87.00 83.33 83.67 85.33 86.00 81.25 84.32
2ndRunup
This is my latest code:
"SELECT s.no, s.name, AVG(s.casual) AS Casual, AVG(s.barong) AS Barong, AVG(s.swimsuit) AS Swimsuit, AVG(s.formal) AS Formal, " & _
"(select count(*) / (select count(*) from (textvote) where no like '%Ginoo%') * (100 / count(*)) + (100 - (100 / count(*))) AS 'Text Vote', " & _
"(AVG(s.casual) * 0.15) + (AVG(s.barong) * 0.25) + (AVG(s.swimsuit) * 0.15) + (AVG(s.formal) * 0.15) + " & _
"(select count(*) / (select count(*) from (textvote) where no like '%Ginoo%') * (100 / count(*)) + (100 - (100 / count(*))) * 0.15 AS Total " & _
"FROM scores s " & _
"INNER JOIN textvote t ON s.no = t.no " & _
"WHERE t.no LIKE '%Ginoo%' " & _
"GROUP BY t.no"
I got this error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near " at line 1
Until I know how your calculating textvote I have left textvotecalc blank (fill in yourself):
SELECT s.candidate, AVG(s.gown) AS "gown 30%",
AVG(s.talent) AS "talent 30%",
?textvotecalc? AS "textvote 40% ",
((AVG(s.gown)*100)/30) + ((AVG(s.talent)*100)/30) + ((?textvotecalc?*100)/40) AS TOTAL
FROM scores s
INNER JOIN textvote t ON s.candidate = t.candidate
GROUP BY s.candidate
EDIT:
You had an extra ) at the end of the TOTAL calculation.
SELECT s.no, s.name, AVG(s.casual) AS Casual, AVG(s.barong) AS Barong, AVG(s.swimsuit) AS Swimsuit, AVG(s.formal) AS Formal,
(COUNT(t.no) / " & TextVoteCountGinoo & ") * (" & TextVoteCountGinoo & " - COUNT(t.no)) + (" & TextVoteCountGinoo & " - (" & TextVoteCountGinoo & " - COUNT(t.no))) AS'Text Vote',
(AVG(s.casual) * 0.15) + (AVG(s.barong) * 0.25) + (AVG(s.swimsuit) * 0.15) + (AVG(s.formal) * 0.15) + (COUNT(t.no) / " & TextVoteCountGinoo & ") * (100 / " & TextVoteCountGinoo & ") + (100 - (100 / " & TextVoteCountGinoo & ") ) * 0.15 AS Total
FROM scores s
INNER JOIN textvote t ON s.no = t.no
WHERE s.no LIKE '%Ginoo%'
GROUP BY s.no
In your VB format:
SELECT s.no, s.name, AVG(s.casual) AS Casual, AVG(s.barong) AS Barong, AVG(s.swimsuit) AS Swimsuit, AVG(s.formal) AS Formal, & _
"(COUNT(t.no) / " & TextVoteCountGinoo & ") * (" & TextVoteCountGinoo & " - COUNT(t.no)) + (" & TextVoteCountGinoo & " - (" & TextVoteCountGinoo & " - COUNT(t.no))) AS'Text Vote'," & _
"(AVG(s.casual) * 0.15) + (AVG(s.barong) * 0.25) + (AVG(s.swimsuit) * 0.15) + (AVG(s.formal) * 0.15) +" & _ "(COUNT(t.no) / " & TextVoteCountGinoo & ") * (100 / " & TextVoteCountGinoo & ") + (100 - (100 / " & TextVoteCountGinoo & ") ) * 0.15 AS Total" & _
"FROM scores s
INNER JOIN textvote t ON s.no = t.no
WHERE s.no LIKE '%Ginoo%'
GROUP BY s.no"

How to calculate checksum digit for EAN-14?

I am trying to validate EAN 14 UPC code in vba access. I am trying to find it online but no luck. I just found for EAN 8 and EAN 13. So, I just tried to code it similar to EAN 13 as following:
If Len(Barcode) = 14 Then
'do the check digit for EAN 14 for anything 14 long
checkDigitSubtotal = (Val(Mid(Barcode, 2, 1))) _
+ (Val(Mid(Barcode, 4, 1))) _
+ (Val(Mid(Barcode, 6, 1))) _
+ (Val(Mid(Barcode, 8, 1))) _
+ (Val(Mid(Barcode, 10, 1))) _
+ (Val(Mid(Barcode, 12, 1)))
checkDigitSubtotal = (3 * checkDigitSubtotal) _
+ (Val(Mid(Barcode, 1, 1))) _
+ (Val(Mid(Barcode, 3, 1))) _
+ (Val(Mid(Barcode, 5, 1))) _
+ (Val(Mid(Barcode, 7, 1))) _
+ (Val(Mid(Barcode, 9, 1))) _
+ (Val(Mid(Barcode, 11, 1))) _
+ (Val(Mid(Barcode, 13, 1)))
If Right(Str(300 - checkDigitSubtotal), 1) <> Right(Barcode, 1) Then
Validate_UPC = "EAN14-BAD"
Exit Function
End If
Validate_UPC = "EAN14-GOOD"
Exit Function
End If
It is not working. Issue I am having is although i enter valid EAN, it gives me EAN14-BAD. I think my validating code is not working. I just added last line
+ (Val(Mid(Barcode, 13, 1)))
on EAN13 validation code. Please help.
It worked when I switched multiplying odd ones by 3 as following:
If Len(Barcode) = 14 Then
checkDigitSubtotal = (Val(Mid(Barcode, 1, 1))) _
+ (Val(Mid(Barcode, 3, 1))) _
+ (Val(Mid(Barcode, 5, 1))) _
+ (Val(Mid(Barcode, 7, 1))) _
+ (Val(Mid(Barcode, 9, 1))) _
+ (Val(Mid(Barcode, 11, 1))) _
+ (Val(Mid(Barcode, 13, 1)))
checkDigitSubtotal = (3 * checkDigitSubtotal) _
+ (Val(Mid(Barcode, 2, 1))) _
+ (Val(Mid(Barcode, 4, 1))) _
+ (Val(Mid(Barcode, 6, 1))) _
+ (Val(Mid(Barcode, 8, 1))) _
+ (Val(Mid(Barcode, 10, 1))) _
+ (Val(Mid(Barcode, 12, 1)))
If Right(Str(300 - checkDigitSubtotal), 1) <> Right(Barcode, 1) Then
Validate_UPC = "EAN14-BAD"
Exit Function
End If
Validate_UPC = "EAN14-GOOD"
Exit Function
End If
Did you try with 8 characters?
If Len(Barcode) = 8 Then
'do the check digit for EAN 8 for anything 8 long
checkDigitSubtotal = (Val(Mid(Barcode, 2, 1))) _
+ (Val(Mid(Barcode, 4, 1))) _
+ (Val(Mid(Barcode, 6, 1)))
checkDigitSubtotal = (3 * checkDigitSubtotal) _
+ (Val(Mid(Barcode, 1, 1))) _
+ (Val(Mid(Barcode, 3, 1))) _
+ (Val(Mid(Barcode, 5, 1))) _
+ (Val(Mid(Barcode, 7, 1)))
If Right(Str(300 - checkDigitSubtotal), 1) <> Right(Barcode, 1) Then
Validate_UPC = "EAN8-BAD"
Exit Function
End If
Validate_UPC = "EAN8-GOOD"
Exit Function
End If
EAN8 AND EAN13 IN VB.NET
Public Function generateEAN(ByVal barcode As String, EsEan8 As Boolean) As String
Dim first As Integer = 0
Dim second As Integer = 0
If EsEan8 Then
barcode = Right(("00000000" & barcode), 7)
Else
barcode = Right(("000000000000" & barcode), 12)
End If
If barcode.Length() = 7 OrElse barcode.Length() = 12 Then
For counter As Integer = 0 To barcode.Length() - 1 Step 2
first = (first + barcode.Substring(counter, 1))
If counter + 1 < barcode.Length Then
second = (second + barcode.Substring(counter + 1, 1))
End If
Next
If EsEan8 Then
first = first * 3
Else
second = second * 3
End If
Dim total As Integer = second + first
Dim roundedNum As Integer = (10 - (total Mod 10)) Mod 10
barcode = barcode & roundedNum
End If
Return barcode
End Function

How would one go about gathering information from one column in an SQL database(specific cell) that has double or triple values

How would one go about gathering information from one column in an SQL database(specific cell)
that has double or triple values stored in it, like:
row_number column1
1 A,B
2 B,D
*Note: the above table is not true, just an example
and place it in a column with the "AS" attribute according to a rank over to another column
after you have split column1?
The situation I have is as follows:
I am able to split and do the above with case statements, but I am only able to get value "B" after I split it and placed it in the AS Score column according to the rank over of column called ElIndex.
Code as follows:
SQLStr1 = "DECLARE #ScoreA VARCHAR(100), #ScoreB VARCHAR(100), #ScoreC VARCHAR (100), #ScoreD VARCHAR (100), #ScoreE VARCHAR (100), #ScoreF VARCHAR (100), #ScoreG VARCHAR (100) SET #ScoreA = 1 SET #ScoreB = 2 SET #ScoreC = 3 SET #ScoreD = 4 SET #ScoreE = 5 SET #ScoreF = 6 SET #ScoreG = 7"
SQLStr1 = SQLStr1 & " SELECT DISTINCT cs.ModuleName, cs.ScreenName, se.AnswerScore, cs.CourseName, tf.FileText, tf.VoiceFile, se.ElIndex"
if rsUserModules1("LinkType") = "Submit Answer Multi" then
readyList = Split(rsUserModules1("LinkAction"),",")
For i = 0 TO UBound(readyList) -1
if readyList(i) = "A" then
SQLStr1 = SQLStr1 & " ,(CASE WHEN se.LinkAction = 'A' THEN #ScoreA "
SQLStr1 = SQLStr1 & " WHEN #ScoreA = RANK()OVER(ORDER BY se.ElIndex) THEN '-1' ELSE '0' END) AS Score"
end if
if readyList(i) = "B" then
SQLStr1 = SQLStr1 & " ,(CASE WHEN se.LinkAction = 'B' THEN #ScoreB"
SQLStr1 = SQLStr1 & " WHEN #ScoreB = RANK()OVER(ORDER BY se.ElIndex) THEN '-1' ELSE '0' END) AS Score"
end if
if readyList(i) = "C" then
SQLStr1 = SQLStr1 & " ,(CASE WHEN se.LinkAction = 'C' THEN #ScoreC"
SQLStr1 = SQLStr1 & " WHEN #ScoreC = RANK()OVER(ORDER BY se.ElIndex) THEN '-1' ELSE '0' END) AS Score"
end if
if readyList(i) = "D" then
SQLStr1 = SQLStr1 & " ,(CASE WHEN se.LinkAction = 'D' THEN #ScoreD"
SQLStr1 = SQLStr1 & " WHEN #ScoreD = RANK()OVER(ORDER BY se.ElIndex) THEN '-1' ELSE '0' END) AS Score"
end if
if readyList(i) = "E" then
SQLStr1 = SQLStr1 & " ,(CASE WHEN se.LinkAction = 'E' THEN #ScoreE"
SQLStr1 = SQLStr1 & " WHEN #ScoreE = RANK()OVER(ORDER BY se.ElIndex) THEN '-1' ELSE '0' END) AS Score"
end if
next
end if
SQLStr1 = SQLStr1 & " FROM GroupScreens cs WITH (NOLOCK)"
SQLStr1 = SQLStr1 & " INNER JOIN TextFiles tf WITH (NOLOCK)"
SQLStr1 = SQLStr1 & " ON cs.ScreenName = tf.ScreenName"
SQLStr1 = SQLStr1 & " INNER JOIN Screens s WITH (NOLOCK)"
SQLStr1 = SQLStr1 & " ON s.ScreenName = tf.ScreenName"
SQLStr1 = SQLStr1 & " INNER JOIN ScreenElements se WITH (NOLOCK)"
SQLStr1 = SQLStr1 & " ON se.CourseName = cs.CourseName AND tf.SEUID = se.UID"
SQLStr1 = SQLStr1 & " WHERE tf.CourseName= '" & Name1 & "' AND cs.ModuleName= '" & Name & "' AND (s.ScreenType = 'A' AND ( [FileText] NOT LIKE '%?%' AND [FileText] NOT LIKE '%...%' AND [FileText] NOT LIKE '%True or False%' AND [FileText] NOT LIKE '%Name%' AND [FileText] NOT LIKE '%There may be%' AND [FileText] NOT LIKE '%Select%' AND [FileText] NOT LIKE '%[(]%' AND [FileText] NOT LIKE ''))AND cs.ScreenName ='" & ScreenName1 & "'"
SQLStr1 = SQLStr1 & " ORDER BY se.ElIndex"
The problem I am having is in the image below: