You can use the following script to retrieve records from a database. This particular example only returns data from the first record:
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

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

Still have questions? We can help. Submit a case to Technical Support.

Last Modified On: September 11, 2020