Private Sub Text1_KeyPress(KeyAscii As Integer) \'De tilladte char i text1 box\'en Const Numbers$ = \"0123456789,.\"
If KeyAscii <> 9 And KeyAscii <> 32 And KeyAscii <> 8 Then If InStr(Numbers, Chr(KeyAscii)) = 0 Then KeyAscii = 0 MsgBox \"Only Numeric Characters and xxx Characters are allowed.\" End If End If End Sub
Vi har et dynamisk antal tekstfelter, og vi ved ikke fra gang til gang, hvilket et af felterne som er et valutafelt.
Kan man ikke anvende denne sætning:
If InStr(Numbers, ?????????) Then
og på en eller anden måde spørge om tekstens elementer en efter en er indeholdt i Numbers, og hvis man møder et element i strengen, som ikke er med i numbers, så generere en msgbox eller lign.
Prøv at kalde denne funktion i din tekst boks KeyPress function
Public Sub OnlyNumber(ByRef KeyAscii As Integer, Optional OnlyInt As Boolean = True, Optional AllowScientificNotation As Boolean = False, Optional AllowSign As Boolean = False, Optional AdditionalAllows As Variant)
Select Case KeyAscii Case Asc(\"-\") If (AllowSign) Then Exit Sub Case Asc(\"+\"), vbKeyE If (AllowScientificNotation) Then Exit Sub Case vbKeyBack Exit Sub Case Else Dim i As Integer If (Not IsMissing(AdditionalAllows) And Not IsEmpty(AdditionalAllows)) Then For i = 0 To UBound(AdditionalAllows) If (KeyAscii = Asc(AdditionalAllows(i))) Then Exit Sub End If Next End If End Select
If (Not OnlyInt) Then If (KeyAscii = Asc(\".\") Or KeyAscii = Asc(\",\")) Then If (CDbl(\"1\" + Chr(KeyAscii) + \"2\") = 1.2) Then Exit Sub End If End If End If
If (KeyAscii < Asc(\"0\") Or KeyAscii > Asc(\"9\")) Then KeyAscii = 0 End If End Sub
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.