Randomly generating number take 2

Posted on 2003-12-10
Last Modified: 2010-05-01
Hi i have managed to generate a three digit number randomly and populate it to a textbox. My application connects  to a DB and populates a listview according to whats in the database. What i need to do now is to make sure that the randomly generated number doesnt already exist in the database. So basically i have text fields that let users enter clientID, firstname, lastname etc, from text boxes and upon pressing the save button, saves it to the database. So when  i press the add button, my ClientID text field populates with a randomly generated 3 digit number, i just need to make sure it doesnt generate a number or client ID, already in the database....
Question by:intox1221
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
LVL 27

Expert Comment

ID: 9915274
Hi intox1221:
Sounds like an easy one to me.
To be able to answer, though, you should give more information about your database:
MSAccess? SQL?
Is the clientID field indexed? Is it the PrimaryKey?


Author Comment

ID: 9915528
it is the primary key. It is indexed to yes (no duplicates).

These r my connection details. this is the top level class variable in the form.
Dim cstrConnection As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" _
        & System.AppDomain.CurrentDomain.BaseDirectory() & "MailingList.mdb"

I have  Dbtier class set up which handles all the connections. A part of the code is as follows.

    Public Shared Function ReturnData(ByVal pstrConnection As String, ByVal pstrSQL As String) As DataSet
        ' This procedure returns a dataset containing
        ' all records from the passed-in query. The
        ' connection string is needed to connect to
        ' the database.

        'declare the DB connection, data adapter, and data set objects
        Dim conConnection As OleDbConnection
        Dim daAdapter As OleDbDataAdapter
        Dim dsData As DataSet


            'open the connection
            conConnection = New OleDbConnection(pstrConnection)

            'create the data adapter (this manages the data) based on the query
            daAdapter = New OleDbDataAdapter(pstrSQL, conConnection)

            'create the data set and fill it from the data adapter
            dsData = New DataSet

            'close the connection
LVL 27

Expert Comment

ID: 9916014
It does help to know that you are using .NET and an .mdb file

What you need to use is the "Find" function

Just type dsData.Tables(0).Find and press F1 for help.
If the help is not clear to you, then please let me know, and I will guide you through it.

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 27

Expert Comment

ID: 9916022

Author Comment

ID: 9916030
i will take a look at help like u suggest, it always helps. But i would rather u guide me where i need to go... thanks for ur time though....
LVL 27

Expert Comment

ID: 9916118
Something like this:

      Dim dt As DataTable = dsData.Tables(0)
      Dim dc(0) As DataColumn
      dc(0) = dt.Columns("clientId")
      dt.PrimaryKey = dc
      Dim keys(0) As String
      keys(0) = txtClientID.Text

Author Comment

ID: 9916239
what do i declare dsdata as and where. Also does it have to be local or class level variable.
LVL 27

Accepted Solution

Dabas earned 500 total points
ID: 9916292
I just took the variable as appears in your code!

Insert my code after the daAdapter.Fill(dsData) line

LVL 27

Expert Comment

ID: 10270351
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
No response from intox1221 from 12/10/2003 comment
Award points to Dabas is recommend.
Please leave any comments here within the next seven days.

EE Cleanup Volunteer

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question