How to make textbox generate random account number for database-VBForums
Results 1 to 3 of 3

Thread: How to make textbox generate random account number for database

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Location
    Legazpi
    Posts
    8

    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.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    87,325

    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?

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Location
    Legazpi
    Posts
    8

    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.