kramervonfeldberg
New member
- Local time
- Yesterday, 18:49
- Joined
- Mar 4, 2009
- Messages
- 2
I have an existing select query that I would like to execute with DAO. I have been able to write code to create and execute the query but how do you simple excute an existing query. Say the exiting query is named renames as shown below.
Code:
[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]Option Compare Database[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]Option Explicit[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] [/SIZE][/FONT]
[FONT=Calibri][SIZE=3]'References[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]'Visual Basic for Applications[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]'Microsoft Access 12.0 object library[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]'Microsoft DAO 3.6 object library[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]'Microsoft Forms 2.0 object library[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]'OLE Automation[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]'Microsoft Visual Basic for Applications Extensibility 5.3[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] [/SIZE][/FONT]
[FONT=Calibri][SIZE=3] [/SIZE][/FONT]
[FONT=Calibri][SIZE=3]Sub Renames_Query()[/SIZE][/FONT]
[SIZE=3][FONT=Calibri] Dim db As Database[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] Dim qdf As QueryDef[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] Dim strSQL As String[/FONT][/SIZE]
[FONT=Calibri][SIZE=3] [/SIZE][/FONT]
[SIZE=3][FONT=Calibri] On Error GoTo Err_SelectQuery[/FONT][/SIZE]
[FONT=Calibri][SIZE=3] [/SIZE][/FONT]
[SIZE=3][FONT=Calibri] strSQL = "SELECT ScadaDevicesOld.P_KEY, ScadaDevices.P_KEY FROM ScadaDevicesOld INNER JOIN ScadaDevices ON ScadaDevicesOld.P_OID = ScadaDevices.P_OID WHERE ((([ScadaDevices.P_KEY])<>[ScadaDevicesOld.P_KEY]))"[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] Set db = CurrentDb[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] Set qdf = db.CreateQueryDef("renames", strSQL)[/FONT][/SIZE]
[FONT=Calibri][SIZE=3]ExitHere:[/SIZE][/FONT]
[SIZE=3][FONT=Calibri] Set qdf = Nothing[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] db.Close[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] Set db = Nothing[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] Exit Sub[/FONT][/SIZE]
[FONT=Calibri][SIZE=3]Err_SelectQuery:[/SIZE][/FONT]
[SIZE=3][FONT=Calibri] If Err.Number = 3012 Then[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] MsgBox "Query with this name already exists."[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] Else[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] MsgBox Err.Description[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] End If[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] Resume ExitHere [/FONT][/SIZE]
[FONT=Calibri][SIZE=3]