Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I check a generated number against a database table row?

Status
Not open for further replies.

toksoladuti

IS-IT--Management
Apr 11, 2001
62
GB
I'm very new to visual basic 2008 and could do with a helping hand. I've created a small windows form app with a Combobox (dropdownlist) where users can select a pre-entered productgroup. There is a TextBox where they can enter a quantity, a Label for results and finally a generate Button.
At the moment, I've generated the code below, which allows me select a product, click the button and it prints a random generated productcode to the Label. I'd actually like to click the button and it create as many random product codes as the number entered in the quantity TextBox. I'd also want the numbers to unique and thus the app reference a row called "productcode" in a database table called "product". Any pointers or help would be greatly appreciated.

Current code:

Public Class Form1

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
End
End Sub

Private Sub selectComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles selectComboBox.SelectedIndexChanged

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub generateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles generateButton.Click

Dim pcprefix As Integer
Dim qty As Integer
qty = quantityTextBox.Text

Select Case selectComboBox.Text
Case "01 Adhesives"
pcprefix = 20100000 + (New Random).Next(10000, 99999)
Case "02 Brackets"
pcprefix = 20200000 + (New Random).Next(10000, 99999)
Case "03 Bolts"
pcprefix = 20300000 + (New Random).Next(10000, 99999)
Case "04 Chipboard"
pcprefix = 20400000 + (New Random).Next(10000, 99999)
Case "05 Contiboard"
pcprefix = 20500000 + (New Random).Next(10000, 99999)
Case "06 Cupboard Doors"
pcprefix = 20600000 + (New Random).Next(10000, 99999)
Case "07 Cut To Size"
pcprefix = 20700000 + (New Random).Next(10000, 99999)
Case "08 Decorative Boards"
pcprefix = 20800000 + (New Random).Next(10000, 99999)
Case "09 Decorators Aids"
pcprefix = 20900000 + (New Random).Next(10000, 99999)
Case "10 Doors"
pcprefix = 21000000 + (New Random).Next(10000, 99999)
End Select

resultLabel.Text = pcprefix

End Sub

Private Sub resultLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles resultLabel.Click

End Sub
End Class

Thanks.
 
You can put the whole thing in a loop to get entries for multiple quantities. Something like this.

Code:
Private Sub generateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles generateButton.Click
  Dim pcprefix As Integer
  Dim strResults As String = Nothing

  If Not IsNumeric(quantityTextBox.Text) AndAlso CInt(quantityTextBox.Text) > 0 Then

    MessageBox.Show("Quantity must be a number")

  Else

    For iCnt As Integer = 1 To CInt(quantityTextBox.Text)
      Select Case selectComboBox.Text
      ...           
      End Select

      If IsNothing(strResults) Then
        strResults = pcprefix.ToString
      Else
        strResults &= vbCrLf & pcprefix
      End If

      resultLabel.Text = strResults

    Next
  End If
End Sub


That being said, displaying the results in a label probably isn't the best solution. You're going to end up writing a procedure to parse through the label text to get that info back when you need it for more processing. Also, the data validation for the quantity should be a function of the QuantityTextBox rather than in the button's click event.

Since you're going to be putting this into a database, using a datatable to hold the data and a datagridview to display it might be the way to go. Look into the Forms Over Data videos on Microsoft's website. They're very informative and will most likely give you a lot of ideas that will help with this kind of project. Here's a link to the site:

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top