-
Mar 3rd, 2012, 12:12 PM
#1
Thread Starter
New Member
How to make textbox generate random account number for database
I can't figure out how to code this.
What I want is for a text box to generate a random number for an account number database field every time i click the new button. The text box will search the db and if it can't find the random number there, it will paste it in the accountnumbertextbox to be saved after clicking a save button.
So far, here's my code.
Code:
Private Sub btnnew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnew.Click
Do Until Not Me.txthiddensearch.Text = AccountNumberTextBox.Text
Randomize()
Me.txthiddensearch.Text = (Str(Int(Rnd() * 3)))
Me.CustomersBindingSource.Filter = "AccountNumber = '" & txthiddensearch.Text & " ' "
Loop
Me.CustomersBindingSource.AddNew()
AccountNumberTextBox.Text = Me.txthiddensearch.Text
End Sub
Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
Me.Validate()
Me.CustomersBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(UsersDataSet)
Me.txthiddensearch.Clear()
End Sub
I'll be honest I'm confused with how the loop is supposed to work.
-
Mar 3rd, 2012, 08:53 PM
#2
Re: How to make textbox generate random account number for database
TextBoxes don't generate random numbers and TextBoxes don't search databases. All TextBoxes do is display text and allow the user to enter text.
I don't really understand why you would want a random account number in the first place. Would it not make more sense to use sequential account numbers? Anyway, if you are going to use Randomize and Rnd then you do NOT call Randomize multiple times. Call Randomize once and once only, at application startup. That said, don't use Randomize and Rnd at all. Use the Random class. Create one instance and call its Next method each time you want a random number.
Further to that, you say that you want to search the database but then you are filtering a BindingSource. They are NOT the same thing. Either you want to search the database or you want to search data that you have already retrieved from the database. Which is it?
-
Mar 3rd, 2012, 10:50 PM
#3
Thread Starter
New Member
Re: How to make textbox generate random account number for database
Hmmm.. I don't think I explained that one very well.. Sorry.. So here I go..
I'm trying to make a customers database. Whenever I click new on the main form, a second form pops out where you'll have to fill in details. My primary key is AccountID, not AccountNumber. I decided to do it that way so that I can reuse an account number after deleting an account id (which has an auto increment) which used to have the reused account number. I need the account number so that I can search the database for a particular account to bill. Last names have the tendency to be the same, the account number unique.
I'm sorry, I didn't really mean the textbox itself would generate a random number. If you check my code, the randomize command actually comes after clicking btnnew. So what I really meant was how to make my AccountNumberTextBox generate a random number (after clicking btnnew) without repeating the ones already found in a database.
I actually do the search using:
Code:
Me.TABLENAMEBindingSource.Filter = "TABLECOLUMNNAME = '" & TEXTBOXNAME & " ' "
I'm thinking I need it so that after clicking btnnew, it will generate a random number, search the database if that number already exists in the AccountNumber column, if yes it will generate another number, if not, it will stop searching and leave the randomly generated number in the AccountNumberTextBox.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|