[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?
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