Saturday, November 5, 2011

Visual Basic ADO to Page Through Records 10 at a Time


Option Explicit
Private
 m_CombinedNames As String
‘Display the first 10 records.
Private Sub cmdList_Click()‘ Reset m_CombinedNames‘ to select the first record.m_CombinedNames = “,”‘ Get the next 10 records.cmdNext.Enabled = TruecmdNext_Click
End Sub
‘ Display the next 10 records.
Private Sub cmdNext_Click()Dim DB_Name As StringDim Conn As ADODB.ConnectionDim RS As ADODB.RecordsetDim txt As StringDim i As Integer‘ Get the database name.
DB_Name = App.Path

If Right$(DB_Name, 1) <> “” Then DB_Name = DB_Name & “”
DB_Name = DB_Name & “Employee.mdb”‘ Open a connection.Set Conn = New ADODB.ConnectionConn.ConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;” & “Data Source=” & DB_Name & “;” & _“Persist Security Info=False”Conn.Open‘ Get the next 10 records.
Set RS = Conn.Execute(“SELECT TOP 10 * FROM Employees “ & “WHERE LastName + ‘,’ + _FirstName > “ & “‘” & m_CombinedNames & “‘ ORDER BY LastName, “ & “FirstName”)‘ Display the records.Do until RS.EOFi = i + 1txt = txt & vbCrLf & Format$(RS!EmployeeId, “@@@”) & ” “ & _Format$(RS!LastName, “!@@@@@@@@@@@@@”) & Format$(RS!FirstName, “!@@@@@@@@@@@@@”)m_CombinedNames = RS!LastName & “,” & RS!FirstNameRS.MoveNextLoop‘ See if we ran out of records.If i < 10 Thentxt = txt & vbCrLf & “<END>”cmdNext.Enabled = FalseEnd If‘ Display the data.If Len(txt) > 0 Then txt = Mid$(txt, 3)txtEmployees.Text = txt
End Sub

No comments:

Post a Comment