The program has a button named INCREMENT . The label that starts with 0 will increment every time the button is being pressed and save it in a database then so on. . .those being saved in the database will appear in MSFlexGrid.

Important part:
in MSFlexGrid
1
2
3
4
5
6

if the number 4 being deleted. . .it will look like this in MSFlexGrid:
1
2
3

5
6

if i press the button "RE-ARRANGE" it will look like this:
1
2
3
5
6

if i press the button "INCREMENT" it will not go directly to seven but instead the number 4 will be inserted:
1
2
3
5
6
4
press re-arrange:
1
2
3
4
5
6 and so on
7
8
9
10

this is a challenging one so pls help me. . i need to know how to do this. thank you so much.

Dani AI

Generated

Short summary and a robust way forward for : the task is to keep a sequence 1,2,3,... in the grid while allowing deletes, have a RE-ARRANGE that compacts the rows, and make INCREMENT insert the smallest missing number (fill gaps) instead of always appending the next-highest. Building on ’s array idea and ’s “call rearrange” hint, a database-centric solution is simpler, deterministic and scales better than juggling arrays in form memory.

To insert the smallest missing positive integer use a single SQL query (works for Access or SQL Server) and then insert that value. Example Access SQL to compute the missing value (returns 1 if table empty):

SELECT Nz(MIN(t1.num + 1), 1) AS Missing
FROM MyTable AS t1
LEFT JOIN MyTable AS t2
  ON (t1.num + 1) = t2.num
WHERE t2.num IS NULL;

A minimal VB6 + ADO pattern: execute the query, read Missing, then INSERT that number and refresh the MSFlexGrid (requery the recordset or reload the grid rows).

A safer, higher-performance alternative for production: maintain a small FreeNumbers table. On delete insert the freed number into FreeNumbers. On INCREMENT, if FreeNumbers has rows take the MIN(num) from it and move that value back into the main table; otherwise insert MAX(num)+1. This avoids scanning the whole main table each time and is easy to make atomic with BeginTrans/CommitTrans.

RE-ARRANGE (compact): if num is only a display sequence, renumber a separate display_order column (preferred). For SQL Server: use ROW_NUMBER() in a CTE to update display_order. For Access: open an ordered recordset, iterate and write sequential values inside a transaction. Important cautions: do not renumber a primary key that other tables reference without updating dependents; wrap read+insert operations in a transaction to avoid race conditions; always test changes on a copy of the database before applying to production.

Recommended Answers

All 5 Replies

Hi,

Try the below coding,

Option Base 1 'Set Index of Array start with 0 or 1Dim i As Integer
Dim b() As Integer ' Array Variable
Dim Rearrange As Boolean
'To Delete the Selected row in the gridPrivate Sub cmdDelete_Click()
msflxgrid.TextMatrix(msflxgrid.RowSel, 0) = ""
End Sub
'To Increment the values
Private Sub cmdIncrement_Click()
Dim m As Integer
'Add new row in the gridmsflxgrid.Rows = msflxgrid.Rows + 1
' If Reaarange is true then only it will go into the process otherwise it will increment the value continuouslyIf Rearrange = True Then
    'Loop process    For j = 1 To UBound(b)
        'Check whether J+1 is lesser than or equal to the b array size
        If j + 1 <= UBound(b) Then
            'Check whether Current and Next array value is greater than 1
            'If the value is greater than 1 then increment the current array value.            If Abs(b(j) - b(j + 1)) > 1 Then
                msflxgrid.TextMatrix(msflxgrid.Rows - 1, 0) = b(j) + 1
                m = 0
                Exit For
            Else
                m = 1
            End If
        End If
    Next
Else
    'Continuous Increment
    i = i + 1
    msflxgrid.TextMatrix(i, 0) = i
End If
If m = 1 Then
    'Continuous Increment    i = i + 1
    msflxgrid.TextMatrix(msflxgrid.Rows - 1, 0) = i
End If
m = 0
End Sub
'Re - Arrange the Values in the gridPrivate Sub cmdReArrange_Click()
Dim j, k, l As Integer
Erase b
l = 1
Rearrange = True
'Set the size of the ArrayReDim Preserve b(1)
'Store the values from the grid to Array variableFor j = 1 To msflxgrid.Rows - 1
    If msflxgrid.TextMatrix(j, 0) <> "" Then
        ReDim Preserve b(UBound(b) + 1)
        b(l) = msflxgrid.TextMatrix(j, 0)
        l = l + 1
    End If
Next
'Arrange the values in order
For j = 1 To UBound(b)
    For k = 1 To UBound(b)
        If b(k) > b(j) Then
          l = b(j)
          b(j) = b(k)
          b(k) = l
        End If
    Next
Next
'Finally assign the values to the gridmsflxgrid.Rows = 1
For j = 1 To UBound(b)
    If b(j) > 0 Then
        msflxgrid.Rows = msflxgrid.Rows + 1
        msflxgrid.TextMatrix(msflxgrid.Rows - 1, 0) = b(j)
    End If
Next
End Sub

Shailaja:)

The program has a button named INCREMENT . The label that starts with 0 will increment every time the button is being pressed and save it in a database then so on. . .those being saved in the database will appear in MSFlexGrid.

Important part:
in MSFlexGrid
1
2
3
4
5
6

if the number 4 being deleted. . .it will look like this in MSFlexGrid:
1
2
3

5
6

if i press the button "RE-ARRANGE" it will look like this:
1
2
3
5
6

if i press the button "INCREMENT" it will not go directly to seven but instead the number 4 will be inserted:
1
2
3
5
6
4
press re-arrange:
1
2
3
4
5
6 and so on
7
8
9
10

this is a challenging one so pls help me. . i need to know how to do this. thank you so much.

Hi,

You want code for Increment or what..?

If you already have code for that, then,
Call Re-Arrange Code in Increment Click button...

Regards
Veena

Hi,

You want code for Increment or what..?

If you already have code for that, then,
Call Re-Arrange Code in Increment Click button...

Regards
Veena

I need the whole code. . it would be better if you include the increment code so it is easy to trace. . .

Hi,

Download the attached file and test it.


Shailaja:)

I need the whole code. . it would be better if you include the increment code so it is easy to trace. . .

commented: WOW. . .YOUR PROGRAM IS COOL. . IS NOW SOLVED. . .THANK YOU SO MUCH +1

Hi,

Download the attached file and test it.


Shailaja:)

hallo, I am studying the program you gave me. . .I'll let you know soon the result. . by the way, this that really you in the pic?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.