L
Loot
Hello, all -
I have an Access DB I am designing. I am still in development, so the
DB is empty - only has 6 records in it right now. Now, this DB is for
a trading firm and I have a very specfic requirement on how prices of
trades are displayed. So, I wrote a VBA funtion to take the actual
price from the DB and return a formatted string (function code below).
My problem is that I am calling this function from a continuous
subform so all of the displayed records will display the price
correctly. I have a text box I am using to display the price, and the
control source is set to =displayPrice(price)
This works perfectly, but there is a noticable lag - perhaps a second
- between when the form opens and the prices are displayed. This has
me nervous, since if it is lagging with 6 records, what will happen
when it is 100? Can anyone recommend a way to speed this up, or is
there something I'm missing in the below function which would cause it
to process slowly?
Thanks for any help - definitely not a VBA pro, just someone with some
experience who has been thrown into the mix by his job...
Public Function displayPrice(decimalPrice As String) As String
'This function lets us take any price and display in ticks or decimal
as appropriate
Dim priceArray As Variant
Dim priceInTicks As String
'Insert a test to see if decimalPrice comes back as a whole number,
like if a user enters
'100-32 (it will pass 101). If so, return the decimal.
If int(decimalPrice) = decimalPrice Then
displayPrice = decimalPrice
Exit Function
End If
'Otherwise, we are on our way
priceArray = Split(decimalPrice, ".")
priceArray(1) = (("." & priceArray(1)) * 32)
'After tick conversion, we want to convert ".5" to "+", but only if
the tick ends in .5
'In other words, we don't want 100-31.5253 to show as 100-31+253
'Those wacky prices are rare, but we can still handle them
'If the tick ends in .25, .5, or .75 we display it - anything else, we
exit and return the decimal. I can't see
'the need in displaying some oddball tick number like 101-03.27462 -
decimal is better in that
'scenario
If InStr(priceArray(1), ".") Then
Dim ticksArray As Variant
ticksArray = Split(priceArray(1), ".")
'Test to make sure the decimal portion is one of our accepted
values
If ticksArray(1) <> "25" And ticksArray(1) <> "5" And ticksArray
(1) <> "75" Then
displayPrice = decimalPrice
Exit Function
End If
'Adds leading zero to single digits for aesthetics
If ticksArray(0) < 10 Then
ticksArray(0) = "0" & ticksArray(0)
End If
'Test the decimal portion to see if it is equal to ".5"
If ticksArray(1) = "5" Then
ticksArray(1) = "+"
'Now, replace priceArray(1) with our result
priceArray(1) = ticksArray(0) & ticksArray(1)
Else
priceArray(1) = ticksArray(0) & "." & ticksArray(1)
End If
Else
'Adds leading zero to single digits for aesthetics
If priceArray(1) < 10 Then
priceArray(1) = "0" & priceArray(1)
End If
End If
'Form our string and return it
displayPrice = priceArray(0) & "-" & priceArray(1)
End Function
I have an Access DB I am designing. I am still in development, so the
DB is empty - only has 6 records in it right now. Now, this DB is for
a trading firm and I have a very specfic requirement on how prices of
trades are displayed. So, I wrote a VBA funtion to take the actual
price from the DB and return a formatted string (function code below).
My problem is that I am calling this function from a continuous
subform so all of the displayed records will display the price
correctly. I have a text box I am using to display the price, and the
control source is set to =displayPrice(price)
This works perfectly, but there is a noticable lag - perhaps a second
- between when the form opens and the prices are displayed. This has
me nervous, since if it is lagging with 6 records, what will happen
when it is 100? Can anyone recommend a way to speed this up, or is
there something I'm missing in the below function which would cause it
to process slowly?
Thanks for any help - definitely not a VBA pro, just someone with some
experience who has been thrown into the mix by his job...
Public Function displayPrice(decimalPrice As String) As String
'This function lets us take any price and display in ticks or decimal
as appropriate
Dim priceArray As Variant
Dim priceInTicks As String
'Insert a test to see if decimalPrice comes back as a whole number,
like if a user enters
'100-32 (it will pass 101). If so, return the decimal.
If int(decimalPrice) = decimalPrice Then
displayPrice = decimalPrice
Exit Function
End If
'Otherwise, we are on our way
priceArray = Split(decimalPrice, ".")
priceArray(1) = (("." & priceArray(1)) * 32)
'After tick conversion, we want to convert ".5" to "+", but only if
the tick ends in .5
'In other words, we don't want 100-31.5253 to show as 100-31+253
'Those wacky prices are rare, but we can still handle them
'If the tick ends in .25, .5, or .75 we display it - anything else, we
exit and return the decimal. I can't see
'the need in displaying some oddball tick number like 101-03.27462 -
decimal is better in that
'scenario
If InStr(priceArray(1), ".") Then
Dim ticksArray As Variant
ticksArray = Split(priceArray(1), ".")
'Test to make sure the decimal portion is one of our accepted
values
If ticksArray(1) <> "25" And ticksArray(1) <> "5" And ticksArray
(1) <> "75" Then
displayPrice = decimalPrice
Exit Function
End If
'Adds leading zero to single digits for aesthetics
If ticksArray(0) < 10 Then
ticksArray(0) = "0" & ticksArray(0)
End If
'Test the decimal portion to see if it is equal to ".5"
If ticksArray(1) = "5" Then
ticksArray(1) = "+"
'Now, replace priceArray(1) with our result
priceArray(1) = ticksArray(0) & ticksArray(1)
Else
priceArray(1) = ticksArray(0) & "." & ticksArray(1)
End If
Else
'Adds leading zero to single digits for aesthetics
If priceArray(1) < 10 Then
priceArray(1) = "0" & priceArray(1)
End If
End If
'Form our string and return it
displayPrice = priceArray(0) & "-" & priceArray(1)
End Function