Solved

Randomly generating number take 2

Posted on 2003-12-10
9
243 Views
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....
0
Comment
Question by:intox1221
  • 5
  • 3
9 Comments
 
LVL 27

Expert Comment

by:Dabas
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?

Dabas
0
 

Author Comment

by:intox1221
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

        Try

            '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
            daAdapter.Fill(dsData)

            'close the connection
            conConnection.Close()
0
 
LVL 27

Expert Comment

by:Dabas
ID: 9916014
intox1221:
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.

Dabas
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 27

Expert Comment

by:Dabas
ID: 9916022
0
 

Author Comment

by:intox1221
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....
0
 
LVL 27

Expert Comment

by:Dabas
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
      dt.Rows.Find(keys)
0
 

Author Comment

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

Accepted Solution

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

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

Dabas
0
 
LVL 27

Expert Comment

by:planocz
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.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

planocz
EE Cleanup Volunteer
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

770 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