Results 1 to 2 of 2

Thread: [RESOLVED] Last Record from MS Access table to Textbox on a form!

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2007
    Posts
    788

    Resolved [RESOLVED] Last Record from MS Access table to Textbox on a form!

    Dear Friends,

    How can I retrieve the last record from a table to textbox on form?

    I am using MS Access Database.
    Application : VB6 (ADODB Recordset)
    Table Name is : ERecords
    There are 3 fields in the Table:
    1) ID (Autonumber - Primary Key)
    2) EName
    3) Position

    I need to retrieve the last record from ERecords table ("ID" Field) in a textbox called txtLastRecord on Employees Form.
    How can I do it?

  2. #2
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Last Record from MS Access table to Textbox on a form!

    Here is a quick sample using the Northwinds db
    Code:
    Private Sub Command1_Click()
    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim strSQL As String
    
        'Adjust to match your db
        strSQL = "Select Max(EmployeeID) as EmployeeID from Employees"
    
        'connect to the database
        Set cn = New ADODB.Connection
        cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Microsoft Visual Studio\VB98\NWIND.MDB;Persist Security Info=False"
        cn.Open
        
        'create a recordset
        Set rs = New ADODB.Recordset
        rs.ActiveConnection = cn
        rs.Source = strSQL
        rs.Open
        
        Text1.Text = rs!EmployeeID
        
        'Cleanup
        rs.Close
        Set rs = Nothing
        cn.Close
        Set cn = Nothing
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width