I'vは、TextBox
ESに入力されたすべての値がに従っていることを確認押されています一度それだ(「確認」ボタンのような)サンプルUser_Form
とCommandButton
を作成しましたルール
以下のコードは、「パレットID」(9桁)と「カートンID」(10桁)の値をチェックします。例えば、各ボックスの `MaxLength`プロパティを設定し、テキストボックスのイベントを処理
コード
Option Explicit
Private Sub CommandButton1_Click()
Dim Reg1 As Object
Dim Reg2 As Object
Dim RegMatches As Object
' ====== Test PalletID_Tb =====
Set Reg1 = CreateObject("VBScript.RegExp")
With Reg1
.Global = True
.IgnoreCase = True
.Pattern = "\d{9,9}" ' Match any set of 9 digits
End With
Set RegMatches = Reg1.Execute(Me.PalletID_Tb.Value)
If RegMatches.Count = 1 Then '<-- make sure there is only 1 match (9 digits and not 18 or 27)
MsgBox "Value in Pallet ID is OK"
Else
MsgBox "Pallet ID must have a 9 digit format"
Me.PalletID_Tb.Value = ""
Exit Sub
End If
' ====== Test CartonID_Tb =====
Set Reg2 = CreateObject("VBScript.RegExp")
With Reg2
.Global = True
.IgnoreCase = True
.Pattern = "\d{10,10}" ' Match any set of 10 digits
End With
Set RegMatches = Reg2.Execute(Me.CartonID_Tb.Value)
If RegMatches.Count = 1 Then '<-- make sure there is only 1 match (10 digits and not 20)
MsgBox "Value in Carton ID is OK"
Else
MsgBox "Carton ID must have a 10 digit format"
Me.CartonID_Tb.Value = ""
Exit Sub
End If
' Do something if passed the 2 conditions
End Sub
もしあなたが個々のキーを傍受したいなら(実際にはそれらが最初から入力されるのを防ぐ)ためには 'txtPalletID_KeyDown'、フォーカスが別のコントロールに切り替わったときに内容を検証する' txtPalletID_Exit'テキストボックスにフォーカスを戻す)。そうすれば、すべてのフィールドが有効であることがわかるまで、「Agregar」ボタンを無効にすることができます...しかし、それは単一のSOの質問のためのかなり広いテーマです。何かを試して、つまって、*特定の*問題=)に戻ってくる –
私はそれを試してみましょうおかげで:) – Deluq
@Deluq私の答えとコードを読む –