There are 2 basic types of queries
MS Access 97
Recordset_X!FieldName Recordset_x![FieldName]The following example creates a recordset based on the query string.
CurrentDB is a reference to the current database.
Because of the aggregate function Max(), the following example produces a recordset with only one record. Notice that in order to reference the result of an aggregate function, the result MUST be given a new identifier.
Dim SQL_String As String
Dim Temp_dbs As Database
Dim Temp_qdf As QueryDef
Dim Temp_rst As Recordset
SQL_String = "Select Max([SomeField]) as SomeName from SomeTable;"
Set Temp_dbs = CurrentDb
Set Temp_qdf = Temp_dbs.CreateQueryDef("", SQL_String)
Set Temp_rst = Temp_qdf.OpenRecordset(dbOpenSnapshot)
xx_UIField.SetFocus ' This is required before writing to a UI object
xx_UIField.Text = Temp_rst![SomeName] + 5
The following shows how to open a query without using
a QueryDef.
Set dbs = CurrentDb SQL_Str = "SELECT * FROM Orders WHERE [StateField] = 'VA'" Set Some_Recordset = dbs.OpenRecordset(SQL_Str)The following method also works.
Set Some_Recordset = CurrentDb.OpenRecordset( _
"SELECT * FROM Orders WHERE [StateField] = 'VA'")
Queries Which Do Not Return Recordsets
Create a new Table
CurrentDB.Execute("select * into xx from yy"), dbFailOnEror
Append to an existing table
CurrentDB.Execute("Insert Into xx select * from yy"), dbFailOnEror
MS Access 2000/2002/XP
Dim cnn As ADODB.Connection
Dim rst1 As New ADODB.Recordset
Set cnn = CurrentProject.Connection
Query = "Select * from Alias_Details_tbl where Purpose='" & Alias_UICombo & "'"
rst1.Open Query, cnn, adOpenKeyset, adLockOptimistic, adCmdTableDirect
Do Until rst1.EOF
' Place your code here
str1 = rst1!FieldName
Loop
rst1.Close
This syntax works for queries that don't return a record set.
DoCmd.RunSQL "Delete * from Aliases_tbl"
Delphi 5.0