Sample ADO Database Record Retrieve SQL Script - Support

Sample ADO Database Record Retrieve SQL Script

From Support

Jump to: navigation, search

The following script can be used to retrieve records from a database. This particular example only returns data from the first record read.


Dim SQLString As String

Dim conn As Object Dim rs As Object

'Connect to DB with ConnectString Set conn = CreateObject("ADODB.Connection") conn.Open "DSN=pubs;UID=sa;PWD="

'Set record select to select specific record

'Select specific from authors table SQLString = "select * from authors where au_id = '238-95-7766'"

'Create recordset object Set rs = CreateObject("ADODB.Recordset") rs.CursorLocation = 2 'Read only rs.CursorType = 0 'Forward only recordset rs.Open SQLString, conn ' Run the Query

'Check for end of file. If record found, If Not (rs.EOF And rs.BOF) Then 'Data found

       MsgBox rs("au_id")

Else 'No Data Found

       MsgBox "Record not found"

End If

'Close recordset and connection rs.Close conn.Close

'Release ADO objects Set rs = Nothing Set conn = Nothing


Personal tools