Barcode Len in textbox

johnsonk

Board Regular
Joined
Feb 4, 2019
Messages
172
Hi,

I am having problems trying to get the below to work, when I scan the barcode it stops after the first digit I have tried other subs with no joy can anyone help. I just want it to show error msg if the length of the barcode is wrong.

VBA Code:
Private Sub textbox2_Change()
Dim erow As Long
Application.DisplayAlerts = False
If Len(TextBox2.Value) < 46 Then
MsgBox "Too short!"
Else
If Len(TextBox2.Value) > 46 Then
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Cells(erow, 2) = TextBox1.Text
Cells(erow, 3) = TextBox2.Text
Cells(erow, 7) = TextBox3.Text
Cells(erow, 8) = TextBox4.Text
Range("A" & Rows.Count).End(xlUp).Offset(1).Value = Date
Cells(erow, 4).FormulaR1C1 = "=mid(rc[-1],4,4)"
Cells(erow, 5).FormulaR1C1 = "=mid(rc[-2],8,4)"
Cells(erow, 6).FormulaR1C1 = "=mid(rc[-3],30,6)"
TextBox2.Text = ""
TextBox3.Text = ""
TextBox2.Visible = True
TextBox4.Text = ""
TextBox1.Visible = True
TextBox3.Visible = False
TextBox4.Visible = False
Label3.Visible = False
Label4.Visible = False
Label5.Visible = False
Label6.Visible = False
Range("A1").End(xlDown).Select
Application.DisplayAlerts = True
TextBox2.SetFocus
End If
End If
End Sub

Regards
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Replace your event change for this.
Configure your scanner to send an enter after capturing.

VBA Code:
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  Dim erow As Long
  Application.DisplayAlerts = False
  If Len(TextBox2.Value) <= 46 Then
    MsgBox "Too short!"
    Cancel = True
  Else
    erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    Cells(erow, 2) = TextBox1.Text
    Cells(erow, 3) = TextBox2.Text
    Cells(erow, 7) = TextBox3.Text
    Cells(erow, 8) = TextBox4.Text
    Range("A" & Rows.Count).End(xlUp).Offset(1).Value = Date
    Cells(erow, 4).FormulaR1C1 = "=mid(rc[-1],4,4)"
    Cells(erow, 5).FormulaR1C1 = "=mid(rc[-2],8,4)"
    Cells(erow, 6).FormulaR1C1 = "=mid(rc[-3],30,6)"
    TextBox2.Text = ""
    TextBox3.Text = ""
    TextBox2.Visible = True
    TextBox4.Text = ""
    TextBox1.Visible = True
    TextBox3.Visible = False
    TextBox4.Visible = False
    Label3.Visible = False
    Label4.Visible = False
    Label5.Visible = False
    Label6.Visible = False
    Range("A1").End(xlDown).Select
    Application.DisplayAlerts = True
    Cancel = False
  End If
End Sub
 
Upvote 0
Hi DanteAmor,

When my userform opens the msgbox pops up then I get an error when I debug it highlights the textbox1.setfocus in the userform activate below is the full code for the userform
1593523264968.png


Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim erow As Long
Application.DisplayAlerts = False
If Len(TextBox2.Value) <= 46 Then
MsgBox "Too short!"
Cancel = True
Else
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Cells(erow, 2) = TextBox1.Text
Cells(erow, 3) = TextBox2.Text
Cells(erow, 7) = TextBox3.Text
Cells(erow, 8) = TextBox4.Text
Range("A" & Rows.Count).End(xlUp).Offset(1).Value = Date
Cells(erow, 4).FormulaR1C1 = "=mid(rc[-1],4,4)"
Cells(erow, 5).FormulaR1C1 = "=mid(rc[-2],8,4)"
Cells(erow, 6).FormulaR1C1 = "=mid(rc[-3],30,6)"
TextBox2.Text = ""
TextBox3.Text = ""
TextBox2.Visible = True
TextBox4.Text = ""
TextBox1.Visible = True
TextBox3.Visible = False
TextBox4.Visible = False
Label3.Visible = False
Label4.Visible = False
Label5.Visible = False
Label6.Visible = False
Range("A1").End(xlDown).Select
Application.DisplayAlerts = True
Cancel = False
End If
End Sub

Private Sub commandbutton1_Click()
Dim erow As Long
Application.DisplayAlerts = False
If Len(TextBox4.Value) < 6 Then
TextBox4.Text = ""
TextBox4.SetFocus
MsgBox "Please Enter 6 Digits For Weight example( 002500 )"
Else
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Cells(erow, 2) = TextBox1.Text
Cells(erow, 7) = TextBox3.Text
Cells(erow, 6) = TextBox4.Text
Range("A" & Rows.Count).End(xlUp).Offset(1).Value = Now
TextBox3.Text = ""
TextBox4.Text = ""
TextBox1.Visible = True
TextBox2.Visible = False
TextBox3.Visible = True
TextBox4.Visible = True
Label3.Visible = True
Label4.Visible = True
Label5.Visible = True
Label6.Visible = True
Range("A1").End(xlDown).Select
TextBox1.Visible = True
TextBox2.Visible = True
TextBox3.Visible = False
TextBox4.Visible = False
Label3.Visible = False
Label4.Visible = False
Label5.Visible = False
Label6.Visible = False
Application.DisplayAlerts = True
TextBox2.SetFocus
End If
End Sub

Private Sub CommandButton2_Click()
Unload UserForm1
ActiveWorkbook.Save
Application.Quit
End Sub

Private Sub CommandButton3_Click()
Login.Show
Unload UserForm1
End Sub

Private Sub CommandButton4_Click()
Unload UserForm1
ActiveWindow.DisplayWorkbookTabs = False
Sheet2.Activate
End Sub

Private Sub CommandButton5_Click()
TextBox2.Visible = False
Label2.Visible = False
TextBox3.Visible = True
TextBox4.Visible = True
Label3.Visible = True
Label4.Visible = True
Label5.Visible = True
Label6.Visible = True
TextBox3.SetFocus
End Sub

Private Sub UserForm_Activate()
Me.StartUpPosition = 0
Me.Top = Application.Top + 5
Me.Left = Application.Left + Application.Width - Me.Width - 5
TextBox3.Visible = False
TextBox4.Visible = False
Label3.Visible = False
Label4.Visible = False
Label5.Visible = False
Label6.Visible = False
TextBox1.SetFocus
End Sub

Private Sub UserForm_QueryClose _
(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
MsgBox "Clicking the Close button does not work."
Cancel = True
End If
End Sub

Regards
 
Upvote 0
Change the TabIndex property of Textbox1 to 0 and the property of Textbox2 to 1
 
Upvote 0

Forum statistics

Threads
1,214,660
Messages
6,120,787
Members
448,994
Latest member
rohitsomani

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top