I'm doing some development at school and i have to create an app as follows:
Create a Digits of a number app that accepts a two-digit number and displays the digits separately.

The instructions give the #32 and the picture of the app separates 3 and 2 in 2 different labels.

This in vb 6 by the way thanks in advance.

6 Years
Discussion Span
Last Post by kinwang2009

Simply convert the number to a string and, presuming it is always 2 digits in length, do the following: -

iInteger = txtNumber.Text
sString = str(iInteger)

sFirst = Left(sString, 1)
sSecond = Right(sString, 1)


Well considering that the "Text" in text1.text is "Text" to begin with there WebSpoonUK, your code shows how vb coerces values from string to number without explicit conversion (iIntegerVariable = Int(Text1.Text)). So, your code can be shortened to just...

Text2.Text = Left(Text1.Text, 1)
Text3.Text = Right(Text1.Text, 1)

Furthermore Devsof, setting the max length property of text1 to 2 will prevent more than two characters from being entered into the text box but that will not prevent other characters than numbers from being entered. BUT that can be fixed with the following...

Option Explicit

Private Sub Text1_KeyPress(KeyAscii As Integer)

On Error GoTo Text1_KeyPressError

If KeyAscii = vbKeyBack Then Exit Sub
Select Case KeyAscii
Case 48 To 57
Case Else
  KeyAscii = 0
End Select

Exit Sub

MsgBox "Text1_KeyPress " & Err.Number & ":" & Err.Description

End Sub

Good Luck


vb5prgrmr will work fine. If you want to accept Enter Key just like Backspace Key then you can make little changes like

If KeyAscii = vbKeyBack Or KeyAscii = vbKeyReturn Then Exit Sub

instead of

If KeyAscii = vbKeyBack Then Exit Sub

in vb5prgrmr's code.

This article has been dead for over six months. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.