Hi, I'm building an application that reads barcode scanning data into multiple forms and textboxes. At the moment, to handle that data I have created a large nested if statement that checks to see which object has focus so that the scanned data is placed there and focus moves down to the next object:

Private Sub HandleData(ByVal TheReaderData As Symbol.Barcode.ReaderData)

        'Scanning Data into frmGetCount
        If frmName.Name = "frmGetCount" Then
            If frmGetCount.txtGetCountItemNo.Focused = True Then
                frmGetCount.txtGetCountItemNo.Text = TheReaderData.Text
                If frmGetCount.txtGetCountItemNo.TextLength <> 20 Then
                    MessageBox.Show("Sorry, you did not scan an Item No. Please try again.")
                    frmGetCount.txtGetCountItemNo.Text = ""
                    frmGetCount.txtGetCountItemNo.Focus()
                Else
                    frmGetCount.txtGetCountVariantCode.Focus()
                End If
            ElseIf frmGetCount.txtGetCountVariantCode.Focused = True Then
                frmGetCount.txtGetCountVariantCode.Text = TheReaderData.Text
                If frmGetCount.txtGetCountVariantCode.TextLength <> 10 Then
                    MessageBox.Show("Sorry, you did not scan a Variant Code, please try again.")
                    frmGetCount.txtGetCountVariantCode.Text = ""
                    frmGetCount.txtGetCountVariantCode.Focus()
                End If
            End If
End Sub

This is just for one of 5 forms I am currently developing. I am looking for a way to make this code much more dynamic and re-usable by removing the hard-coded objects name. Does anyone know how this might be done?

Recommended Answers

All 2 Replies

Hi, I'm building an application that reads barcode scanning data into multiple forms and textboxes. At the moment, to handle that data I have created a large nested if statement that checks to see which object has focus so that the scanned data is placed there and focus moves down to the next object:

Private Sub HandleData(ByVal TheReaderData As Symbol.Barcode.ReaderData)

        'Scanning Data into frmGetCount
        If frmName.Name = "frmGetCount" Then
            If frmGetCount.txtGetCountItemNo.Focused = True Then
                frmGetCount.txtGetCountItemNo.Text = TheReaderData.Text
                If frmGetCount.txtGetCountItemNo.TextLength <> 20 Then
                    MessageBox.Show("Sorry, you did not scan an Item No. Please try again.")
                    frmGetCount.txtGetCountItemNo.Text = ""
                    frmGetCount.txtGetCountItemNo.Focus()
                Else
                    frmGetCount.txtGetCountVariantCode.Focus()
                End If
            ElseIf frmGetCount.txtGetCountVariantCode.Focused = True Then
                frmGetCount.txtGetCountVariantCode.Text = TheReaderData.Text
                If frmGetCount.txtGetCountVariantCode.TextLength <> 10 Then
                    MessageBox.Show("Sorry, you did not scan a Variant Code, please try again.")
                    frmGetCount.txtGetCountVariantCode.Text = ""
                    frmGetCount.txtGetCountVariantCode.Focus()
                End If
            End If
End Sub

This is just for one of 5 forms I am currently developing. I am looking for a way to make this code much more dynamic and re-usable by removing the hard-coded objects name. Does anyone know how this might be done?

The one thing I can think of that will save you from having to write this same code 5 times is to create a function that will receive the form name. Then you can substitute the "frmGetCount" (form name) with the variable that holds the name of the form (passed to the function).

If I get what you mean?

Actually, "frmName" does contain the name of the current form. However each form has different textboxes and names, so the code has to change for each of them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.