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
Related
I have written code which generates a random number between 1-9.
I now want to add to this and generate a random number but generate a new number if that number has already been used before.
Sub main()
Dim max,min
max=9
min=1
Randomize
MsgBox(Int((max-min+1)*Rnd+min))
// Random number between 1-9 is generated
I had tried to implement a loop but i'm unsure of how it would work as i would need to keep the number generated in memory
If Int = random
Msgbox("Already in use")
End If
If Int = not random Then
Msgbox("Can be used")
End If
End Sub
Sounds like you just want to keep track of which random numbers have already been chosen. You could handle this any number of different ways (eg, with an array, hash-table/dictionary, numeric bit mask, etc.)
The solution I present below is similar to a numeric bit mask, but uses a string. Starting at all zeros (eg, "0000"), each indexable position in the string gets populated with a one (1) until the string becomes all ones (eg, "1111"). While potentially oversimplified -- since it assumes your min will always be one (1) -- it should get you started.
Dim min : min=1
Dim max : max=9
Dim result : result = ""
Dim r, s: s = String(max, "0")
Randomize
Do
r = Int((max-min+1)*Rnd+min)
If "0" = Mid(s, r, 1) Then
WScript.Echo "Can be used: " & r
result = result & ":" & r
s = Left(s, r-1) & "1" & Right(s, max-r)
Else
WScript.Echo "Already in use: " & r
End If
Loop Until String(max, "1") = s
WScript.Echo "Result" & result
Sample output:
Can be used: 4
Can be used: 5
Can be used: 9
Can be used: 3
Already in use: 3
Can be used: 1
Can be used: 8
Can be used: 6
Already in use: 6
Can be used: 7
Already in use: 8
Already in use: 4
Already in use: 3
Already in use: 1
Already in use: 6
Can be used: 2
Result:4:5:9:3:1:8:6:7:2
Hope this helps.
As of a windows update, the Join operator in VB6 is throwing a Type mismatch error.
My vb6 jtTaskBO class has the following properties;
Friend Property Set PredecessorOffsets(ByVal Offsets As LongArray)
mPredecessorOffsets.Assign Offsets
End Property
Public Property Get PredecessorOffsets() As LongArray
Set PredecessorOffsets = mPredecessorOffsets
End Property
My code has been working for years but I had to modify it today as follows;
Private Function GetPredecessorsDisplay(bo As jtTaskBO)
On Error GoTo error_handle:
' This used to work
' GetPredecessorsDisplay = Join(bo.PredecessorOffsets, ", ")
' replaced by the following
Dim s As String
s = ""
If Not IsNull(bo.PredecessorOffsets) Then
If (bo.PredecessorOffsets.Count > 0) Then
Dim i As Integer
Dim n As Integer
n = bo.PredecessorOffsets.Count - 1
For i = 0 To n
If i <> 0 Then s = s & ", "
s = s & bo.PredecessorOffsets(i)
Next
End If
End If
GetPredecessorsDisplay = s
exit_point:
Exit Function
error_handle:
MsgBox Error$
Resume exit_point
End Function
Winver reports
Windows 10 version 1809 Build 17763.678
A user also reports the issue with Windows 7
Is there any way to fix the issue without doing a release?
I see there is mention of VB6 in KB4511553 or KB4512508
[Update]
I updated to version 1903 Build 18362.295 but the issue is still there.
Is there an official channel to report to Microsoft via?
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.
I am a long time C# developer and have had to look at some VB6 code lately. After some digging, we found out that, somewhere, somehow, we are storing a number as a string. Now we are reading it back and our code is not handling culture differences very well. Here is an example:
Dim text as String
Dim myVal as Double
Set text = "1,123456"
'This sets the value of myVal to 1123456 on our system - incorrect
myVal = text
Set text = "1.123456"
'This sets the value of myVal to 1.123456 on our system - correct
myVal = text
Keeping in mind that this is VB6 and NOT VB.NET, are there any built-in methods, functions, whatever, that can convert a string to a double using a particular culture?
Or at the very least, hinting to the conversion that we may be dealing with a different format?
We are still digging how the value gets written and see if we can reverse engineer the process to give us a consistent result. However, our customer(s) already has(have) data in one format or the other (or both, we are checking...), so a proper conversion algorithm or implementation would be a better answer at this point.
I've used this quick and dirty function in the past to get a double from a text. Here in Argentina, people sometimes use the point and sometimes the comma to separate decimal values, so this function is ready to accept both formats. If only one punctuation is present, it's assumed that it's the decimal separator.
function ConvertDBL( texto )
dim retval, SepDecimal, SepMiles
If texto = "" then texto = "0"
retval = replace(trim(texto), " ", "")
If cdbl("3,24") = 324 then
SepDecimal = "."
SepMiles = ","
else
SepDecimal = ","
SepMiles = "."
end if
If InStr( Retval, SepDecimal ) > 0 then
If InStr( Retval, SepMiles ) > 0 then
If InStr( Retval, SepDecimal ) > InStr( Retval, SepMiles ) then
Retval = replace( Retval, SepMiles, "" )
else
Retval = replace( Retval, SepDecimal, "" )
Retval = replace( Retval, SepMiles, SepDecimal )
end if
end if
else
Retval = replace( Retval, SepMiles, SepDecimal )
end if
ConvertDbl = cdbl( retval )
end function
You cannot choose an arbitrary culture by passing a culture object like you can in .Net. You can choose:
Functions which always use the current regional settings. CStr, Format, CDbl etc. Implicit conversions (like the ones in your question) also use the current regional settings.
Functions which always use USA regional settings (similar to the .Net invariant culture). Str, Val
Usually the current regional settings are taken from Control Panel. There are rumours that you can call SetThreadLocale to change the regional settings from code at runtime - never tried it myself.
In Visual Basic tested works very fast and efficient.
Step 1: Conversion from a string to a value representation.
Step 2: Conversion from a value representation to a double integer value.
Dim Str as String
Dim mValue as double
str = "100,10"
mValue = CDbl(Val(str))
Some of us unfortunately are still supporting legacy app like VB6. I have forgotten how to parse a string.
Given a string:
Dim mystring As String = "1234567890"
How do you loop in VB6 through each character and do something like
for each character in mystring
debug.print character
next
In C# i would do something like
char[] myChars = mystring.ToCharArray();
foreach (char c in theChars)
{
//do something with c
}
Any ideas?
Thanks a lot
You can use the 'Mid' function to get at the individual characters:
Dim i As Integer
For i = 1 To Len(mystring)
Print Mid$(mystring, i, 1)
Next
Note this is untested.
There is no possibility to use foreach on strings.
Use
Dim i As Integer
For i = 1 To Len(YourString)
Result = Mid$(YourString, i, 1)
Next
note that the type of Result is a length-1 string, no char or byte type.
If performance is important, you'll have to convert the string to a bytearray fist (using StrConv) and then loop through it like this.
Dim i As Long
For i = 0 To UBound(Data)
Result = Data(i) ' Type is Byte '
Next
This is much more efficient.
The easiest way is to convert the string into an array of bytes and iterate over the byte array (converting each byte to a character).
Dim str As String
Dim bytArray() As Byte
Dim count As Integer
str = "This is a string."
bytArray = str
For count = 0 To UBound(bytArray)
Debug.Print Chr(bytArray(count))
Next
Don't loop; rather, set a reference to Microsoft VBScript Regular Expressions library and use regular expressions to achieve your 'do something' goal.