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
Related
I know that the following is equal:
X + X'Y'Z = X + Y'Z
How can simplify the left side to arrive the right side using basic Boolean identities?
Thanks in advance.
Expression Justification
--------------------------------- -------------------------
X + X'Y'Z initial expression
(XY'Z + X(Y'Z)') + X'Y'Z r = rs + rs'
(XY'Z + XY'Z + X(Y'Z)') + X'Y'Z r = r + r
(XY'Z + X(Y'Z)' + XY'Z) + X'Y'Z r + s = s + r
(XY'Z + X(Y'Z)') + (XY'Z + X'Y'Z) (r + s) + t = r + (s + t)
X(Y'Z + (Y'Z)') + (Y'Z)(X + X') rs + rt = r(s + t)
X(1) + (Y'Z)(1) r + r' = 1
X + Y'Z r(1) = r
The fastest way to prove this expression is to add a redundant term that will discard X'
X + X'Y'Z = X(1+Y'Z) + X'Y'Z
= X + XY'Z + X'Y'Z
= X + (X+X')Y'Z
= X + Y'Z
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
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
Ok, I have a query that needs ALL of the weekending DATES to show on the output as well as doing the counts of callIDs reviewed within the week that starts Sunday and ends on Saturday?
Currently I am plugging in the startdate and enddate but this does not accumulate the weekendings in the output.
Meaning what I want to see in the output is:
Weekending 2/7/2015 count of 15 (for each supervisor)
Weekending 2/14/2015 count of 25
Weekending 2/21/2015 count of 9
SELECT DISTINCT [Coach].[LastName] & ", " & [Coach].[FirstName] AS CoachName, Count(S.CallId) AS [Count], tblLocations.LocationName, S.DateScored
FROM tblLocations INNER JOIN (tblScores AS S INNER JOIN tblEmployees AS Coach ON S.CoachId = Coach.ID) ON tblLocations.ID = S.LocationId
GROUP BY [Coach].[LastName] & ", " & [Coach].[FirstName], tblLocations.LocationName, Coach.Source, S.Completed, S.DateScored
HAVING (((tblLocations.LocationName)="Nashville" Or (tblLocations.LocationName)="Solon" Or (tblLocations.LocationName)="San Antonio" Or (tblLocations.LocationName)="Phoenix") AND ((Coach.Source)="Supervisor") AND ((S.Completed)=Yes) AND ((S.DateScored)>=[Start Date] And (S.DateScored)<=[End Date]));
You will have to use a few Access SQL functions: IIF(), Weekday(), and DateAdd(). Essentially, you have to find a date's weekday number (1-7, corresponding to Sun-Sat) and then add a corresponding value to get all dates to return Saturday dates:
SELECT [Coach].[LastName] & ", " & [Coach].[FirstName] AS CoachName,
Count(S.CallId) AS [CountOfSupervisors], tblLocations.LocationName,
IIF(Weekday(S.DateScored) = 7, S.DateScored,
IIF(Weekday(S.DateScored) = 6, DateAdd('d', 1, S.DateScored),
IIF(Weekday(S.DateScored) = 5, DateAdd('d', 2, S.DateScored),
IIF(Weekday(S.DateScored) = 4, DateAdd('d', 3, S.DateScored),
IIF(Weekday(S.DateScored) = 3, DateAdd('d', 4, S.DateScored),
IIF(Weekday(S.DateScored) = 2, DateAdd('d', 5, S.DateScored),
IIF(Weekday(S.DateScored) = 1, DateAdd('d', 6, S.DateScored), Null))))))) As EndOfWeek
FROM tblLocations INNER JOIN (tblScores AS S
INNER JOIN tblEmployees AS Coach ON S.CoachId = Coach.ID) ON tblLocations.ID = S.LocationId
WHERE (((tblLocations.LocationName IN ('Nashville','Solon','San Antonio','Phoenix') AND
((Coach.Source)='Supervisor') AND ((S.Completed)=Yes) AND
((S.DateScored)>=[Start Date] And (S.DateScored)<=[End Date]))
GROUP BY [Coach].[LastName] & ", " & [Coach].[FirstName], tblLocations.LocationName,
Coach.Source, S.Completed,
IIF(Weekday(S.DateScored) = 7, S.DateScored,
IIF(Weekday(S.DateScored) = 6, DateAdd('d', 1, S.DateScored),
IIF(Weekday(S.DateScored) = 5, DateAdd('d', 2, S.DateScored),
IIF(Weekday(S.DateScored) = 4, DateAdd('d', 3, S.DateScored),
IIF(Weekday(S.DateScored) = 3, DateAdd('d', 4, S.DateScored),
IIF(Weekday(S.DateScored) = 2, DateAdd('d', 5, S.DateScored),
IIF(Weekday(S.DateScored) = 1, DateAdd('d', 6, S.DateScored), Null)))))));
Alternatively, here is a less verbose solution as identified by #HansUp without nested IIF() statements:
SELECT [Coach].[LastName] & ", " & [Coach].[FirstName] AS CoachName,
Count(S.CallId) AS [CountOfSupervisors], tblLocations.LocationName,
DateAdd('d', IIf(S.DateScored Is Null, 0, 7 - Weekday(S.DateScored)), S.DateScored) As EndOfWeek
FROM tblLocations INNER JOIN (tblScores AS S
INNER JOIN tblEmployees AS Coach ON S.CoachId = Coach.ID) ON tblLocations.ID = S.LocationId
WHERE (((tblLocations.LocationName IN ('Nashville','Solon','San Antonio','Phoenix') AND
((Coach.Source)='Supervisor') AND ((S.Completed)=Yes) AND
((S.DateScored)>=[Start Date] And (S.DateScored)<=[End Date]))
GROUP BY [Coach].[LastName] & ", " & [Coach].[FirstName], tblLocations.LocationName,
Coach.Source, S.Completed,
DateAdd('d', IIf(S.DateScored Is Null, 0, 7 - Weekday(S.DateScored)), S.DateScored);
You will notice I adjusted some of your code. You can move all your HAVING statements into a WHERE clause (HAVING is used when aggregates are expressed - Count, Sum, Avg, First, Last). Also, you can use the IN() clause for multiple OR conditions. Hope this works!
The following code returns a NaN in situations where there are no records. How do I prevent this from being displayed in the report? A 0 would be preferred.
=FormatNumber(
((
(Code.NullSafeSplit(Fields!AvgLOSC1.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC1.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC2.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC2.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC3.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC3.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC4.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC4.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC5.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC5.Value, 1))
) / (
CInt(Code.NullSafeSplit(Fields!AvgLOSC1.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC2.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC3.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC4.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC5.Value, 1))
))
, 0)
I think the formula below should get it. I haven't tested this, so I might be missing a parenthesis. The problem is likely coming from a divide by zero when all entries are null. This catches that and sets the divisor to 1 in that case.
=FormatNumber(
(
(Code.NullSafeSplit(Fields!AvgLOSC1.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC1.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC2.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC2.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC3.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC3.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC4.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC4.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC5.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC5.Value, 1))
)
/
(
IIF((
CInt(Code.NullSafeSplit(Fields!AvgLOSC1.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC2.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC3.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC4.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC5.Value, 1))
) = 0,
1,
(
CInt(Code.NullSafeSplit(Fields!AvgLOSC1.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC2.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC3.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC4.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC5.Value, 1))
)
)
, 0)