The following must work in qtp so i can not use WScript.Echo. The following code have to ask for an integer between 1 to 10 inclusive using an inputbox. If nothing entered the it has to give a message "Aborted".
If anything else entered then it has to say what is the problem and ask again for the number until I abort by cancel or by entering nothing. I have the following code but it looks like it is skipping the first condition and goes to the first else in the loop:
Option Explicit
Dim vNum, sNum, nNum
Do
vNum = InputBox("Please enter an integer between 1 and 10 inclusive")
If IsEmpty(vNum) Then
msgbox("Aborted")
Exit Do
Else
sNum = Trim(vNum)
If "" = sNum Then
vNum=Inputbox("Empty string")
Else
If IsNumeric(sNum) Then
nNum = CDbl(sNum)
If nNum <> Fix(nNum) Then
vNum=inputbox("Not an Integer")
Else
If nNum < 1 Or nNum > 10 Then
vNum=inputbox ("Not in range")
Else
msgbox nNum,("number ok")
Exit Do
End If
End If
Else
vNum= inputbox ("Not a number")
End If
End If
End If
Loop
msgbox ("Done")
You could loop and change the instruction message each time:
Dim vNum, instruction
instruction = "Please enter an integer between 1 and 10 inclusive"
Do
vNum = InputBox(instruction)
If vNum = False Then
MsgBox "Aborted"
Exit Do
ElseIf CStr(Trim(vNum)) = "" Then
instruction = "Empty string"
ElseIf Not IsNumeric(vNum) Then
instruction = "Not an integer"
ElseIf IsNumeric(vNum) And vNum < 1 Or vNum > 10 Then
instruction = "Not in range"
ElseIf IsNumeric(vNum) And vNum > 0 And vNum < 11 Then
MsgBox "Number OK"
Exit Do
Else
instruction = "Invalid Entry"
End If
Loop
This is what help says.
If the user clicks OK or presses ENTER, the InputBox function returns whatever is in the text box. If the user clicks Cancel, the function returns a zero-length string ("").
So you aren't testing for a empty, or zero length, string. It is a valid string, just empty.
Also from Help the meaning of Empty in VBS, which has nothing to do with what's in a string.
Empty
The Empty keyword is used to indicate an uninitialized variable value. This is not the same thing as Null.
Update
HELP IS NOT WRONG. InputBox returns a zero length string just like the docs say.
A uninitialized variable HAS A VALUE (for numbers, dates, and strings)
0 for numbers
1899 sometime for dates
and a zero length string for strings (and a string of spaces for fixed length strings).
HELP IS NOT a TECHNICAL REFERENCE
Help is a CONTRACTUAL document describing behaviour not implementation. As in the COM philosophy.
This is known as LET COERCION. And why x=65:Msgbox x works. There are two variables there.
From VBA Implementers Guidelines
The semantics of Empty Let-coercion depend on the destination’s declared type: Source
Any numeric type - The result is 0.
Boolean - The result is False.
Date - The result is 12/30/1899 00:00:00.
String - The result is a 0-length string.
String * length - The result is a string containing length spaces.
Any class or Object - Runtime error 424 (Object required) is raised. -
Any other type except Variant - Runtime error 13 (Type mismatch) is raised.
Related
I have two functions, and I am trying to use the result of one function in the second one. It's going to the else part, but it's not printing anything for "cus_number".
How do I get the "cus_number" printed?
Function getNumber
number = "423"
End Function
cus_number = getNumber
If (IsNull(cus_number)) Then
WScript.Echo "Number is null"
Else
WScript.Echo "cus_number : " & cus_number
End If
To return a value from a VBScript function, assign the value to the name of the function, like this:
Function getNumber
getNumber = "423"
End Function
I'm getting a error message in a VB6 .exe file running on Windows XP.
I compile and "make it" on Windows 7/8, but always get an Overflow error message when it executes this two lines on XP:
sUrl = "C:\Arquivos de Programas\Internet Explorer\IEXPLORE.EXE http://example.com/WebForms/send.aspx?id=" & intCodID & "&type=500&usr=" & intCodUser
openWeb = Shell(sUrl, vbMaximizedFocus)
sUrl is a String and OpenWeb is actually a Integer, but I already declared it as Double and as nothing (just Dim OpenWeb) and still get the overflow error.
UPDATE
Didn't managed to find out what was happening there, but another solution for calling IE:
Dim IE
sUrl = "http://www.google.com/"
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate sUrl
While the VB6 documentation says Shell() returns a Variant Double... that appears to be obsolete information left over from manuals for earler versions of VB. Instead if you check the typelib info (i.e. look in the IDE's Object Browser) it actually returns a Double type result value.
As far as I can tell Shell() is a wrapper around a call to the WinExec() function.
The returned values are:
0 The system is out of memory or resources.
ERROR_BAD_FORMAT = 11 The .exe file is invalid.
ERROR_FILE_NOT_FOUND = 2 The specified file was not found.
ERROR_PATH_NOT_FOUND = 3 The specified path was not found.
or a Process ID
Also contrary to the documentation, Shell() turns those error values into exceptions ("File not found", "Invalid procedure call or argument," etc.). So if the call succeeds you always get back a PID value.
In all cases this is a DWORD. So it always fits in a Double without the possibility of an overflow. If you are seeing an overflow there is something else going wrong in your code.
Sadly a Double isn't particularly useful here, though it can at least hold the entire range of values. But you'd normally want to carefully convert it to a Long value:
Option Explicit
Function DDoubleToDLong(ByVal DDouble As Double) As Long
'Some functions like the intrinsic Shell() return a Double
'to get around the lack of a UI4 type (DWORD, i.e. unsigned
'Long) in VB. Of course this isn't clean to pass to API
'calls, making it sort of worthless so we need to do a type
'conversion such as this:
If DDouble > 2147483647# Then
DDoubleToDLong = CLng(DDouble - 2147483648#) Or &H80000000
Else
DDoubleToDLong = CLng(DDouble)
End If
End Function
Private Sub Form_Load()
Dim DD As Double
Dim DL As Long
AutoRedraw = True
Font.Name = "Courier New" 'Or other handy monospaced font.
Font.Size = 12#
DD = 0#: DL = DDoubleToDLong(DD): Print DD, DL, Hex$(DL)
DD = 1#: DL = DDoubleToDLong(DD): Print DD, DL, Hex$(DL)
DD = 2147483647#: DL = DDoubleToDLong(DD): Print DD, DL, Hex$(DL)
DD = 2147483648#: DL = DDoubleToDLong(DD): Print DD, DL, Hex$(DL)
DD = 4294967295#: DL = DDoubleToDLong(DD): Print DD, DL, Hex$(DL)
End Sub
Integer is worthless since overflows will be common. Long without the conversion could cause overflows now and then. String is just silly.
You also need to quote the values for the EXE and its arguments property, as in:
Option Explicit
Function DDoubleToDLong(ByVal DDouble As Double) As Long
If DDouble > 2147483647# Then
DDoubleToDLong = CLng(DDouble - 2147483648#) Or &H80000000
Else
DDoubleToDLong = CLng(DDouble)
End If
End Function
Private Sub Form_Load()
Dim sUrl As String
Dim PID As Long
sUrl = """C:\Arquivos de Programas\Internet Explorer\IEXPLORE.EXE"" " _
& """http://example.com/WebForms/send.aspx?id=" _
& intCodID _
& "&type=500&usr=" _
& intCodUser _
& """"
PID = DDoubleToDLong(Shell(sUrl, vbMaximizedFocus))
End Sub
Even this isn't quite "right" since exception handling should be added. And both intCodID and intCodUser may require "cleansing" (UrlEncoding) depending on what types they are and what values they really have. These might be Integers based on the names, with you relying on implicit String coercion? If so they might be Ok.
BTW, as we see above special folder names get localized. For that matter the system drive might not even be C:\ at all! So such paths should never be hard-coded but instead be built up based on values returned from calls to Shell32 to look up the special folder.
An integer can only be a whole number. No decimals.
You say it's declared as an integer therefore you cannot assign 1. anything, and you certainly can't assign anything like that to a number variable as it's not a valid number anyway as it has two decimal points.
You need to declare it as string.
I give validation for mobile number but I want mobile number should start with 7,8,9 digits.I want to give validation that mobile number should start with 7,8,9 digits only.
If Len(TextBox11.Text) < 10 Or Len(TextBox11.Text) > 10 Then
MsgBox("Enter the phone number in 10 digits!", vbExclamation, "")
This code is gives 10 digits validation but mobile number schould start with 7,8,9 digits.
10 digits, starting 7,8,9
isValid = TextBox11.Text like "[789]#########"
if (not isValid) then
msgbox "Invalid"
...
If Len(TextBox11.Text) <> 10 Or Left(TextBox11.Text, 1) <> "7" or Left(TextBox11.Text, 1) <> "8" or Left(TextBox11.Text, 1) <> "9" Then
MsgBox("Enter the phone number in 10 digits!", vbExclamation, "")
End If
RegEx are a way for doing complex validation. It's in the VBScript library which is called Microsoft Regular Expressions 5.5.
[7-9]\d{9}$
a number between 7 and 9 [7-9], followed by digits \d and there should be 9 of them {9}. $ marks end of input so won't match if more than 10 characters.
I use this code which is work fine
Dim MobileNumber As New Regex("^[7-9][0-9]*")
If MobileNumber.IsMatch(TextBox11.Text) Then
MsgBox("Valid Mobile number")
else
MsgBox("Not Valid Mobile number")
End If
Private Sub Text7_LostFocus()
Dim CHAR As Integer
Dim TELEPHONE As String
TELEPHONE = Val(Text7.Text)
CHAR = Left(TELEPHONE, 1)
If CHAR < 7 Then
Msg("ENTR THE CORRECT MOBILE NUMBER")
Text7.Text
Else If (TELEPHONE < 10) Then
Msg("ENTR THE 10 DIGIT MOBILE NUMBER")
Text7.Text
Exit Sub
End If
End Sub
Private Sub Text5_Validate(Cancel As Boolean)
If (Len(Text5.Text) < 10 Or Len(Text5.Text) > 10) Then
MsgBox "Enter the phone number in 10 digits!", vbExclamation, ""
End If
End Sub
On my reporting application which is developed using SSRS 2005, I have a parameter of type string which accepts time. the time should be in the format "HH:mm:ss" How can I check if the input string is of correct format?
I tried to do the following
IsDate(TimeValue(parametr!stime.Value))
This returns true as long as the value is within range. But if the value is 24:00:00 or a wrong value then an exception is thrown.
I also tried to create a function in the report code as follows:
Public Function CheckNum(sNum as String) as Boolean
Dim msg as String
msg = ""
Try
If IsDate(TimeValue(sNum))=1 Then
Return True
Else
msg="Parameter must be a number"
End If
Catch ex as Exception
Return False
End Try
If msg <> "" Then
MsgBox(msg, 16, "Parameter Validation Error")
Err.Raise(6,Report) 'Raise an overflow
End If
End Function
And when I input a value 24:00:00 I still get an error
" The conversion of a char type to datetime data type resulted in an out of range date time value"
How can I handle the exception so that I don't the error?
EDIT:
public Function CheckNum(sNum as String) as Boolean
Dim REGEX_TIME = "^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$"
If System.Text.RegularExpressions.Regex.IsMatch(sNum, REGEX_TIME) Then
Return True
Else
Return False
End If
End Function
Then I assigned a parameter(validateTime) value as =Code.CheckNum(Parameters!sTime.Value)
But the value of the parameter is always true. When I specify a value greater than 23, I still see the error. Please see the image
Instead of using IsDate function, use VB.NET regular expressions. SSRS allows full use of .NET functions.
See an example of time regex.
A good tutorial on regex.
Example Code Console Application
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim REGEX_TIME = "^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$"
Dim InputList As List(Of String) = New List(Of String)
InputList.Add("25:00:21")
InputList.Add("22:00:21")
InputList.Add("AA:00:21")
InputList.Add("17:21:02")
For Each input As String In InputList
If Regex.IsMatch(input, REGEX_TIME) Then
Console.WriteLine("TIME " + input + " IS OK")
Else
Console.WriteLine("TIME " + input + " IS NOT OK")
End If
Next
End Sub
End Module
Output is :
TIME 25:00:21 IS NOT OK
TIME 22:00:21 IS OK
TIME AA:00:21 IS NOT OK
TIME 17:21:02 IS OK
I think, you can capture a InvalidCastException.
Is it possible to test a string with IsNumeric() and for it to return true, but when you cast that same string to an integer using CInt() and assign it to a variable of type integer that it will give a type mismatch error?
I ask because I was getting a type mismatch error, so I used IsNumeric() to check the string was numeric before trying to cast it, but I still get the error.
I am tearing my hair out with this.
Here is the code in question.
iGlobalMaxAlternatives = CInt(strMaxAlternatives) is where the error is occuring.
Dim strMaxAlternatives As String
Dim iGlobalMaxAlternatives As Integer
iGlobalMaxAlternatives = 0
bSurchargeIncInFare = True
strMaxAlternatives = ReadStringKeyFromRegistry("Software\TL\Connection Strings\" & sConn & "\HH", "MaxAlt")
If IsNumeric(strMaxAlternatives) And strMaxAlternatives <> "" Then
iGlobalMaxAlternatives = CInt(strMaxAlternatives)
End If
You may have an overflow due the maximum integer size; the currency type actually does very well for large numbers (but beware of any regional issues). See edits below for Int64 discussion.
According to MSDN documentation on IsNumeric:
IsNumeric returns True if the data
type of Expression is Boolean, Byte,
Decimal, Double, Integer, Long,
SByte, Short, Single, UInteger,
ULong, or UShort, or an Object that
contains one of those numeric types.
It also returns True if Expression is
a Char or String that can be
successfully converted to a number.
IsNumeric returns False if Expression
is of data type Date or of data type
Object and it does not contain a
numeric type. IsNumeric returns False
if Expression is a Char or String
that cannot be converted to a number.
Since you are getting a Type Mismatch, perhaps a Double is interfering with the conversion. The IsNumeric does not guarantee it is an Integer, just that it matches one of the numeric possibilities. If the number is a double, perhaps regional settings (comma versus period and so on) are causing the exception.
You might try converting it to a double and then to an integer.
' Using a couple of steps
Dim iValue As Integer
Dim dValue As Double
dValue = CDbl(SourceValue)
iValue = CInt(iValue)
' Or in one step (might make debugging harder)
iValue = CInt(CDbl(SourceValue))
EDIT: After your clarification, it appears you are getting an overflow conversion. First try using a Long and CLng() instead of CInt(). There is still a chance the entry is Int64 though, which is more difficult using VB6.
I have used the following code for the LARGE_INTEGER and Integer8 types (both Int64), but it may not work for your situation:
testValue = CCur((inputValue.HighPart * 2 ^ 32) + _
inputValue.LowPart) / CCur(-864000000000)
This example was from an LDAP password expiration example, but like I said it may or may not work in your scenario. If you don't have the LARGE_INTEGER type, it looks like:
Private Type LARGE_INTEGER
LowPart As Long
HighPart As Long
End Type
Search for LARGE_INTEGER and VB6 for more information.
EDIT: For debugging, it may be useful to temporarily avoid error handling and then turn it back on after passing the troubling lines:
If IsNumeric(strMaxAlternatives) And strMaxAlternatives <> "" Then
On Error Resume Next
iGlobalMaxAlternatives = CInt(strMaxAlternatives)
If Err.Number <> 0 Then
Debug.Print "Conversion Error: " & strMaxAlternatives & _
" - " & Err.Description
EndIf
On Error Goto YourPreviousErrorHandler
End If
Yes, "3.41" would be numeric but not an integer.
VB6 doesn't provide good methods to guarantee CInt won't fail. I've found the simplest way is to just use error-handling.
function TryParse(byval text as string, byref value as integer) as boolean
on error resume next
value = CInt(text)
TryParse = (err.number = 0)
endfunction
Of course your error-handling preferences may vary.
Yes. Try this:
If IsNumeric("65537") Then
Dim i As Integer
i = CInt("65537") 'throws an error on this line!
End If
This one's an overflow, but I think it illustrates the unreliability of IsNumeric() in general (especially for ints - for doubles it's much more reliable).
According to the VB6 documentation, "IsNumeric returns True if the data type of Expression is Boolean, Byte, Decimal, Double, Integer, Long, SByte, Short, Single, UInteger, ULong, or UShort, or an Object that contains one of those numeric types. It also returns True if Expression is a Char or String that can be successfully converted to a number."
Many of those cannot be converted to an Integer. For example "1.5" is numeric but it's not an integer. So, you can convert it to a number, but not necessarily an integer.
The following code works without a Type Mismatch error in Visual BASIC 6
Dim I As Integer
I = CInt("3.41")
The same for this variant
Dim I As Integer
Dim TempS As String
TempS = "3.41"
I = CInt(TempS)
Posting the code in question would help answer your question. Basically there are several function in VB6 that are used to convert strings into number.
CInt and Int convert into number but handle rounding different. Direct assignment works and equivalent to using CInt. Howver I recommend continuing to use CInt to make the operation clear to you and your fellow developers in the future.
CInt works on number with commas like "3,041.41" However VB6 has problem handling region settings so if you are using notation other than standard American English you will get strange results and errors.
Your best bet is to start logging the errors with the actual values it's working with so you can figure out whats going on.
IsNumeric will return true if it can convert the string to a number. Even if there are non-numeric characters in the string. I always loop though the string one character at a time and test each character. If one character fails then I can return an error.
Just found this nugget. If you run the following, script #1 returns TRUE but script #2 & #3 will fail:
SELECT ISNUMERIC('98,0') AS isNum -- Fails
SELECT CONVERT(INT, '98,0') -- Fails
SELECT CONVERT(NUMERIC(11,4), '98,0') -- Fails
Two options...
Change
If IsNumeric(strMaxAlternatives) And strMaxAlternatives <> "" Then
iGlobalMaxAlternatives = CInt(strMaxAlternatives)
End If
To
If IsNumeric(strMaxAlternatives) And strMaxAlternatives <> "" Then
iGlobalMaxAlternatives = CDbl(strMaxAlternatives) ' Cast to double instead'
End If
Or
If IsNumeric(strMaxAlternatives) And strMaxAlternatives <> "" Then
If CDbl(strMaxAlternatives) Mod 1 = 0 Then ' Make sure there\'s no decimal points'
iGlobalMaxAlternatives = CInt(strMaxAlternatives)
End If
End If