Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Database and Reporting > Check for duplicate values Dynamically?


Reply
 
Thread Tools Display Modes
  #1  
Old 08-16-2001, 11:00 AM
The Cleaner
Guest
 
Posts: n/a
Question Check for duplicate values Dynamically?


Hi, I'm working with Access and I want a function that will check a specific column in a specific table for duplicates. Is there one built into Visual Basic? If so, what is it and how do you use it?

I have made my own, but the only part that doesn't work is the part in bold below. I want to be able to make a call to it by using a Form, Table, Specific Column in the table, and a control on the Form as arguements.

The column name is the part in bold below and is the only thing that doesn't work. If I manually type the Column name into the below function, i.e. replace the bold part below with Me!LastName (where "LastName" is the column to be searched for in the specified table) then everything works fine. I just don't want to have to type it in everytime into the function itself. I want to be able to pass the desired search column as a function arguement.

An example of how I want the function call to work would be:
<pre>
'Arguement Discription Type
'-------------------------------------------------------------------------------------
'Me - Form to be searched ('Me' for current) (form)
'"CommonNames" - Actual name of table to be searched (string)
'"LNAME" - Name of column in field to be searched (string)
'txtCommon - Name of control on form that holds the text to check (control)

If Duplicate(Me, "CommonNames", "LNAME", txtCommon) Then
MsgBox "Common Name"
Else
MsgBox "Not a very common name"
End If



</pre>

I have tried to comment everything as well as possible.
I attached the original function for anybody who wants to edit it and return it to me.
Any and all help is appreciated!

<pre>


Function Duplicate(SForm As Form, STable As String, SCol As String, SBox As Control) As Boolean

'FUNCTION OPERATION:
'Search any table field, in any table, in any form, for any duplicates. Return True if one is found, otherwise return False

'ARUGEMENTS for Duplicate Function:
'SForm As Form - Name of form to be searched (use 'Me' for current form)
'STable As String - Name of the Table to search (form's record source)
'SCol As String - Name of the column in the table to search for duplicate
'SBox As Control - Name of field on form that holds possible duplicate


If IsNull(SBox.Value) Then: Duplicate = False: Exit Function

Dim Name As String: Name = SBox.Value
Dim SBoxName As String: SBoxName = SBox.Name

Dim strOriginalRecordSource As String 'store the original RS of form
strOriginalRecordSource = SForm.RecordSource

Dim strOriginalControlSource As String 'store the original RS of control
strOriginalControlSource = SBox.ControlSource

SForm.RecordSource = STable 'change desired table
SBox.ControlSource = SCol 'change record sources

DoCmd.GoToControl SBoxName
DoCmd.FindRecord Name, acEntire



If Me!SCol = Name Then
Duplicate = True
Else
Duplicate = False
End If


SForm.RecordSource = strOriginalRecordSource
SBox.ControlSource = strOriginalControlSource

End Function
</pre>

Reply With Quote
  #2  
Old 08-16-2001, 11:19 AM
Thinker Thinker is offline
Iron-Fisted Programmer

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
Default Re: Check for duplicate values Dynamically?

Instead of Me!SCol try Me.Recordset.Fields(SCol).Value

I think therefore I am... sometimes right. [img]images/icons/wink.gif[/img]
__________________
Posting Guidelines
Reply With Quote
  #3  
Old 08-16-2001, 11:35 AM
anhmytran anhmytran is offline
Senior Contributor

Retired Moderator
* Guru *
 
Join Date: Aug 1999
Location: Hartford, Connecticut, 06
Posts: 1,487
Default Re: Check for duplicate values Dynamically?

Dim strVariable As String
strVariable = frmYourForm.txtYourText.Text

Create an instance of the Recordset object.
Set its source property to "SELECT Count(yourField) AS Counter FROM YourTable " & _
WHERE yourField = '" & strVariable & "';"
Open it.

bolDuplicate = False
If YourRecordset!Counter > 1 Then bolDuplicate = True

AnhMy_Tran<P ID="edit"><FONT class="small"><EM>Edited by anhmytran on 08/16/01 12:39 PM.</EM></FONT></P>
Reply With Quote
  #4  
Old 08-16-2001, 12:22 PM
The Cleaner
Guest
 
Posts: n/a
Default Re: Check for duplicate values Dynamically?

Could you show me how to actually program those few lines that you just mentioned. I'm having some probs with it. I don't change recordsets like this often

Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->