I was wondering of there was a way of formatting an integer (789) into a string value (7.8.9).
Here is what I am currently doing to format the number:
Dim i as integer
Dim s as String
i = 789
s = Left(i, 1) & "." & Mid(i, 2, 1) & "." & Right(i, 1)
Note: The integer value will always be a 3 digit number.
Dim i As Integer
Dim s As String
i = 789
s = Format$(i, "0\.0\.0")
Related
I am trying to find whether the character is present in a given string or not but unable to search and increment value though it is present
Dim testchar,noOfSpecialChar
noOfSpecialChar=0
Dim specialChars
specialChars="*[#.^$|?#*+!)(_=-]."
for lngIndex = 1 to Len("test#123")
testchar = mid("test#123",lngIndex,1)
if((InStr(specialChars,testchar))) then
noOfSpecialChar=noOfSpecialChar+1
end if
next
The problem here is InStr() as highlighted in the documentation;
Returns the position of the first occurrence of one string within another.
We can use this knowledge to create a boolean comparison by checking the return value of InStr() is greater than 0.
Dim testString: testString = "test#123"
Dim testchar, foundChar
Dim noOfSpecialChar: noOfSpecialChar = 0
Dim specialChars: specialChars = "*[#.^$|?#*+!)(_=-]."
For lngIndex = 1 To Len(testString)
testchar = Mid(testString, lngIndex, 1)
'Do we find the character in the search string?
foundChar = (InStr(specialChars, testchar) > 0)
If foundChar Then noOfSpecialChar = noOfSpecialChar + 1
Next
I want to divide a string into three parts. I am using following code.
dim length1 as string
dim length2 as string
dim lenght3 as string
length1=Mid$(Text1.text,1,30)
length2=Mid$(Text1.text,31,70)
length3=Mid$(Text1.text,71,100)
msgbox length1
msgbox lenght2
msgbox length3
msgbox 2 show me the length of 11,30. Why?
What I have tried:
What have wrong with my code? I know that Mid$ start at the left of the string.
I am assuming that you are wanting each string to be 10 characters long? Your problem appears to be that you keep changing the length of the string by 10.
Edit: Since OP provided more information in his comments, I updated code to accommodate request.
Sub Test()
Dim length1 As String
Dim length2 As String
Dim length3 As String
Dim divTot As Integer, leftOver As Integer
divTot = Len(text1.Text) / 3
leftOver = Len(text1.Text) - (divTot * 2)
length1 = Mid$(text1.Text, 1, divTot)
length2 = Mid$(text1.Text, divTot + 1, divTot)
length3 = Mid$(Text1.Text, (divTot * 2) + 1, leftOver)
MsgBox length1
MsgBox length2
MsgBox length3
End Sub
I'm using VB6 and I'm trying to compare a string to an Array element. I know that if the string exists it will always be in index 0. Currently it always skips to End If. What am I doing wrong?
Dim attributeFinal As String,
strArray() As String,
stringFound As Integer,
code As String
attributes = "Material=10011,C=123123"
strArray = Split(attributes, ",")
If UBound(strArray) Then
code = strArray(0)
stringFound = InStr(1, "Material", code)
If stringFound <> 0 Then
attributeFinal = code & ",C=" & cCode
End If
End If
Solved thanks to #AndrewMorton. The arguments for comparing strings were in reverse order.
Dim attributeFinal As String,
strArray() As String,
stringFound As Integer,
code As String
attributes = "Material=10011,C=123123"
strArray = Split(attributes, ",")
If UBound(strArray) Then
code = strArray(0)
stringFound = InStr(code, "Material")
If stringFound <> 0 Then
attributeFinal = code & ",C=" & cCode
End If
End If
I want to reduce the decimal length
text1.text = 2137.2198231578
From the above, i want to show only first 2 digit decimal number
Expected Output
text1.text = 2137.21
How to do this.
Format("2137.2198231578", "####.##")
I was about to post use Format() when I noticed p0rter comment.
Format(text1.text, "000.00")
I guess Int() will round down for you.
Been many years since I used VB6...
This function should do what you want (inline comments should explain what is happening):
Private Function FormatDecimals(ByVal Number As Double, ByVal DecimalPlaces As Integer) As String
Dim NumberString As String
Dim DecimalLocation As Integer
Dim i As Integer
Dim LeftHandSide As String
Dim RightHandSide As String
'convert the number to a string
NumberString = CStr(Number)
'find the decimal point
DecimalLocation = InStr(1, NumberString, ".")
'check to see if the decimal point was found
If DecimalLocation = 0 Then
'return the number if no decimal places required
If DecimalPlaces = 0 Then
FormatDecimals = NumberString
Exit Function
End If
'not a floating point number so add on the required number of zeros
NumberString = NumberString & "."
For i = 0 To DecimalPlaces
NumberString = NumberString & "0"
Next
FormatDecimals = NumberString
Exit Function
Else
'decimal point found
'split out the string based on the location of the decimal point
LeftHandSide = Mid(NumberString, 1, DecimalLocation - 1)
RightHandSide = Mid(NumberString, DecimalLocation + 1)
'if we don't want any decimal places just return the left hand side
If DecimalPlaces = 0 Then
FormatDecimals = LeftHandSide
Exit Function
End If
'make sure the right hand side if the required length
Do Until Len(RightHandSide) >= DecimalPlaces
RightHandSide = RightHandSide & "0"
Loop
'strip off any extra didgits that we dont want
RightHandSide = Left(RightHandSide, DecimalPlaces)
'return the new value
FormatDecimals = LeftHandSide & "." & RightHandSide
Exit Function
End If
End Function
Usage:
Debug.Print FormatDecimals(2137.2198231578, 2) 'outputs 2137.21
Looks fairly simple, but I must be missing something subtle here. What about:
Option Explicit
Private Function Fmt2Places(ByVal Value As Double) As String
Fmt2Places = Format$(Fix(Value * 100#) / 100#, "0.00")
End Function
Private Sub Form_Load()
Text1.Text = Fmt2Places(2137.2198231578)
End Sub
This also works in locales where the decimal point character is a comma.
How can I convert a hex value to a decimal value in VB6?
I'm trying just to see if this works:
Dim hexVal as string
hexVal = "#7B19AB"
clng("&H" & hexVal)
However, I'm getting "Type MisMatch" error.
Get rid of the # sign
Dim hexVal as string
hexVal = "7B19AB"
clng("&H" & hexVal)
Get rid of the number sign (#) in the hexVal string.
This should do it
Dim hexVal as String
hexVal = "#7B19AB"
Dim intVal as Integer
intVal = Val("&H" & Replace(hexVal, "#", ""))
Try It:
value=CDbl("&H" & HexValue)
or
value=CInt("&H" & HexValue) 'but range +- 32,768
Try it this way:
Print Hex(Asc(Text1.Text))
Be very carful.
Dim hexVal as string
hexVal = "FFFF"
clng("&H" & hexVal)
will return a value of -1 because it thinks your HEX value is signed.
See what happens with F00A, again it thinks its signed.
Replace the Clng with ABS.
Dim uzunluk as Integer
On Error Resume Next
uzunluk = Len(Text1.Text)
For i = 0 To uzunluk
Text1.SelStart = i
Text1.SelLength = 1
Print Hex(Asc(Text1.SelText))
Next i
Dim hexVal As String
Dim str As String
Dim uzunluk As Integer
On Error Resume Next
hexVal = "#7B19AB"
str = Replace(hexVal, "#", "")
Text1.Text = str
uzunluk = Len(Text1.Text)
For i = 0 To uzunluk
Text1.SelStart = i
Text1.SelLength = 1
Print Hex(Asc(Text1.SelText))
Next i