0

This is the table...

Id | Sort | ->a bunch of other data columns
____________________________________________
3687  |  1   |
3674  |  2   |
3688  |  3   |
3689  |  4   |
3680  |  5   |
3679  |  6   |

How to code 'Next' and 'Prev' buttons to traverse data records based Id or Id and Sort? Column Sort is editable and is how the data is sorted for the user. They can edit the number in the Sort column to change how the records are sorted. The LoadDataFor sub will retrieve the data record Id in a form for editing.

Starting at row Id 3687 clicking the next button will never get the record for 3674

I have based the code from this post: https://stackoverflow.com/a/1446831/6610415

Code for 'Next' button:

If Id < GetMaxID(CabinetName, DrawerId) Then
            Id = GetNextID(Id, CabinetName, DrawerId)
            'Me.Text = "Edit Tool " & Id
            LoadDataFor(Id)
End If

Code for 'Prev' button:

If Id > GetMinID(CabinetName, DrawerId) Then
            Id = GetPreviousID(Id, CabinetName, DrawerId)
            'Me.Text = "Edit Tool " & Id
            LoadDataFor(Id)
End If

Functions:


 Private Function GetPreviousID(currentId As Integer, CabinetName As String, DrawerId As Integer) As Integer
        Dim output As Integer = 0
        Dim sqlStr As String = "SELECT ID FROM [dbo].[Tool_Contents] WHERE ID = 
(SELECT MAX(ID) FROM [dbo].[Tool_Contents] WHERE ID < " & currentId & ") AND ([CabinetName] LIKE @CabinetName) AND ([Drawer_ID] = @DrawerId)"
        Using dbConn As New SqlConnection(My.Settings.ConnectionString)
            dbConn.Open()
            Using sqlcmd As New SqlCommand With {.Connection = dbConn, .CommandType = CommandType.Text, .CommandText = sqlStr}
                sqlcmd.Parameters.Add("@CabinetName", SqlDbType.NVarChar, 255).Value = CabinetName
                sqlcmd.Parameters.Add("@DrawerId", SqlDbType.Int).Value = DrawerId
                Dim sqlDr As SqlDataReader = sqlcmd.ExecuteReader(CommandBehavior.SingleResult)
                If sqlDr.HasRows Then
                    While sqlDr.Read
                        output = sqlDr.GetSqlInt32(0)
                    End While
                Else
                    output = currentId
                End If
            End Using
        End Using
        Return output
    End Function

    Private Function GetNextID(currentId As Integer, CabinetName As String, DrawerId As Integer) As Integer
        Dim output As Integer = 0
        Dim sqlStr As String = "SELECT ID FROM [dbo].[Tool_Contents] WHERE ID = 
(SELECT MIN(ID) FROM [dbo].[Tool_Contents] WHERE ID > " & currentId & ") AND ([CabinetName] LIKE @CabinetName) AND ([Drawer_ID] = @DrawerId)"
        Using dbConn As New SqlConnection(My.Settings.ConnectionString)
            dbConn.Open()
            Using sqlcmd As New SqlCommand With {.Connection = dbConn, .CommandType = CommandType.Text, .CommandText = sqlStr}
                sqlcmd.Parameters.Add("@CabinetName", SqlDbType.NVarChar, 255).Value = CabinetName
                sqlcmd.Parameters.Add("@DrawerId", SqlDbType.Int).Value = DrawerId
                Dim sqlDr As SqlDataReader = sqlcmd.ExecuteReader(CommandBehavior.SingleResult)
                If sqlDr.HasRows Then
                    While sqlDr.Read
                        output = sqlDr.GetSqlInt32(0)
                    End While
                Else
                    output = currentId
                End If
            End Using
        End Using
        Return output
    End Function

    Private Function GetMaxID(CabinetName As String, DrawerId As Integer) As Integer
        Dim output As Integer
        Dim sqlStr As String = "SELECT MAX(ID) FROM [dbo].[Tool_Contents] WHERE ([CabinetName] LIKE @CabinetName) AND ([Drawer_ID] = @DrawerId)"
        Using dbConn As New SqlConnection(My.Settings.ConnectionString)
            dbConn.Open()
            Using sqlcmd As New SqlCommand With {.Connection = dbConn, .CommandType = CommandType.Text, .CommandText = sqlStr}
                sqlcmd.Parameters.Add("@CabinetName", SqlDbType.NVarChar, 255).Value = CabinetName
                sqlcmd.Parameters.Add("@DrawerId", SqlDbType.Int).Value = DrawerId
                Dim sqlDr As SqlDataReader = sqlcmd.ExecuteReader(CommandBehavior.SingleResult)
                If sqlDr.HasRows Then
                    While sqlDr.Read
                        output = sqlDr.GetSqlInt32(0)
                    End While
                End If
            End Using
        End Using
        Return output
    End Function

    Private Function GetMinID(CabinetName As String, DrawerId As Integer) As Integer
        Dim output As Integer
        Dim sqlStr As String = "SELECT MIN(ID) FROM [dbo].[Tool_Contents] WHERE ([CabinetName] LIKE @CabinetName) AND ([Drawer_ID] = @DrawerId)"
        Using dbConn As New SqlConnection(My.Settings.ConnectionString)
            dbConn.Open()
            Using sqlcmd As New SqlCommand With {.Connection = dbConn, .CommandType = CommandType.Text, .CommandText = sqlStr}
                sqlcmd.Parameters.Add("@CabinetName", SqlDbType.NVarChar, 255).Value = CabinetName
                sqlcmd.Parameters.Add("@DrawerId", SqlDbType.Int).Value = DrawerId
                Dim sqlDr As SqlDataReader = sqlcmd.ExecuteReader(CommandBehavior.SingleResult)
                If sqlDr.HasRows Then
                    While sqlDr.Read
                        output = sqlDr.GetSqlInt32(0)
                    End While
                End If
            End Using
        End Using
        Return output
    End Function

Made some changes to the code.

JDesigns
  • 1
  • 3
  • What actual errors or unexpected behavior are you getting from that code? – JohnPete22 Jan 16 '20 at 15:27
  • 1
    Related - don't use [addwithvalue](http://www.dbdelta.com/addwithvalue-is-evil/). And don't partially parameterize your queries. And why is _currentId_ a string on input but an int on output? Don't mix datatypes for the same value. – SMor Jan 16 '20 at 15:32
  • No Errors just skips some rows or doesn't scroll 1 at a time. – JDesigns Jan 16 '20 at 17:42

0 Answers0