Vb6 .text property for textbox required - vb6

I am trying to convert letters to numbers.
I have a sub which ensures only numbers are put into the textbox.
My questions is will the following code work. I have a textbox(for numbers) and combobbox(for letters)
Dim sha As String
Dim stringposition As Long
Dim EngNumber As Long
sha = "abcdefghifjklmnopqrstuvwxyz"
stringposition = InStr(1, sha, Mid(1, cmbEngletter.Text, 1))
MsgBox "stringposition"
EngNumber = (txtManuNo.Text * 10) + stringposition
My only question above would be will the multiplication work with a .text. I believe it won't because it is a string. Please advise then on how to deal with a situation.

You can use CLng() to convert a string to a Long variable
CLng() will throw an error though if it doesn't like the contents of the string (for example if it contains a non-numeric character), so only use it when you are certain your string will only contain numbers
More forgiving is it to use Val() to convert a string into a numeric variable (a Double by default)
I also suggest you look into the following functions:
Asc() : returns the ASCII value of a character
Chr$() : coverts an ASCII value into a character
Left$() : returns the first characters of a string
CStr() : convert a number into a string
I think in your code you mean to show the contents of your variable stringposition instead of the word "stringposition", so you should remove the ""
I do wonder though what you are trying to accomplish with your code, but applying the above to your code gives:
Dim sha As String
Dim stringposition As Long
Dim EngNumber As Long
sha = "abcdefghifjklmnopqrstuvwxyz"
stringposition = InStr(1, sha, Left$(cmbEngletter.Text, 1))
MsgBox CStr(stringposition)
EngNumber = (Val(txtManuNo.Text) * 10) + stringposition
I used Val() because I am not certain your txtManuNo will contain only numbers
To ensure an user can only enter numbers you can use the following code:
Private Sub txtManuNo_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case vbKeyBack
'allowe backspace
Case vbKey0 To vbKey9
'allow numbers
Case Else
'refuse any other input
KeyAscii = 0
End Select
End Sub
An user can still input non-numeric charcters with other methods though, like copy-paste via mouse actions, but it is a quick and easy first filter

Related

hidden space in excel

I tried almost all the methods (CLEAN,TRIM,SUBSTITUTE) trying to remove the character hiding in the beginning and the end of a text. In my case, I downloaded the bill of material report from oracle ERP and found that the item codes are a victim of hidden characters.
After so many findings, I was able to trace which character is hidden and found out that it's a question mark'?' (via VBA code in another thread) both at the front and the end. You can take this item code‭: ‭11301-21‬
If you paste the above into your excel and see its length =LEN(), you can understand my problem much better.
I need a good solution for this problem. Therefore please help!
Thank you very much in advance.
Thanks to Gary's Student, because his answer inspired me.
Also, I used this answer for this code.
This function will clean every single char of your data, so it should work for you. You need 2 functions: 1 to clean the Unicode chars, and other one to clean your item codes_
Public Function CLEAN_ITEM_CODE(ByRef ThisCell As Range) As String
If ThisCell.Count > 1 Or ThisCell.Count < 1 Then
CLEAN_ITEM_CODE = "Only single cells allowed"
Exit Function
End If
Dim ZZ As Byte
For ZZ = 1 To Len(ThisCell.Value) Step 1
CLEAN_ITEM_CODE = CLEAN_ITEM_CODE & GetStrippedText(Mid(ThisCell.Value, ZZ, 1))
Next ZZ
End Function
Private Function GetStrippedText(txt As String) As String
If txt = "–" Then
GetStrippedText = "–"
Else
Dim regEx As Object
Set regEx = CreateObject("vbscript.regexp")
regEx.Pattern = "[^\u0000-\u007F]"
GetStrippedText = regEx.Replace(txt, "")
End If
End Function
And this is what i get using it as formula in Excel. Note the difference in the Len of strings:
Hope this helps
You have characters that look like a space character, but are not. They are UniCode 8236 & 8237.
Just replace them with a space character (ASCII 32).
EDIT#1:
Based on the string in your post, the following VBA macro will replace UniCode characters 8236 amd 8237 with simple space characters:
Sub Kleanup()
Dim N1 As Long, N2 As Long
Dim Bad1 As String, Bad2 As String
N1 = 8237
Bad1 = ChrW(N1)
N2 = 8236
Bad2 = ChrW(N2)
Cells.Replace what:=Bad1, replacement:=" ", lookat:=xlPart
Cells.Replace what:=Bad2, replacement:=" ", lookat:=xlPart
End Sub

having trouble validating input. need to verify that there is at least 1 numeric char and 1 alpha char

As stated in description, I need to validate user input to make sure it is a minimum of 6 characters long and contains 1 numeric character and 1 character from the alphabet.
So far, I've gotten the length validation working but cannot seem to get my numeric validation to function properly. If I enter nothing but numbers, it works, but if I put letters in IE abc123 it will not recognize that there are numbers present.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If txtPassword.TextLength < 6 Then
lblError.Text = "Sorry that password is too short."
ElseIf txtPassword.TextLength >= 6 Then
Dim intCheck As Integer = 0
Integer.TryParse(txtPassword.Text, intCheck)
If Integer.TryParse(txtPassword.Text, intCheck) Then
lblError.Text = "Password set!"
Else
lblError.Text = "Password contains no numeric characters"
End If
End If
End Sub
End Class
how about a regular expression?
using System.Text.RegularExpressions;
private static bool CheckAlphaNumeric(string str)   {
return Regex.Match(str.Trim(), #"^[a-zA-Z0-9]*$").Success;  
}
of if you just up to validating a complex password, then this will do it.
-Must be at least 6 characters
-Must contain at least one one lower case letter,
-One upper case letter,
-One digit and one special character
-Valid special characters are – ##$%^&+=
Dim MatchNumberPattern As String = "^.*(?=.{6,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%^&+=]).*$"
If txtPasswordText.Trim <> "" Then
If Not Regex.IsMatch(txtPassword.Text, MatchNumberPattern) Then
MessageBox.Show("Password is not valid")
End If
End If
You may want to use regex to validate if the input contains upper/lower case alpha chars, numbers, special chars.

How to split string by HEX character

I need help to split string which contains a HEX character.
How to do it?
The code I have got doesnt work.
dim itr
itr="343434 XX7777777" ' SI
msgbox itr
dim scrns
scrns=Split(itr,"SI",-1,1)
msgbox scrns(1)
Define the character by its numeric value (0x0f for "shift in"):
scrns = Split(itr, Chr(&h0f))
In VBScript you define hexadecimal numbers by prefixing them with &h. The Chr function turns the number into the corresponding character.
As #JNevill pointed out in the comments you could also use the decimal instead of the hexadecimal value:
scrns = Split(itr, Chr(15))

How to convert string to double in VBScript?

I need to write some code in VBScript and have a version number string in a text file that I need to compare against. If I write this code as a test:
option explicit
Dim VersionString
VersionString = "6.2.1"
Dim Version
Version = CDbl (VersionString)
Version = Version * 100
I get an error on the CDbl line:
Microsoft VBScript runtime error: Type mismatch: 'CDbl'
How should I read and compare this string value?
"6.2.1" is not a Double formatted as a String. So CDbl() can't convert it. Your options are:
treat versions as strings; ok if you only need to compare for equality, bad if you need "6.9.1" to be smaller that "6.10.2"
Split() the string on "." and deal with the parts (perhaps converted to Integer/Long) separately; you'll need to write a comparison function for such arrays
Remove the "."s and CLng the resulting string; will break for versions like "6.10.2"
Split() the string on "*" and multiply + add the 'digits' to get one (integer) version number (6 * 100 + 2 * 10 + 1 * 1 = 621 for your sample); may be more complex for versions like "15.00.30729.01"
The conversion to a double isn't working because there are two decimal points in your string. To convert the string, you will have to remove one or both of them.
For this, you can use the Replace function. The syntax for Replace is
Replace(string, find, replacewith [, start [, count [, compare]]])
where string is the string to search, find is the substring to find, replacewith is the substring to replace find with, start is an optional parameter specifying the index to start searching at, count is an optional parameter specifying how many replaces to make, and compare is an optional parameter that is either 0 (vbBinaryCompare) to perform a binary comparison, or 1 (vbTextCompare) to perform a textual comparison
' Remove all decimals
Version = CDbl(Replace(VersionString, ".", "")
' Remove only the first decimal
Version = CDbl(Replace(VersionString, ".", "", 1, 1)
' Remove only the second decimal
Version = CDbl(Replace(VersionString, ".", "", 3, 1)

How to read an integer from a string in ASP (VB)

I have some picture named like this . (like "2.jpeg", "1234.gif" etc.)
how can I get the integer from the string?
I mean, is there something like the C function:
sscanf(myString,"%d", myInteger);
thanks
Alessandro
You will need to remove the non-numeric portion and then call TryParse.
Dim numericPortion As String
Dim result As Integer
numericPortion = myString.Substring(0, myString.IndexOf('.'))
If (Not Int32.TryParse(numericPortion, ByRef result)) Then
''// Handle Error
Else
''// Use Result
End If

Resources