Access VBA Dmin and Dmax on comma seperated decimals - function

My system is using , as decimal seperator and . as thousands.
When i use the Dmin and Dmax functions in VBA, i get the error:
run-time error 3075 syntax error (comma)
While running this:-
UpperPower = DMin("Column2", "t_table", "Column2" & ">=" & RatedPower)
Where RatedPower is a function variable declared Public RatedPower As Double.
The function is declared double as well.
Everything else works fine with the comma being the decimal seperator.
I have looked into the replace function but I am not sure how to use it in the Dmin function...
What can i do?
Best Regards, Emil.

This will work:
UpperPower = DMin("Column2", "t_table", "Column2 >= " & Replace(RatedPower, ",", "."))
Since the first argument to Replace() must be a string, RatedPower is implicitly converted to a string using the local decimal separator (if non-integer) and no thousands separators.
This code is foolproof, in that it works regardless if the local decimal separator is "." or ","

Related

Why am I getting type mismatch from label in access form?

I am getting values from labels that are formatted as Percent (0.00%).
Therefore, I cast it to a double CDBL(label.caption) and I get the type-mismatch error.... see my code:
Forms(frmName).Label80.Caption = format(CDbl(Forms(frmName).Label123.Caption) + CDbl(Forms(frmName).Label162.Caption), "Percent")
Originally:
label123 has 10.00% value and label162 has 0.00% value
so if I do cdbl(label123) it gives me 10 (good!)
if I do cdbl(label162) it produces an error
if I do val(label162) it produces an error
I'm thinking it has something to do with 0?? I can't seem to figure this out...
You can also use the Format function to parse the percentage and return a string containing the equivalent value in General Number format, so that it may be successfully converted using the CDbl function, e.g.:
Forms(frmName).Label80.Caption = Format(CDbl(Format(Forms(frmName).Label123.Caption, "General Number")) + CDbl(Format(Forms(frmName).Label162.Caption, "General Number")), "Percent")
The advantage of this method is that the Format function recognises that the string represents a percentage and therefore automatically handles the division by 100:
?Format("10.48%", "General Number")
0.1048
The use of CDbl also allows for regional differences in decimal number representation (e.g. the use of a comma in place of a period) - my thanks to #Gustav for pointing this out.
Try removing the % signs:
Forms(frmName).Label80.Caption = Format(Val(Replace(Forms(frmName).Label123.Caption, "%", "")) + Val(Replace(Forms(frmName).Label162.Caption, "%", "")), "Percent")
Edit:
Using Val, only works having dot (.) as the decimal separator.
For a universal solution, use CDbl or CCur as these convert the localised format correctly where a value formatted as percent may display as 10,48%.
Forms(frmName).Label80.Caption = Format(CDbl(Replace(Forms(frmName).Label123.Caption, "%", "")) + CDbl(Replace(Forms(frmName).Label162.Caption, "%", "")), "Percent")
Use the following template for each label. The evaluate takes care of the percent sign.
L0 = CDbl(Evaluate(Label0.Caption))

How to calculated decimal values in vba access?

I am trying to update an inventory stock as follows:
[InventoryStock] = ([CurrentInventoryStock]-[QuantityOrdered])
Note that QuantityOrdered can be decimal, something like: 5.2 or can be full number, something like 8.
The InventoryStock column is set to Number (double)
The QuantityOrdered column is also set to Number (double)
When QuantityOrdered is a full number, like 5, it works perfectly; but when QuantityOrdered is a decimal number, like 7.5 , then I receive:
Run-time error 3144 (syntax error in update statement)
...highlighting the update code that I wrote in VBA.
If Not IsNull(Me.QuantityOrdered) Then
CurrentDb.Execute " UPDATE Inventory SET InventoryStock = InventoryStock - " & Nz(Me.QuantityOrdered.Value, 0) & ""
End If
As described above, my goal is to deducted the quantity ordered (whether decimal or full number) from the Inventory Stock value.
Can someone help me out?
Regarding your problem:
Using implicit string conversion will use a decimal separator following your country/language settings, for example a (,), which causes your issue.
So you should explicitely use Str() to convert the numeric value to a string to get a dot (.) as separator.
Two more small remarks:
NZ(…) is not necessary because you already check that before (If Not IsNull(Me.QuantityOrdered) Then).
Appending just an empty string (& "") is not necessary too.
If Not IsNull(Me.QuantityOrdered) Then
CurrentDb.Execute "UPDATE Inventory SET InventoryStock = InventoryStock - " & Str(Me.QuantityOrdered.Value)
End If

Getting run-time error 2465 when using Instr function

I am trying to pull the date out of a string contained in a variable.
Variable is RWPSheetvalues(96)
RWPSheetvalues(96) contains the string "AIT_13_11_11_Metro_Sky Park 13_UR"
The date is 11/11/2013
So, I thought I would use the Instr function to remove the first few characters up to beginning of date (this could be between 3 & 5 characters - depending on string). So the line of code is as follows:
CharPosition = InStr(1, {RWPSheetvalues(96)],"-",1)
This gives ma a run-time error of 2465.
Any ideas why the error?
You can use this fancy expression:
ThisDate = CDate("20" & Replace(Left(Split(RWPSheetvalues(96), "_", 2)(1), 8), "_", "/"))
It strips the first part, select the next eight chars (the date), replaces _ with /, prefix with 20 for the century, and finally converts to a date value.
You could use the Split function with an underscore delimiter, and then supply the appropriate elements to the DateSerial function.

Formatting the Field in Dsum

In my project I am using dsum to query a table to compare years. But I want to render the field as a year for the comparison.
Public Function GetValue(whatyear) As Long
GetValue = DSum("Modification", "Accounting Totals", "Format([EntryDate],'yyyy') = " & whatyear & " AND [ModType] like *2*")
End Function
I keep getting this error:
Syntax error (missing opeator in query expression
'Format([EntryDate],'yyyy' = 2016 AND [ModType] like *2*"
This is probably an easy one for you VBA Gurus. What do I do?
you need qoutes for the year, and if [ModType] is text, you need qoutes for it as well. in addition, handle null values like this, else if it doesn't find any rows, that will throw another error:
Nz(DSum("Modification", "Accounting Totals", "Format([EntryDate],'yyyy') = '" & whatyear & "' AND [ModType] like '*2*' "), 0)
if [ModType] is a numeric value, then the like operator is not going to work, you need to use another operator such as these: =, >=, <=, BETWEEN
Got it - I had to remove the As Long from the function declaration
If so, you may have zero records and DSum returns Null. Catch that - as O. Gungor showed - with Nz. And get the year as a number:
So:
Public Function GetValue(ByVal whatyear As Integer) As Currency
GetValue = Nz(DSum("Modification", "Accounting Totals", "Year([EntryDate]) = " & whatyear & " AND [ModType] Like '*2*'"), 0)
End Function

Removing non-alphanumeric characters in an Access Field

I need to remove hyphens from a string in a large number of access fields. What's the best way to go about doing this?
Currently, the entries are follow this general format:
2010-54-1
2010-56-1
etc.
I'm trying to run append queries off of this field, but I'm always getting validation errors causing the query to fail. I think the cause of this failure is the hypens in the entries, which is why I need to remove them.
I've googled, and I see that there are a number of formatting guides using vbscript, but I'm not sure how I can integrate vb into Access. It's new to me :)
Thanks in advance,
Jacques
EDIT:
So, Ive run a test case with some values that are simply text. They don't work either, the issue isn't the hyphens.
I'm not sure that the hyphens are actually the problem without seeing sample data / query but if all you need to do is get rid of them, the Replace function should be sufficient (you can use this in the query)
example: http://www.techonthenet.com/access/functions/string/replace.php
If you need to do some more advanced string manipulation than this (or multiple calls to replace) you might want to create a VBA function you can call from your query, like this:
http://www.pcreview.co.uk/forums/thread-2596934.php
To do this you'd just need to add a module to your access project, and add the function there to be able to use it in your query.
I have a function I use when removing everything except Alphanumeric characters. Simply create a query and use the function in the query on whatever field you are trying to modify. Runs much faster than find and replace.
Public Function AlphaNumeric(inputStr As String)
Dim ascVal As Integer, originalStr As String, newStr As String, counter As Integer, trimStr As String
On Error GoTo Err_Stuff
' send to error message handler
If inputStr = "" Then Exit Function
' if nothing there quit
trimStr = Trim(inputStr)
' trim out spaces
newStr = ""
' initiate string to return
For counter = 1 To Len(trimStr)
' iterate over length of string
ascVal = Asc(Mid$(trimStr, counter, 1))
' find ascii vale of string
Select Case ascVal
Case 48 To 57, 65 To 90, 97 To 122
' if value in case then acceptable to keep
newStr = newStr & Chr(ascVal)
' add new value to existing new string
End Select
Next counter
' move to next character
AlphaNumeric = newStr
' return new completed string
Exit Function
Err_Stuff:
' handler for errors
MsgBox Err.Number & " " & Err.Description
End Function
Just noticed the link to the code, looks similar to mine. Guess this is just another option.