tags:

views:

92

answers:

2

Hi i have a recordset name rcdDNE. I read the rtn, accno, first name, Middle name, last name, amount, from text file and store it to the recordset. Now I want to store that values to database table. In my table accno is primary key. So before storing that into my table i want to find out if there is any duplicate accno in my recordset. If i have i want to write it to text file.

Can anyone help me.

' Set up rcdDNE structure
With rcdDNE.Fields
    .Append "RTN", adVarChar, 9
    .Append "AccountNbr", adVarChar, 17
    .Append "IndividualName", adVarChar, 22
    .Append "FirstName", adVarChar, 50
    .Append "MiddleName", adVarChar, 1
    .Append "LastName", adVarChar, 50
    .Append "Amount", adCurrency
End With

rcdDNE.Open
intFileNbr = FreeFile(1)
Open strFileName For Input As #intFileNbr Len = 95 ' Open file for input.
Do While Not EOF(intFileNbr)
   Line Input #intFileNbr, strCurrentLine
   If Mid(strCurrentLine, 1, 1) = 6 Then
     strRoutingNbr = Mid(strCurrentLine, 4, 8)
     strAcct = Trim(Mid(strCurrentLine, 13, 17))
     strIndividualName = Trim(Mid(strCurrentLine, 55, 22))
     strAmount = Trim(Mid(strCurrentLine, 30, 10))
     strAmount = Left(strAmount, Len(strAmount) - 1)
     curAmount = CCur(strAmount)

   ' Add new record to temporary recordset
        With rcdDNE
            .AddNew
            .Fields![RTN] = strRoutingNbr
            .Fields![AccountNbr] = strAcct
            .Fields![IndividualName] = strIndividualName
            .Fields![Amount] = curAmount
            .Update
        End With
   End If
Loop

' Write records to Database

frmDNELoad.lblStatus.Caption = "Loading data into database......"
Dim lngRecCount As Long
lngRecCount = 0
rcdDNE.MoveFirst

 With cmdCommand
    .ActiveConnection = objConn
    .CommandText = "insert into t_DATA_DneFrc (RTN, AccountNbr, FirstName, MiddleName, LastName, Amount) values ('" & rcdDNE("RTN") & "', '" & rcdDNE("AccountNbr") & "', '" & rcdDNE("FirstName") & "', '" & rcdDNE("MiddleName") & "', '" & rcdDNE("LastName") & "', '" & rcdDNE("Amount") & "')"
    .CommandType = adCmdText
End With

Set rcddnefrc = New ADODB.Recordset
With rcddnefrc
    .ActiveConnection = objConn
    .Source = "SELECT * FROM T_DATA_DNEFRC"
    .CursorType = adOpenDynamic
    .CursorLocation = adUseClient
    .LockType = adLockOptimistic
    .Open
End With

Do Until rcdDNE.EOF
    lngRecCount = lngRecCount + 1
    frmDNELoad.lblStatus.Caption = "Adding record " & lngRecCount & " of " & rcdDNE.RecordCount & " to database."
    frmDNELoad.Refresh
    DoEvents
    Call CommitNew
    rcdDNE.MoveNext
Loop
A: 

In the loop, where reading the data from the text file, Build a list of accno.

Each time you read a line from the text, first check if the list contains the accno, if not, add the record, and add the accno to the list.

If it does contain the accno in the list already, dont add the line to the record set and move to the next line.

astander
can explain little bit more or if you have any sample code can you post that.
pbrp
how do you want me to build alist of acc no. Do i need to create another recordset or what? Sorry, I am new to vb6 and this coding. Thats why these many questions. Thanks In Advance
pbrp
@pbrp: Use Collection or Scripting.Dictionary.
Tomek Szpakowicz
Use an array to store the accno in, and to check if you have already stored the value. Try to get something similar to C# List<type> where you can add new entries and check if entries has already been added.http://visualbasic.freetutes.com/learn-vb6/lesson6.htmlhttp://www.codeguru.com/forum/showthread.php?t=469202So as you read the text line, and get the accno, check in this list/array you have if it is present, if not, add the new record, if so, dont add it.
astander
You don't want/need to create another recordset. You're writing you're duplicates to a file right? So start at the top of your recordset, grab the first record, compare that record to the other records. If it's a duplicate, write it to the file, then move to the next record. If it's not, insert it into the database, then move to the next record. Very straightforward.
Satanicpuppy
A: 

Meh. Duplicate checking sucks. You might be better off just dumping the whole dataset into the database, and then doing SELECT DISTINCT...INTO... and creating another table without duplicates. Then you can compare records in the two tables to find the duplicate records.

Otherwise you're going to have to pull the first record, check it against your ENTIRE dataset, pull the second record, etc. High cost to that kind of comparison.

Satanicpuppy