Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a VB randomizer when a button is clicked 1

Status
Not open for further replies.

Kris912

Programmer
Jul 29, 2005
27
US
Hi,

I'm not sure how to call a VB function from a form. I want to generate a random number when a button in a form is clicked. This is what I have so far...

The Function:

<%
Dim MyValue
Function Randomize
upperlimit = 5000000.0
lowerlimit = -300000.0
MyValue =Int((upperlimit - lowerlimit + 1)*Rnd() + lowerlimit)

end function
%>

The Call:

<input type="button" name="generate" value="Generate" onClick="Randomize">

The Output:

<input name="number" type="text" value="<% = MyValue %>" size="15">

What am I missing? I've worked more with javascript than VB and it's been a while since I've worked in VB at all.
Any help would be appreciated. Thanks!

Kris
 
I would suggest Javascipt to do this random number since ASP needs to the form to be submited to run.


<%
'if form is submited do this part
IF request.ServerVariables("REQUEST_METHOD")="POST" THEN
Dim MyValue
Function Randomize
upperlimit = 5000000.0
lowerlimit = -300000.0
MyValue =Int((upperlimit - lowerlimit + 1)*Rnd() + lowerlimit)
response.redirect("page.asp?rnd="& MyValue) 'or wherever
END IF
%>

<html>...
<form ACTION="page.asp" method="POST" name="form1">
...
...
<input name="Random" type="text" id="Random" size="20" value="<%=Request.QueryString("rnd")%>"/>
<input type="button" name="generate" value="Generate" onClick="Randomize">
</form>
...
...
</html>
 
Hi Candyman,

Thanks for the info. I think this will work although I don't want to redirect to another page using the function.

I want to run the randomizer on click and than populate the text box with that number because the user is going to have to validate that number before being allowed to submit the form. As it looks the form must be submitted for the random function to work? Could you please clarify for me. Thanks for your help!
 
Yes the form must be submited in order to work. asp runs at the server only, not on the client so it must send the clicks, info, and whatever back to the server to work. again i would suggest javascript to make the random number (since it won't require submiting the page). Something like this:

var randomnumber=Math.floor(Math.random()*11)

where 11 indicated the number will be between 0-10, so something like this:
Code:
<head>
<script language="JavaScript" type="text/javascript">
<!--
function Randomize() {
    var randomnumber=Math.floor(Math.random()*11)
    document.form1.number1.value = randomnumber
}}
//-->
</script>
</head>
<body>
...

<input type="button" name="generate" value="Generate" onClick="Randomize">
<input name="number1" type="text" size="15">
...
 
That will do the trick! Thanks Again Candyman ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top