asp classic FormatNumber bidding script - vbscript

Afternoon,
Im playing around with a little bidding script im trying to write. But im having trouble with formatNumber function.
currentBid = 50.51 'from database dataType double(16,2)
yourBid = isNumeric(Request("bid"))
If FormatNumber(yourBid,2) > FormatNumber(currentBid,2) Then
Response.Write"bid successful... woop woop"
else
Response.Write"you cant bid below the current asking price"
end if
But if i was to bid 1000 is writes "you cant bid below the current asking price"
Please advise
Regards
Shane
'Changed as advised
currentBid = 50.51 'value from database
If IsNumeric(Request.Form("bid")) Then
yourBid = CDbl(Request.Form("bid"))
end if

You have two issues here:
As Ekkehard mentioned, IsNumeric() returns a boolean. To test if the value is numeric and then store to your variable, use:
If IsNumeric(Request("bid")) Then yourBid = CDbl(Request("bid"))
FormatNumber() returns a string representation of a number. So you're comparing one string against another instead of one number against another. If you need to round your numbers to two decimals, use the Round() function instead:
If Round(yourBid,2) > Round(currentBid,2) Then
Edit: Proof.
MsgBox VarType(4) ' 2 = vbInteger
MsgBox VarType(FormatNumber(4)) ' 8 = vbString

The line
yourBid = isNumeric(Request("bid"))
does not store a valid number into yourBid, but the (booelan) result of the IsNumeric() function applied to Request("bid").
Change the line to
yourBid = CDbl(Request("bid"))
and see if your IF statement works as expected. Then add a proper validation for Request("bid").

Related

Invalid Control loop error with array

I have a fairly complex look of code where I am looking through multiple control variables.
I am getting an error 'Invalid 'for' loop control variable
the line in questions is
for w(1) = 32 to 127
I am more familiar with VBA where I would have zero problem with this statement.
I'm guessing it has something to do with the fact that i will be looping through w(1),w(2),w(3) etc. in the same tree. I initialize the variable as dim x(10) but have also tried dim w() , dim w() redim w(10)
Any thoughts? its a fairly critical aspect of the script; as such I am unwilling to swap out all my w 1,2... for individual variables
Thoughts?
EDIT:
As per comments I should clarify a Few things:
Essentially there is a alpha numeric association with an ID in a system that I am working with which I was not handed down the key too. So I have a multi-dimensional array of rates that are used for multiplying out costs.
What I am doing is working backwards through invoices and matching a material with very subtle differences that have different pricings.
For simplicity sake, say theres a 2 dimensional material where AA, AB, ... A9 are all priced through several multiplication factors in what would just be a 2x2 grid. So maintaining a pivot point based on the position in string is very important. For this code you could take tier to mean how many characters in the string (aka how complex the composition of the material):
dim x(), w()
for tier = 1 to 2
for w(1) = 32 to 127
x(1)= chr(w(1))
If tier = 2 then
for w(2)= 32 to 127
X(2)=chr(w(2))
next
end if
str = ""
for y = 1 to (tier)
str = trim(str & x(y))
next
'''msgbox str 'debug
next
end if
str = ""
for y = 1 to (tier)
str = trim(str & x(y))
next
'' msgbox str ' debug
next 'tier
This is just an excerpt i pulled to get a basic idea of the structure w/o any calculations. this is in essence what is not working
The error is quite clear, you cannot use an Array as the control variable. The definition in For...Next Statement is even clearer;
Numeric variable used as a loop counter. The variable cannot be an array element or an element of a user-defined type.
This is one of the key differences between VBA and VBScript.
You won't loop through x(1),x(2)...on what you write it's going like this 32(1),33(1)....what type it's your w(1) and how you define him?

Write an SQR condition statement

I'm new in SQR. I need help to write a variable and use it for a condition statement. my pseudo code goes
declare $head_print
let $head_print = (select * from PS_DTR_RPT_ACCT
where RPT_TREE_NODE = 'REAL_ESTATE_EXP'
or TREE_NODE_NUM between 4600000 and 4699999)
if(head_print contain REAL_ESTATE_EXP or Account between 46000000 and 4699999)
then head_print = "REAL ESTATE";
else head_print = "Capital ESTATE";
It's not quite clear what you want so I'm making an assumption.
It seems it is if a certain value is in table PS_DTR_RPT_ACCT, then you want it to say "REAL ESTATE" otherwise say "CAPITAL ESTATE"
Now with SQR, you have to put your SQL in a begin-select block - rules are very strict - field names must be in column 1 - code underneath NOT in column 1. In the following routine, I've tried to code your pseudo code in real SQR, however, I could not test it so you may get errors - plus I don't know your field names since it just says "select *".
Begin-Report
do GetData
End-Report
Begin-Procedure GetData
! Initialize value - if no data found, query this later and set it to the "ELSE"
Let $Head_print = ''
Begin-Select
Head_Print
! Override the value from the table but only if you need to
Let $Head_Print = 'REAL ESTATE'
from PS_DTR_RPT_ACCT
Where RPT_TREE_NODE = 'REAL_ESTATE_EXP'
or TREE_NODE_NUM between 4600000 and 4699999)
End-Select
! If $Head_print is blank, then no value was found with the sql - do the ELSE part
If $Head_Print = ''
Let $Head_Print = 'Capital Estate'
End-If
End-Procedure
SQR is quite a nice finite language to learn - syntax somewhat strict, but simple as Basic with SQL. I do recommend reading the reference manual - it's downloadable from Oracle.
Feel free to ask any other questions about SQR - I get alerts if you do - sorry it took this long to answer

LotusScript: how to iterate numbered fields with for loop

I'm using LotusScript to clean and export values from a form to a csv file. In the form there are multiple date fields with names like enddate_1, enddate_2, enddate_3, etc.
These date fields are Data Type: Text when empty, but Data Type: Time/Date when filled.
To get the values as string in the csv without errors, I did the following (working):
If Isdate(doc.enddate_1) Then
enddate_1 = Format(doc.enddate_1,"dd-mm-yyyy")
Else
enddate_1 = doc.enddate_1(0)
End If
But to do such a code block for each date field didnt feel right.
Tried the following, but that isnt working.
For i% = 1 To 9
If Isdate(doc.enddate_i%) Then
enddate_i% = Format(doc.enddate_i%,"dd-mm-yyyy")
Else
enddate_i% = doc.enddate_i%(0)
End If
Next
Any suggestions how to iterate numbered fields with a for loop or otherwise?
To iterate numbered fields with a for loop or otherwise?
valueArray = notesDocument.GetItemValue( itemName$ )
however do you know that there is a possibility to export documents in CSV format using Notes Menu?
File\Exort
Also there is a formula:
#Command([FileExport]; "Comma Separated Value"; "c:\document.csv")
Combined solution of Dmytro, clarification of Richard Schwartz with my block of code to a working solution. Tried it as an edit on solution of Dmytro, but was rejected.
My problem was not only to iterate the numbered fields, but also store the values in an iterative way to easily retrieve them later. This I found out today trying to implement the solution of Dmytro combined with the clarification of Richard Schwartz. Used a List to solve it completely.
The working solution for me now is:
Dim enddate$ List
For i% = 1 To 9
itemName$ = "enddate_" + CStr(i%)
If Isdate(doc.GetItemValue(itemName$)) Then
enddate$(i%) = Format(doc.GetItemValue(itemName$),"dd-mm-yyyy")
Else
enddate$(i%) = doc.GetItemValue(itemName$)(0)
End If
Next

Visual Basic Function Procedure

I need help with the following H.W. problem. I have done everything except the instructions I numbered. Please help!
A furniture manufacturer makes two types of furniture—chairs and sofas.
The cost per chair is $350, the cost per sofa is $925, and the sales tax rate is 5%.
Write a Visual Basic program to create an invoice form for an order.
After the data on the left side of the form are entered, the user can display an invoice in a list box by pressing the Process Order button.
The user can click on the Clear Order Form button to clear all text boxes and the list box, and can click on the Quit button to exit the program.
The invoice number consists of the capitalized first two letters of the customer’s last name, followed by the last four digits of the zip code.
The customer name is input with the last name first, followed by a comma, a space, and the first name. However, the name is displayed in the invoice in the proper order.
The generation of the invoice number and the reordering of the first and last names should be carried out by Function procedures.
Seeing as this is homework and you haven't provided any code to show what effort you have made on your own, I'm not going to provide any specific answers, but hopefully I will try to point you in the right direction.
Your first 2 numbered items look to be variations on the same theme... string manipulation. Assuming you have the customer's address information from the order form, you just need to write 2 separate function to take the parts of the name and address, take the data you need and return the value (which covers your 3rd item).
To get parts of the name and address to generate the invoice number, you need to think about using the Left() and Right() functions.
Something like:
Dim first as String, last as String, word as String
word = "Foo"
first = Left(word, 1)
last = Right(word, 1)
Debug.Print(first) 'prints "F"
Debug.Print(last) 'prints "o"
Once you get the parts you need, then you just need to worry about joining the parts together in the order you want. The concatenation operator for strings is &. So using the above example, it would go something like:
Dim concat as String
concat = first & last
Debug.Print(concat) 'prints "Fo"
Your final item, using a Function procedure to generate the desired values, is very easily google-able (is that even a word). The syntax is very simple, so here's a quick example of a common function that is not built into VB6:
Private Function IsOdd(value as Integer) As Boolean
If (value Mod 2) = 0 Then 'determines of value is an odd or even by checking
' if the value divided by 2 has a remainder or not
' (aka Mod operator)
IsOdd = False ' if remainder is 0, set IsOdd to False
Else
IsOdd = True ' otherwise set IsOdd to True
End If
End Function
Hopefully this gets you going in the right direction.

How to decrease a value in a text box in VB6

I am a noob to VB and I need to know how its done.
Haven't done VB in 3 years cannot remember much of it.
The textbox has a value in it (5.43), and it needs to be decreased by 0.34.
But this is the code:
TextBox3.Text = Val(TextBox3.Text) -0.34
How do I do this?
THIS IS VB 6 by the way
TextBox3.Text = CDbl(TextBox3.Text) - 0.34
Because your initial value has parenthesis (5.34) you must convert it to a specific number before operating on in.
Val does not recognize values in parens being negative. The Val() function in your original example is converting it to 0 in the same way that val("abcd") will also return 0 because it assumes both are strings.
You can test these conditions in the immediate window to quickly see the results.
Haven't tried it, but could be:
TextBox3.Text = Cdbl(TextBox3.Text) -0.34
The following code will do it:
TextBox3.Text = Cstr(CDbl(TextBox3.Text) - 0.34)
But you should be aware what is going on.
The TextBox does not store a double type, it stores a string type. The above code attempts to convert the string to a double, subtract your constant value from it, and convert it back to a string.
You should ask yourself what should happen if the string in the text box is not a valid number. In the above code, Double.Parse() will throw an exception. Double.TryParse() will return whether the conversion was successful.
Or is it impossible to enter a non-number into the text box? In which case, the safety check is unnecessary, though advisable.
You need to ask these questions when doing type conversions, or your program will behave unpredictably when a value is not convertible to the type you expected.
A safer way to decrement it would be:
Const DECREMENT_VALUE As Double = 0.34
Dim isDouble As Boolean
isDouble = IsNumeric(TextBox3.Text)
If isDouble Then
Dim newValue As Double
newValue = CDbl(TextBox3.Text)
newValue = newValue - DECREMENT_VALUE
TextBox3.Text = CStr(newValue)
Else
MsgBox "The Value was not a Double! Could not Decrement!"
End If
Try this.. It might just work
Dim TxtValue as Integer
TxtValue = TextBox3.Text
Since TxtValue is Integer, the decimal will be automatically dropped.
Like this??
TextBox3.Text=Double.Parse(TextBox3.Text)-0.43
This will work in C#
double number = Convert.ToDouble(textBox1.Text);
number = number - .34;

Resources