Results 1 to 5 of 5

Thread: limit records seached in a DB

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    Georgia
    Posts
    337

    limit records seached in a DB

    I have a form that the user creates a new record in the DB
    He types a Claimnumber in a box then retypes in a second box and I check that they match so I know he hasn't made a typo. However I also must check that the Claimnumber isn't already in the db. Here is the partial code when he clicks a cmdbutton.

    VB Code:
    1. Dim cn As ADODB.Connection
    2. Dim rs As ADODB.Recordset
    3. Set cn = New ADODB.Connection
    4. Set rs = New ADODB.Recordset
    5. cn.ConnectionString = "DSN=eDoan;UID=sa"
    6. cn.Open
    7.  rs.Open "Select ClaimNo from tblClaim Where ClaimNo = '" & txtRetypeClaimNo.Text & "'", cn, adOpenDynamic, adLockPessimistic
    8.    If rs.EOF And rs.BOF And txtFields(1) = txtRetypeClaimNo Then
    9.  
    10. go on with the process
    11.  
    12. Else
    13.  
    14. MsgBox etc

    It works just fine with no problems. However as the DB grows it will take longer and longer to check for no duplicate Claimnumber. My question is can I somehow code this to only check the last 4000 records in the DB ?

    Thanks for any help

  2. #2
    Hyperactive Member goatsucker's Avatar
    Join Date
    Dec 2002
    Location
    Leeds, England
    Posts
    283
    I'm not usre about limiting the number of records searched, but I may have an alternative solution:

    Presumably sooner or later the claim number entered is entered into the tblClaim table? If ClaimNo has a unique index on it (And if it is the primary key of that table, then it should have), this INSERT will fail. Yopu can trap the error message returned by the INSERT and tell the user that this claim has already been entered. You do not need to check explicitly that it has been already entered, if you wait unitl the insert fails.
    After all "Rust Never Sleeps"

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    Georgia
    Posts
    337
    Thanks for the help but I can't do it that way for several reasons but your right the Claim number gets put in tblClaim right after

    THEN

    here is part of what I do after the THEN

    Dim cn1 As ADODB.Connection
    Dim rsMetrix As ADODB.Recordset

    Set cn1 = New ADODB.Connection
    cn1.ConnectionString = "DSN=eDoan;UID=sa"
    cn1.Open

    Set rsMetrix = New ADODB.Recordset
    With rsMetrix
    .Open "tblClaim", cn1, adOpenForwardOnly, adLockPessimistic
    .AddNew

    !CompanyName = DataCombo1.Text
    !ClaimNo = txtFields(1).Text
    !OwnerFirst = txtFields(2).Text
    !OwnerLast = txtFields(3).Text
    !DateRecd = txtDateRecd.Text
    !DispatcherName = DataCombo2.Text
    !CompanyID = txtCoID.Text

    .Update
    .Close


    Wish I could somehow use your suggestion but I have a Primary Key set on another column.

    But thanks alot for your help.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Actually, the best thing to do is put an Index on the field being search and leave it alone. That will ensure that it's at it's optimum. If you start limiting it, you end up having to run two searches, which will cause more degradation. -- The first being to limit the last X # of rows, and the second to see if that # is in that returned list. It's better if you just put an Index (a clustered one would be best) and leave the query alone.

    Another tip: When you open the recordset to add the record, do a "SELECT TOP 0 * FROM tblClaim" instead of the table itself. This will return an empty recordset with the field structure in it. That way you aren't returning the entire table (which will get larger as you go.)
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    Georgia
    Posts
    337
    Thanks techgnome

    I really appreciate the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width