Why does this VBS code fail with a "Type mismatch: 'CInt'" error? - vbscript

I am experiencing difficulty with the following VBS code. It works only sometimes, and even then it fails quickly. Why?
Dim Butt
Set Butt = CreateObject("InternetExplorer.application")
Butt.visible = True
Butt2 = InputBox("Put the link to one hat you would like to snipe.", "Hat Selection")
Butt3 = InputBox("Enter the maximum amount of Robux you will spend on this hat.", "Maximum Payment")
Dim Proace
Set Proace = CreateObject("Microsoft.XMLHTTP")
Proace.Open "GET", "http://www.roblox.com", False
Proace.Send
Do
Do While Butt.Busy
WScript.sleep 200
Loop
St00f = CInt(Replace(Mid(St00f, (InStr(St00f, ">R$")+3), 8), "</b>", ""))
If St00f <= CInt(Butt3) Then
Butt.Navigate "javascript:WebForm_DoPostBackWithOptions(new%20WebForm_PostBackOptions(""ctl00$cphRoblox$TabbedInfo$UserSalesTab$lstItemsForResale$ctrl0$lnkBuyNow"",%20"""",%20true,%20"""",%20"""",%20false,%20true))"
Exit Do
End If
Loop
Do While Butt.Busy
WScript.sleep 200
Loop
MsgBox("Congratulations! Your snipe was successful! You sniped "&Butt2&" for "&Butt3&" Robux!")
Butt.Quit
Set Butt = Nothing
Set Proace = Nothing
WScript.Quit
Error:
Script: C:\Users\John\Downloads\SingleHatSniper.vbs
Line: 14
Char: 1
Error: Type mismatch: 'CInt'
Code: 800A000D
Source: Microsoft VBScript runtime error
Please help me, I'm not that great with VBS. That much is clear, my friend helped me write this.

As you might have known by now, this is where the error occurs
St00f = CInt(Replace(Mid(St00f, (InStr(St00f, ">R$")+3), 8), "</b>", ""))
And that line does these things
InStr that returns the numeric position of the first occurrence of ">R$"
Its then added with 3 to get the index after the string"R$"
Now Mid splits the string St00f with start index after "R$" to a length of 8
Then Replace takes the split string and replaces an occurrence of "</b>" with ""
At last CInt converts the string to an integer or more correctly * converts any number to the variant of subtype Integer*
And you are getting the error at the CInt conversion.
If I were at your place, I will split this line by line by keeping only one function per line and then try something like MsgBox for the output after each line and find what's wrong with that.
The key is the variable St00f and what that variable holds.
Happy Coding :)

The "Type mismatch" error indicates that your Replace(...) did not return a valid numerical string:
>> i = CInt("4711")
>>
>> i = CInt("999999999999")
>>
Error Number: 6
Error Description: Overflow
>> i = CInt("no number")
>>
Error Number: 13
Error Description: Type mismatch
>> i = CInt("")
>>
Error Number: 13
Error Description: Type mismatch
Consider using IsNumeric() before you apply CInt().

CInt can handle betweeen -32,768 and 32,767
Use CLng instead of CInt
Copied from Cint overflow error when value exceeds 100,000+

Related

Valid response causes "Subcript out of range"

I've got a classic ASP application that contacts a database and receives a valid response but then crashes with
Error Number 9: Subscript out of range
after exiting the IF block the db call is made in. What's odd is that the same code is currently working in production. As far as I can tell they're configured identically (but I suspect there's a subtle difference that's causing this issue) and have identical code bases.
What I want to know is:
Where is this array that I'm supposedly attempting to reach a non-existent index of? I don't see it and the error gives no line number. Is there a chance something is not working correctly in the adodb library?
Perhaps this is a common problem having to do with a certain patch and my particular db connection library? Have you had a similar experience?
How do I troubleshoot a problem that doesn't immediately present itself? Should I just start putting troubleshooting statements in the library?
Explanation of what's happening in the code: When the cookie "click" is received err.number is 0. When the cookie "bang" is received the err.number is 9. It then crashes with that error at the end of the IF block.
<%#Language="VBSCRIPT"%>
<% Server.ScriptTimeout = 150 %>
<%
On Error resume Next
%>
<!--#include file="adovbs.inc"-->
<!--#INCLUDE FILE="DBConn.asp"-->
<!--#INCLUDE FILE="ErrorHandler.asp"-->
<%
'Application Timeout Warning
sessionTimeout = 20
advanceWarning = 5
jsTimeout = (sessionTimeout - advanceWarning) * 60000
'If the users session has expired
If Session("USERNAME") = "" or Session("USERNAME") = NULL Then
Response.Redirect("default.asp")
End If
'If the user has just changed their password. Prompt them that it was successfully changed
If Request("changePasswd") = "true" Then
Response.Write("<script language='Javascript'>alert('Your Password Has been Successfully Changed!');</script>")
End If
Dim connection, cmd, objRS, latestDate, lastDateJPMC, firstDateJPMC, lastDateWACH, firstDateWACH, lastDateWFB, firstDateWFB, accountCount
Function calConvertDate(theDate)
Dim yr, mn, dy, dtSplit
dtSplit = Split(theDate,"/")
yr = dtSplit(2)
mn = dtSplit(0)
dy = dtSplit(1)
if Len(mn) = 1 then mn = "0" & mn
if Len(dy) = 1 then dy = "0" & dy
calConvertDate = "[" & yr & "," & mn & "]"
End Function
set connection = Server.CreateObject("adodb.connection")
connection.Open DBConn
connection.CommandTimeout = 60
set connection = Server.CreateObject("adodb.connection")
connection.Open DBConn
connection.CommandTimeout = 60
'Get Earliest & Latest Date in Database
If Err.Number = 0 Then
Response.Cookies("CLICK")=Err.number
Set cmd = Server.CreateObject("ADODB.Command")
With cmd
Set .ActiveConnection = connection
.CommandText = "CIRS_Admin.spGetLatestDate"
.CommandType = adCmdStoredProc
set objRS = .Execute
End With
latestDate = calConvertDate(objRS("latestDate"))
Response.Cookies("latestdate")=objRS("latestDate")
objRS.Close
Set objRS = Nothing
Response.Cookies("BANG")=Err.number
End If
To debug, please add a statement like
Response.Write (objRS("latestDate"))
before the line
latestDate = calConvertDate(objRS("latestDate"))
so you can see if (for example) the date returned from the server has "-" as separator instead of "/" or if an empty value is returned.
After understanding what is causing the problem you can solve it
1.Where is this array that I'm supposedly attempting to reach a non-existent index of? I don't see it and the error gives no line number. Is there a chance something is not working correctly in the adodb library?
This is your array:
yr = dtSplit(2)
mn = dtSplit(0)
dy = dtSplit(1)
What's odd is that the same code is currently working in production. As far as I can tell they're configured identically (but I suspect there's a subtle difference that's causing this issue) and have identical code bases.
May be you have different regional settings?
I strongly suggest to you use better error handling.
Internal Server Error 500 w/ IIS Log

Issue running Classic ASP written in VBScript on Windows 2012 with IIS 8

This is a very peculiar issue. I have a 2012 Server running IIS 8 with support for classic ASP installed. I am building a comma separated string from a form. I then am retrieving this string from a table and want to split on the commas.
First, when I build the string and submit it to the DB (SQL Express 2014), something is adding a space after each comma even though there is no space in the code.
Second, when I return the string and attempt to split on the comma, it doesn't do anything; the ubound method returns -1... For testing purposes, I hand built an array and this has the same behavior.
Code that builds the csv string:
If fieldName = "txt_EnvironmentType" then
strTempEnvCSV = strTempEnvCSV & fieldValue & ","
End If
Test code for split:
txtEnvironmentType = "This,Is,A,Test,String"
If txtEnvironmentType <> "" then
response.write(txtEnvironmentType)
array = split(txtEnvironmentType,",")
l = ubound(array)
response.write("<br>array is " & l & " long")
For i = LBound(array) to UBound(array)
response.write("<br>" & array(i))
Next
End If
The Above test code returns the following to the browser:
This,Is,A,Test,String
array is -1 long
Am I missing something?
Thanks!
For the mysterious spaces, add a TRIM() to make sure you aren't starting with spaces:
If fieldName = "txt_EnvironmentType" then
strTempEnvCSV = strTempEnvCSV & TRIM(fieldValue) & ","
End If
This ran (for your second issue) for me - the only change I made was to dim the array variable and name it something other than "array"
<%
dim arrMine
txtEnvironmentType = "This,Is,A,Test,String"
If txtEnvironmentType <> "" then
response.write(txtEnvironmentType)
arrMine = split(txtEnvironmentType,",")
l = ubound(arrMine)
response.write("<br>arrMine is " & l & " long")
For i = LBound(arrMine) to UBound(arrMine)
response.write("<br>" & arrMine(i))
Next
End If
%>

VBScript calculations - VBscript error - when a data field is NULL or 0

This is the piece of code im having an issue with.
<%=total+((rs("fee_overall")/100)*rs("fee_wip")-rs("fee_rendered"))%>
If any of the data fields are either 0 or NULL then i get a mismatch error
"Microsoft VBScript runtime error '800a000d' Type mismatch: '[string: ""]' "
Any help is apreciated.
Thanks
in this case Zero seems to be ok because you have no division by zero. for null variables the standard solution would be that you check if variables are not null , then do calculations:
if not(isnull(rs("feeoveral")) or isnull( rs("fee_wip")) or isnull(rs("fee_rendered")) ) then
response.write total+((rs("fee_overall")/100)*rs("fee_wip")-rs("fee_rendered"))
else
response.write "bolb"
end if
Try it like this.
Dim fee_over
If (IsNull(rs("fee_overall"))) Then
fee_over = "0"
Else
fee_over = (rs("fee_overall"))
End If
response.write total+((Cint(fee_over))/100)
Also please check that your database record is INT and default 0

VB6: Run-time error '6': Overflow

I'm studying computing at AS Level in England, and the language we are using is VB6.
I am working on out assignment which has to be completed for 18/12/2014.
The project is for a hypothetical situation where a running club needs software to do the following:
- Add Members
- View Members
- Edit Member Information
- Search for Members
- Delete Members
- Add Training Information for Members
- View Training Information
- Compare Training Information
- Automatically select a team of runners based upon the number of hours trained for
Here's my code for the problem form:
Option Explicit
Private Sub CmdExitFromSelectTeam_Click()
Unload Me
End Sub
Private Sub SelectTeam()
Dim TrainingChannel As Integer
Dim Training As TrainingRecord
Dim MemberChannel As Integer
Dim Member As MemberRecord
Dim MemberRecordPointer As Integer
Dim TotalHoursTrained As Single
Dim TrainingRecordPointer As Integer
Dim FoundAtLeastOneRecord
FoundAtLeastOneRecord = False
MemberChannel = FreeFile
Open MemberFile For Random As MemberChannel Len = MemberLength
MemberRecordPointer = 1
Get MemberChannel, MemberRecordPointer, Member
Do While Not EOF(MemberChannel)
TotalHoursTrained = 0
TrainingRecordPointer = 1
TrainingChannel = FreeFile
Open TrainingFile For Random As TrainingChannel Len = TrainingLength
Get TrainingChannel, MemberRecordPointer, Training
Do While Not EOF(TrainingChannel)
If Member.ID = Training.MemberID Then
TotalHoursTrained = Round(TotalHoursTrained, 1) + Round(Training.TimeTaken, 1)
End If
TrainingRecordPointer = TrainingRecordPointer + 1 (*)
Get TrainingChannel, MemberRecordPointer, Training
Loop
Close TrainingChannel
LstTeamSelectDisplayTeam.AddItem TotalHoursTrained, 1
LstTeamSelectDisplayTeam.AddItem Member.ID, 2
LstTeamSelectDisplayTeam.AddItem Member.Forename, 3
LstTeamSelectDisplayTeam.AddItem Member.Surname, 4
MemberRecordPointer = MemberRecordPointer + 1
Get MemberChannel, MemberRecordPointer, Member
Loop
Close MemberChannel
End Sub
Private Sub Form_Load()
SelectTeam
End Sub
When this form (FrmSelectTeam.frm) is loaded at run time, the line: marked with (*) is highlighted as the debug line.
I have no idea what the problem is, and I'd appreciate all the help I can get, so thanks in advance!!!
James
In VB6, the maximum value for the Integer data type is 32767. You are apparently exceeding that limit in the (*) statement. You can change it to a 32-bit integer by declaring it long:
Dim TrainingRecordPointer As Long
#xpda's answer is almost certainly correct.
One handy debugging trick for an error like this, is to modify your code slightly, as below:
Open MemberFile For Random As MemberChannel Len = MemberLength
MemberRecordPointer = 1
Get MemberChannel, MemberRecordPointer, Member
Do While Not EOF(MemberChannel)
TotalHoursTrained = 0
TrainingRecordPointer = 1
TrainingChannel = FreeFile
Open TrainingFile For Random As TrainingChannel Len = TrainingLength
Get TrainingChannel, MemberRecordPointer, Training
Do While Not EOF(TrainingChannel)
If Member.ID = Training.MemberID Then
TotalHoursTrained = Round(TotalHoursTrained, 1) + Round(Training.TimeTaken, 1)
End If
If TrainingRecordPointer > 32750 Then
Debug.Print TrainingRecordPointer
End If
TrainingRecordPointer = TrainingRecordPointer + 1
Get TrainingChannel, MemberRecordPointer, Training
Loop
Close TrainingChannel
LstTeamSelectDisplayTeam.AddItem TotalHoursTrained, 1
LstTeamSelectDisplayTeam.AddItem Member.ID, 2
LstTeamSelectDisplayTeam.AddItem Member.Forename, 3
LstTeamSelectDisplayTeam.AddItem Member.Surname, 4
MemberRecordPointer = MemberRecordPointer + 1
Get MemberChannel, MemberRecordPointer, Member
Loop
Close MemberChannel
Alternatively, you can put a breakpoint in the added If-Then, and step through using the debugger.
Well thankyou for your feedback, but what was actually the cause (Believe it or not) was simple human error; I put "Get TrainingChannel, MemberRecordPointer, Training" instead of "Get TrainingChannel, TrainingRecordPointer, Training"
It's so annoying that something as simple as that could cause such a big problem.
But once again, thanks!!

Convert string to formula

Hope you can help with af old classic ASP problem.
In my database i got a TEXT string with this value: (X*5)/25
strCalString = (X*5)/25
strX = 100
strOutput = replace(strCalString,"X",strX)
Then is my output: (100*5)/25
But i want to have my output to be: 20, and not my formula string,
How can i do this, to get it to work.
i have tried to convert my strOutput to cint(strOutput), but thats not working.
And search on google, i could find anything.
What you need is the VBScript Eval() function:
strCalString = "(X*5)/25"
strX = 100
strOutput = Replace(strCalString, "X", strX)
strResult = Eval(strOutput)

Resources