I'm writing a variable dimensioned as Decimal to an Access DB. The value calculated is 1600.91. The field in Access is set to: Field Size - Decimal, Format - Fixed, Precision - 12, Scale - 2, Decimal Places - 2
But the number that ends up in the table is 1600.90. Even if I hard-code the variable to 1600.91, Access shows 1600.90. Other rows in that column have both decimals showing correctly.
The setup for the Access connection is as follows:
Public con As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\SPJ_CRVM.accdb")
Public daAnswer As New OleDbDataAdapter("SELECT * FROM [answer]", con)
Public cbAnswer = New OleDbCommandBuilder(daAnswer)
Public dtAnswer As New DataTable
Public drAnswer As DataRow
cbAnswer.quoteprefix = "["
cbAnswer.quotesuffix = "]"
daAnswer.Fill(dtAnswer)
drAnswer = dtAnswer.NewRow()
The code is as follows:
Dim ldec_gmp as Decimal
ldec_gmp = Math.Round(NUM / PVP * 12 / M, 4, MidpointRounding.AwayFromZero)
drAnswer("guar_maturity_premium") = ldec_gmp
And writing to Access is done here:
dtAnswer.Rows.Add(drAnswer)
daAnswer.Update(dtAnswer)
Update:
NUM & PVP are both dimensioned as Decimal; M is dimensioned at Integer
In the example row:
NUM = 41506.813854184606481174528411
PVP = 25.927068677092090950619294386
M = 12
ldec_gmp = 1600.9065 with MidpointRounding.AwayFromZero set to 4
ldec_gmp = 1600.91 with MidpointRounding.AwayFromZero set to 2
What am I doing wrong?
Related
I'm trying to do a multilevel factor analysis using the "psych" package. The first step is recommended to use the statsBy() funtion to have a correlation data:
statsBy(study2, group = "ID")
However, it gives this "Error in FUN(data[x, , drop = FALSE], ...) : 'x' must be numeric".
For the dataset, I only included a grouping variable "ID", and other two numeric variables. I ran the following line to check if the varibales are numeric.
sapply(study2, is.numeric)
ID v1 V2
FALSE TRUE TRUE
Here are the code in the tracedown of the error.But I don't know what 'x' refers here, and I noticed in line 8 and 9, the X is in captital and is lowercase in line 10.
*10.
FUN(data[x, , drop = FALSE], ...)
9.
FUN(X[[i]], ...)
8.
lapply(X = ans[index], FUN = FUN, ...)
7.
tapply(seq_len(728L), list(z = c("5edfa35e60122c277654d35b", "5ed69fbc0a53140e516ad4ed", "5d52e8160ebbe900196e252e", "5efa3da57a38f213146c7352", "5ef98f3df4d541726b1bcc48", "5debb7511e806c2a59cad664", "5c28a4530091e40001ca4d00", "5872a0d958ca4c00018ce4fe", "5c87868eddda2d00012add18", "5e80b7427567f07891655e7e", ...
6.
eval(substitute(tapply(seq_len(nd), IND, FUNx, simplify = simplify)), data)
5.
eval(substitute(tapply(seq_len(nd), IND, FUNx, simplify = simplify)), data)
4.
structure(eval(substitute(tapply(seq_len(nd), IND, FUNx, simplify = simplify)), data), call = match.call(), class = "by")
3.
by.data.frame(data, z, colMeans, na.rm = na.rm)
2.
by(data, z, colMeans, na.rm = na.rm)
1.
statsBy(study2, group = "ID")*
The dataset has 728 rows and those like "5edfa35e60122c277654d35b" are IDs. Could anyone help explain what might have gone wrong?
I had the same error, the only way was to convert the group variable to the numeric class.
Try:
study2$ID<-as.numeric(study2$ID)
statsBy(study2, group = "ID")
If dat$ID is of class character:
study2$ID<-as.numeric(as.factor(study2$ID))
statsBy(study2, group = "ID")
I´m trying to find a way to filter my (Semana) column by its MAX value, every week new data is added to my table and I just need to visualize the last week data.
I´ve tried Table.Max and List.Max but I can´t solve the problem
let
Origen = Folder.Files("D:\DOCUMENTS\Cartera"),
#"Personalizada agregada4" = Table.AddColumn(#"Tipo cambiado2", "MetaMora", each if [Días Atraso] <= 14 then 0 else [#"MORA CAP + INTS"]),
#"Filas filtradas1" = Table.SelectRows(#"Personalizada agregada4", each ([Semana] = 30)) *** I change this value manually according to the max value in the table ***
in
#"Filas filtradas1"
I need that the code automatically update the Max value from the [Semana] column.
Like this:
let
Origen = Folder.Files("D:\DOCUMENTS\Cartera"),
#"Personalizada agregada4" = Table.AddColumn(#"Tipo cambiado2", "MetaMora", each if [Días Atraso] <= 14 then 0 else [#"MORA CAP + INTS"]),
max = List.Max(#"Personalizada agregada4"[Semana]),
#"Filas filtradas1" = Table.SelectRows(#"Personalizada agregada4", each [Semana] = max)
in
#"Filas filtradas1"
I have a Visual Basic 6 form with a MSFlexGrid control inside, which takes data from a record set(ADODB) and displays them.
Before starting the copy of data to the FlexGrid, I'm trying to set the rows count, depending on records count. Also I have a collection which contains columns' names, then I can get the number of columns from here.
The following is a code snippet:
v_colsCount = UBound(aCols) + 2 // aCols = array with columns' names
v_regCount = rs.RecordCount // rs = my ADODB record set
myFlexGrid.Rows = 0 // for cleaning rows from a previous display
myFlexGrid.Rows = IIf(v_regCount > 0, v_regCount + 1, 2)
myFlexGrid.Cols = v_colsCount
myFlexGrid.FixedRows = 1
myFlexGrid.FixedCols = 0
There are 7532 rows and 52 columns. The problem comes when I run the application and try to execute this part of the code (fill the FlexGrid with data from the record set):
For iRow = 1 To v_regCount
For iCol = 0 To v_colsCount -2
sAux = ConvStr(rs.Fields(aCols(iCol)).Value)
myFlexGrid.TextMatrix(iRow, iCol) = sAux
I notice that
v_regCount = 7532 but v_colsCount = 2 ,
and I get an error ("Substring out of range"). If I swap the settings order (i.e. if I set myFlexGrid.Cols after set myFlexGrid.Rows), then
v_regCount = 0 and v_colsCount = 52
I don't understand why I can't set rows and columns count at the same time.
Any ideas?
Thanks in advance
So I have 13 binary values, which I call b_1... b_13, and based off these values I'd like to either set something I call indic_j to a previously defined string called inf_j, or nothing at all. Is it possible to do this without using 13 "If..." statements? What I have tried is below:
inf_1 = "aaaaa"
inf_2 = "bbbbb"
... and so on defining 13 infs, where aaaaa, bbbbb etc are names of columns in a table that I want to select.
FOR j = 1 to 13
IF b_j = 1 THEN "indic_"+j = inf_j + ",";
ELSE "indic_"+j = ""
ENDIF
ENDFOR
Also, before this I haven't introduced anything called indic_1, indic_2, etc. Is this needed?
My end goal is to transfer selected columns over to Excel. I've no problems doing this with predetermined columns, but I'm not sure how to allow for selected columns only.
I've tried using 13 IF statements, but I'm getting operator/operand type mismatch errors. My code currently is
IIF(b_1 = 1, indic_1 = inf_1 + ",",indic_1 = "")
IIF(b_2 = 1, indic_1 = inf_2 + ",",indic_1 = "")
IIF(b_3 = 1, indic_1 = inf_3 + ",",indic_1 = "")
and so on for 13 times, and then
SELECTIONRANG = indic_1 + indic_2 + indic_3 + indic_4 + indic_5 + indic_6 +indic_7 + indic_8 + indic_9 + indic_10 + indic_11 + indic_12 + indic_13
SELECTIONRANGE = LEFT(SELECTIONRANG,LEN(Selectionrang)-1)
You could create te variable name as a string and use it with &
As:
ind = 13
Var = "inf_" + ind
&Var ** inf_13
I've created a function called EnergyPrice with the following code:
Here's the pricetable I'm looking up from
Prijstabel
fixed variable
Startdate Einddate gas electra gas electra
€/a €/a ct/KWh ct/KWh
1-1-2010 1-7-2010 181,00 235,00 0,11 0,33
1-7-2010 1-1-2011 362,00 470,00 0,33 1,30
1-1-2011 1-7-2011 191,00 245,00 0,22 0,65
1-7-2011 1-1-2012 162,35 208,25 0,19 0,55
1-1-2012 1-7-2012 324,70 416,50 0,37 1,11
And here's the relevant code
Public Enum Energietype
v_gas = 1
v_electricity = 2
End Enum
Public Enum FixedOrVariable
v_fixed = 1
v_variable = 2
End Enum
Public Function EnergyPrice(PriceDate As Date, E_type As Energietype, variabel As FixedOrVariable) As Variant
Dim PrijsTable As Range
Dim RowNr As Integer
Dim Found As Boolean
Dim KolomNr As Integer
Set PrijsTable = Range("EnergyPriceTable")
If PrijsTable.Columns.Count <> 6 Then Err.Raise Number:=vbObjectError + 1000, Description:="No valid valid pricetable defined"
RowNr = 1
Found = False
While Not (Found) And (RowNr <= PriceTable.Rows.Count)
Found = (PriceTable.Cells(RowNr, 1).Value <= PriceDate) And (PriceTable.Cells(RowNr, 2) > PriceDate)
If Not (Found) Then RowNr = RowNr + 1
Wend
If Found Then
If E_type = v_gas Then KolomNr = 1
If E_type = v_elektra Then KolomNr = 2
If variabel = v_variabel Then KolomNr = KolomNr * 2
KolomNr = KolomNr + 2
EnergyPrice = PriceTable.Cells(RowNr, KolomNr).Value
Else
EnergyPrice = Empty
End If
End Function
The question is how do I use the above enums in an Excel spreadsheet? So I can enter a formula like:
If I use the numbers 1,2 the functions works fine, but I want to use the enum names.
Can this be done using only Excel VBA?
If you add the enums to the workbook as defined names, you can pass them into functions and the value that is passed is the actual value you set the enum for. Do this manually or via VBA if you prefer.
Example:
ActiveWorkbook.Names.Add Name:="v_gas", RefersToR1C1:="=1"
ActiveWorkbook.Names.Add Name:="v_fixed", RefersToR1C1:="=2"
The only way I see, is by defining Names in your workbook, assigning them the constants of your Enums.
From the menu: Insert ... Name .... Define
Name: v_gas
Refers to: =1
Alternatively, you could create those names by VBA, but that has no interest, since it will be a 1 shot (names are saved with the workbook).
By using such names, the users will be able to use F3 while entering the formula.