需要保存一对多关系VB6

时间:2021-07-22 07:43:40

So I have to save from a grid where there are two types of IDs. LineId & CustId. There can be multiple CustIds for each LineId. An example of data would be like this:

所以我必须从有两种ID的网格中保存。 LineId和CustId。每个LineId可以有多个CustId。数据的一个例子是这样的:

LineId     CustId
1          33
2          98
7          101
1          51
3          28
7          02
1          35        

I need to save the code with a save procedure that accepts a null-delimited string of CustIds for each Line id. I call the save procedure once for each LineId being saved. I cannot change how the save procedure works.

我需要使用保存过程保存代码,该过程接受每个行ID的以空分隔的CustIds字符串。我为保存的每个LineId调用一次保存过程。我无法改变保存程序的工作方式。

So far I have been adding the grids to a type array with line id and cust id.

到目前为止,我一直在将网格添加到具有行ID和cust id的类型数组中。

Dim typeRows(gridRows - 1) As Ids 'gridRows is rowcount of grid
For i = 0 To grid.Rows - 1
    typeRows(i).LineId = grid.TextMatrix(i, GridColumns.colLineId)
    typeRows(i).CustId = grid.TextMatrix(i, GridColumns.colCustId)
Next

But I'm a little stuck on what I should do next. Should I sort the grid? Then how would I go through the sorted grid and combine cust ids for each lineid?

但我对下一步应该做的事情有点困惑。我应该对网格进行排序吗?然后,我将如何通过排序的网格并为每个lineid组合custid?

Any help or guidance would be very appreciated.

任何帮助或指导将非常感激。

1 个解决方案

#1


2  

without sorting the grid you have to loop through the array as well to check if the id was already found before, that can be done as well, but might take some time depending on the size of your data

如果没有排序网格,你必须循环遍历数组,以检查之前是否已找到id,也可以这样做,但可能需要一些时间,具体取决于数据的大小

'1 form with :
'    1 flexgrid control : name=grid
Option Explicit

Private Type Ids
  LineId As String
  CustId As String
End Type

Private Sub Form_Click()
  'export to delimited string
  Dim intRow As Integer
  Dim intId As Integer
  Dim intBound As Integer
  Dim udtIds() As Ids
  Dim blnThere As Boolean
  Dim intCount As Integer
  With grid
    intBound = .Rows - 1
    ReDim udtIds(intBound) As Ids
    intCount = 0
    For intRow = 1 To intBound
      'loop through all rows
      blnThere = False
      For intId = 0 To intBound
        'check if lineid was already found before
        If udtIds(intId).LineId = .TextMatrix(intRow, 0) Then
          blnThere = True
          Exit For
        End If
      Next intId
      If blnThere Then
        'if lineid was already found before, just add the custid
        udtIds(intId).CustId = udtIds(intId).CustId & "," & .TextMatrix(intRow, 1)
      Else
        'if lineid is new, then create new lineid with custid in udt
        udtIds(intCount).LineId = .TextMatrix(intRow, 0)
        udtIds(intCount).CustId = .TextMatrix(intRow, 1)
        intCount = intCount + 1
      End If
    Next intRow
  End With 'grid
  'now you can save the udt
End Sub

Private Sub Form_Load()
  With grid
    'fill grid with example values
    .Cols = 2
    .Rows = 8
    .FixedRows = 1
    .FixedCols = 0
    .TextMatrix(0, 0) = "LineId"
    .TextMatrix(0, 1) = "CustId"
    .TextMatrix(1, 0) = "1"
    .TextMatrix(2, 0) = "2"
    .TextMatrix(3, 0) = "7"
    .TextMatrix(4, 0) = "1"
    .TextMatrix(5, 0) = "3"
    .TextMatrix(6, 0) = "7"
    .TextMatrix(7, 0) = "1"
    .TextMatrix(1, 1) = "33"
    .TextMatrix(2, 1) = "98"
    .TextMatrix(3, 1) = "101"
    .TextMatrix(4, 1) = "51"
    .TextMatrix(5, 1) = "28"
    .TextMatrix(6, 1) = "02"
    .TextMatrix(7, 1) = "35"
  End With 'grid
End Sub

#1


2  

without sorting the grid you have to loop through the array as well to check if the id was already found before, that can be done as well, but might take some time depending on the size of your data

如果没有排序网格,你必须循环遍历数组,以检查之前是否已找到id,也可以这样做,但可能需要一些时间,具体取决于数据的大小

'1 form with :
'    1 flexgrid control : name=grid
Option Explicit

Private Type Ids
  LineId As String
  CustId As String
End Type

Private Sub Form_Click()
  'export to delimited string
  Dim intRow As Integer
  Dim intId As Integer
  Dim intBound As Integer
  Dim udtIds() As Ids
  Dim blnThere As Boolean
  Dim intCount As Integer
  With grid
    intBound = .Rows - 1
    ReDim udtIds(intBound) As Ids
    intCount = 0
    For intRow = 1 To intBound
      'loop through all rows
      blnThere = False
      For intId = 0 To intBound
        'check if lineid was already found before
        If udtIds(intId).LineId = .TextMatrix(intRow, 0) Then
          blnThere = True
          Exit For
        End If
      Next intId
      If blnThere Then
        'if lineid was already found before, just add the custid
        udtIds(intId).CustId = udtIds(intId).CustId & "," & .TextMatrix(intRow, 1)
      Else
        'if lineid is new, then create new lineid with custid in udt
        udtIds(intCount).LineId = .TextMatrix(intRow, 0)
        udtIds(intCount).CustId = .TextMatrix(intRow, 1)
        intCount = intCount + 1
      End If
    Next intRow
  End With 'grid
  'now you can save the udt
End Sub

Private Sub Form_Load()
  With grid
    'fill grid with example values
    .Cols = 2
    .Rows = 8
    .FixedRows = 1
    .FixedCols = 0
    .TextMatrix(0, 0) = "LineId"
    .TextMatrix(0, 1) = "CustId"
    .TextMatrix(1, 0) = "1"
    .TextMatrix(2, 0) = "2"
    .TextMatrix(3, 0) = "7"
    .TextMatrix(4, 0) = "1"
    .TextMatrix(5, 0) = "3"
    .TextMatrix(6, 0) = "7"
    .TextMatrix(7, 0) = "1"
    .TextMatrix(1, 1) = "33"
    .TextMatrix(2, 1) = "98"
    .TextMatrix(3, 1) = "101"
    .TextMatrix(4, 1) = "51"
    .TextMatrix(5, 1) = "28"
    .TextMatrix(6, 1) = "02"
    .TextMatrix(7, 1) = "35"
  End With 'grid
End Sub