Using the dbDAO GetRows Method

The GetRows method takes one parameter specifying the number of records to fetch, and returns the data as variants. Unlike Visual Basic, you cannot simply use subscripts to indicate the array field in a variant. Rather, the data is returned as an OLE SafeArray. The parray member of the variant points to an array descriptor. Individual fields are accessed by calling the SafeArrayGetElement function.

See Also For more information about using the SafeArray data type, see the OLE Automation Programmer’s Reference published by Microsoft Press.

The following code from the GetRows sample (found in the \Samples\GetRows folder of the DAOSDK installation) shows how to use the GetRows method. Note that the table schema is known a priori, allowing you to hard-code the display functions.

void CGetRowsDlg::DoGetRows() 
{
	COleVariant		cRows;
	COleVariant		varField;
	CString			strLBRow;
	TCHAR			szId[32];
	LONG			lNumRecords;
	LONG			lIndex[2];
	HRESULT			hResult;
	CListBox		*pListBox = (CListBox *)GetDlgItem(IDD_GETROWSLIST);

	//Perform GetRows on Employee table.
	//This GetRows uses VARIANTS.
	cRows = m_cEmpRecordSet.GetRows(MAX_EMP_REC); //Arbitrarily get MAX_EMP_REC rows.

	//Find out how many records were actually retrieved.
	//(SafeArrays are 1-based.)
	SafeArrayGetUBound(cRows.parray, 2, &lNumRecords);

	//Clear the list box.
	pListBox->ResetContent();
	
	for (lIndex[1] = 0; lIndex[1] <= lNumRecords; lIndex[1]++)
		{
		strLBRow.Empty();//Clear the string.

		lIndex[0] = EMP_ID;
			
		//Use OLE safe array function to access fields.
		hResult = SafeArrayGetElement(cRows.parray, &lIndex[0], &varField);

		//Watch out for bad variants.
		if(FAILED(hResult))
			break;

		if(varField.vt == VT_I4) //Must be a long.
			{
			wsprintf(szId, _T("%d,  "), varField.iVal);
			}
		else
			{
			lstrcpy(szId, _T("Unexpected Data Type"));
			}

		strLBRow += (LPCTSTR)szId;

		//Get last name.
		lIndex[0] = EMP_LNAME;
		SafeArrayGetElement(cRows.parray, &lIndex[0], &varField);
		strLBRow += (LPCTSTR)varField.bstrVal;

		//Get first name.
		strLBRow += _T(", ");
		lIndex[0] = EMP_FNAME;
		SafeArrayGetElement(cRows.parray, &lIndex[0], &varField);
		strLBRow += (LPCTSTR)varField.bstrVal;

		pListBox->AddString(strLBRow);
		}
}