Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Flexgrid to Table - Compare contents 1

Status
Not open for further replies.

LiLgUrL

Technical User
Feb 9, 2003
59
0
0
CA
Here's my code...

Set rs = New ADODB.Recordset
With rs
.Open "Select * from tblPartNumber", conn, adOpenForwardOnly, adLockPessimistic

For i = 1 To flgPartNum.Rows - 2
If !PartNumber = flgPartNum.TextMatrix(i, 0) Then

!PartNumber = flgPartNum.TextMatrix(i, 0)
!GenDesc = flgPartNum.TextMatrix(i, 2)
!PartDesc = flgPartNum.TextMatrix(i, 3)
End If

If !PartNumber <> flgPartNum.TextMatrix(i, 0) Then
rs.AddNew
!PartNumber = flgPartNum.TextMatrix(i, 0)
!GenDesc = flgPartNum.TextMatrix(i, 2)
!PartDesc = flgPartNum.TextMatrix(i, 3)
End If
Next
rs.Update
rs.MoveNext
End With


I want import the contents of the Flexgrid to my table tblPartNumber ... but with some consideration... if from the flexgrid the partnumber is already existing(in tblPartNumber) then it will edit the record and if not existing will add new record... i think there's a problem with my condition... please advice
 
LiLgUrL,

Here's the way I would do it:

' start code
Set rs = New ADODB.Recordset

With rs
.Open "Select * from tblPartNumber", conn, adOpenDynamic, adLockPessimistic

For i = 1 To flgPartNum.Rows - 2
rs.Find "PartNumber = '" & flgPartNum.TextMatrix(i, 0) & "'"
If rs.EOF Then
rs.AddNew
End If

!PartNumber = flgPartNum.TextMatrix(i, 0)
!GenDesc = flgPartNum.TextMatrix(i, 2)
!PartDesc = flgPartNum.TextMatrix(i, 3)

rs.Update

Next
rs.Close

End With

' end code

version of ADO I used is 2.7.
-GS


 
hi garths2 thanks for the prompt reply i tried your code... but just like mine when the PartNumber is existing I receive an error "The changes you requested to the table were not successful because they would create duplicate vaules in the index..." i can't figure where the error lies... because at any angle i think the logic is correct. Please help... i am running out of time with this... been doing this for a long time now :(
 
oh i think i got it...
Code:
' start code
Set rs = New ADODB.Recordset

With rs
    .Open "Select * from tblPartNumber", conn, adOpenDynamic, adLockPessimistic

For i = 1 To flgPartNum.Rows - 2
    rs.Find "PartNumber = '" & flgPartNum.TextMatrix(i, 0) & "'"
    If rs.EOF Then
        rs.AddNew
        !PartNumber = flgPartNum.TextMatrix(i, 0)
    End If

    !GenDesc = flgPartNum.TextMatrix(i, 2)
    !PartDesc = flgPartNum.TextMatrix(i, 3)

    rs.Update

Next
rs.Close

End With

' end code

thanks garths2! Mmmmmwha!
 
LiLgUrL,

Good catch! I didn't even notice you were trying to update the primary key. When I set up an example I didn't use a primary key so I didn't get an error. Glad you found it.

-GS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top