ryanstarret
asked on
Force existing queries to open in SQL view
Here is the dilema:
I have copied a bunch of queries using a chunk of code. All the copied queries want to open in design view. This is no good because it damages the joins when it can't display them.
Can I write code to se all existing queries to open to SQL view so that my joins don't get damaged?
Thanks,
R.
I have copied a bunch of queries using a chunk of code. All the copied queries want to open in design view. This is no good because it damages the joins when it can't display them.
Can I write code to se all existing queries to open to SQL view so that my joins don't get damaged?
Thanks,
R.
there is DoCmd.OpenQuery, but there seems to be no option to open in SQL View.
You may just well be stuck.
You may just well be stuck.
You can usecode to build a table with the queryname and the SQL text and view that from a form.
To create the table use e.g. a function like:
Function fncListQueries()
dim qd as DAO.querydef
for each qd in currentdb.querydefs
if left(qd.name,1) <>"~" then
currentdb.execute ("insert into tblQueries (QueryName, QuerySQL) Values ('" & qd.name & "'," & chr(34) & qd.SQL & chr(34) & ")")
endif
next
QueryName will have to be a text field and QuerySQL a Memo field in table tblQueries.
Getting the idea ?
Nic;o)
To create the table use e.g. a function like:
Function fncListQueries()
dim qd as DAO.querydef
for each qd in currentdb.querydefs
if left(qd.name,1) <>"~" then
currentdb.execute ("insert into tblQueries (QueryName, QuerySQL) Values ('" & qd.name & "'," & chr(34) & qd.SQL & chr(34) & ")")
endif
next
QueryName will have to be a text field and QuerySQL a Memo field in table tblQueries.
Getting the idea ?
Nic;o)
I'm running A2K and found that when I saved the query from the SQL View, it would open in the SQL View. Does that work for you?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hello ryanstarret
I had this problem before, but could not find how and where Access stores the default view for queries. However, saving a query in SQL view is retained (as Ray already mentioned), so that this worked for me:
Sub OpenSQLView(pstrQuery As String)
Dim strSQL As String
strSQL = CurrentDb.QueryDefs(pstrQu ery).SQL
DoCmd.OpenQuery pstrQuery, acViewDesign
RunCommand acCmdSQLView
DoCmd.Save acQuery, pstrQuery
DoCmd.Close acQuery, pstrQuery, acSaveNo
CurrentDb.QueryDefs(pstrQu ery).SQL = strSQL
DoCmd.OpenQuery pstrQuery, acViewDesign
End Sub
Much like in Rick's solution, the original SQL string is preserved and written back into the query once it has been saved from the SQL view.
WARNING: the above actually saves the queries in their broken state (after the links removal), so make sure you have a backup.
Cheers!
(°v°)
I had this problem before, but could not find how and where Access stores the default view for queries. However, saving a query in SQL view is retained (as Ray already mentioned), so that this worked for me:
Sub OpenSQLView(pstrQuery As String)
Dim strSQL As String
strSQL = CurrentDb.QueryDefs(pstrQu
DoCmd.OpenQuery pstrQuery, acViewDesign
RunCommand acCmdSQLView
DoCmd.Save acQuery, pstrQuery
DoCmd.Close acQuery, pstrQuery, acSaveNo
CurrentDb.QueryDefs(pstrQu
DoCmd.OpenQuery pstrQuery, acViewDesign
End Sub
Much like in Rick's solution, the original SQL string is preserved and written back into the query once it has been saved from the SQL view.
WARNING: the above actually saves the queries in their broken state (after the links removal), so make sure you have a backup.
Cheers!
(°v°)
ASKER
Rick,
I"ve modified your code a bit... but for some reason sendkeys fails on a (... not exactly sure why.
R.
I"ve modified your code a bit... but for some reason sendkeys fails on a (... not exactly sure why.
R.
ASKER
Sorry... meant to paste the code:
Sub ReOpenQueries()
Dim strQuery As String
Dim strSQL As String
Dim lngCount As Long
Dim strChr As String
Dim qdf As QueryDef
For Each qdf In CurrentDb.QueryDefs
If Left(qdf.Name, 3) <> "~sq" Then
strQuery = qdf.Name
If Len(qdf.Name) Then
strSQL = qdf.sql
DoCmd.OpenQuery qdf.Name, acViewDesign
DoCmd.RunCommand acCmdSQLView
strSQL = Replace(strSQL, "[SELECT", "(SELECT")
strSQL = Replace(strSQL, "].", ")")
For lngCount = 1 To Len(strSQL)
strChr = Mid(strSQL, lngCount, 1)
Select Case Asc(strChr)
Case 10
'Do Nothing (Line feeds will produce double carriage return
Case Else
Debug.Print Asc(strChr)
SendKeys strChr
End Select
Next lngCount
DoCmd.Close acQuery, qdf.Name, acSaveYes
End If
End If
Next
End Sub
Sub ReOpenQueries()
Dim strQuery As String
Dim strSQL As String
Dim lngCount As Long
Dim strChr As String
Dim qdf As QueryDef
For Each qdf In CurrentDb.QueryDefs
If Left(qdf.Name, 3) <> "~sq" Then
strQuery = qdf.Name
If Len(qdf.Name) Then
strSQL = qdf.sql
DoCmd.OpenQuery qdf.Name, acViewDesign
DoCmd.RunCommand acCmdSQLView
strSQL = Replace(strSQL, "[SELECT", "(SELECT")
strSQL = Replace(strSQL, "].", ")")
For lngCount = 1 To Len(strSQL)
strChr = Mid(strSQL, lngCount, 1)
Select Case Asc(strChr)
Case 10
'Do Nothing (Line feeds will produce double carriage return
Case Else
Debug.Print Asc(strChr)
SendKeys strChr
End Select
Next lngCount
DoCmd.Close acQuery, qdf.Name, acSaveYes
End If
End If
Next
End Sub
ASKER
OK... well. Sendkeys just isn't sending the keystrokes to that window unfortunately... I'm not really sure what's going on.
ASKER
Ok... got the problem solved... and here is the solution:
Private Sub cmdGo_Click()
Dim strQuery As String
Dim strSQL As String
Dim lngCount As Long
Dim strChr As String
Dim qdf As QueryDef
Dim Start As Long
Dim PauseTime As Long
If IsNull(cboQueryNames) Or cboQueryNames.Value = vbNullString Then Exit Sub
MsgBox "Do not touch computer until this task completes!", vbExclamation, "Rexdale Loss Database"
strQuery = cboQueryNames
If Len(strQuery) Then
strSQL = CurrentDb.QueryDefs(strQue ry).sql
DoCmd.OpenQuery strQuery, acViewDesign
DoEvents
DoCmd.RunCommand acCmdSQLView
DoEvents
strSQL = Replace(strSQL, "[SELECT", "(SELECT")
strSQL = Replace(strSQL, "].", ")")
'Insert a pause
PauseTime = 20
Start = Timer
Do While Timer < Start + PauseTime
DoEvents
Loop
'Copy the correct code
For lngCount = 1 To Len(strSQL)
DoEvents
strChr = Mid(strSQL, lngCount, 1)
Select Case Asc(strChr)
Case 10
'Do Nothing (Line feeds will produce double carriage return)
Case Else
Debug.Print Asc(strChr)
If strChr = "(" Or strChr = ")" Or _
strChr = "+" Or strChr = "^" Or _
strChr = "%" Or strChr = "~" Or _
strChr = "[" Or strChr = "]" Or _
strChr = "{" Or strChr = "}" Then
strChr = "{" & strChr & "}"
End If
SendKeys strChr
End Select
Next lngCount
DoEvents
DoCmd.Close acQuery, strQuery, acSaveYes
End If
End Sub
The delay in there was necessary because the queries are slow to open sometimes.
Thanks for the help.
Private Sub cmdGo_Click()
Dim strQuery As String
Dim strSQL As String
Dim lngCount As Long
Dim strChr As String
Dim qdf As QueryDef
Dim Start As Long
Dim PauseTime As Long
If IsNull(cboQueryNames) Or cboQueryNames.Value = vbNullString Then Exit Sub
MsgBox "Do not touch computer until this task completes!", vbExclamation, "Rexdale Loss Database"
strQuery = cboQueryNames
If Len(strQuery) Then
strSQL = CurrentDb.QueryDefs(strQue
DoCmd.OpenQuery strQuery, acViewDesign
DoEvents
DoCmd.RunCommand acCmdSQLView
DoEvents
strSQL = Replace(strSQL, "[SELECT", "(SELECT")
strSQL = Replace(strSQL, "].", ")")
'Insert a pause
PauseTime = 20
Start = Timer
Do While Timer < Start + PauseTime
DoEvents
Loop
'Copy the correct code
For lngCount = 1 To Len(strSQL)
DoEvents
strChr = Mid(strSQL, lngCount, 1)
Select Case Asc(strChr)
Case 10
'Do Nothing (Line feeds will produce double carriage return)
Case Else
Debug.Print Asc(strChr)
If strChr = "(" Or strChr = ")" Or _
strChr = "+" Or strChr = "^" Or _
strChr = "%" Or strChr = "~" Or _
strChr = "[" Or strChr = "]" Or _
strChr = "{" Or strChr = "}" Then
strChr = "{" & strChr & "}"
End If
SendKeys strChr
End Select
Next lngCount
DoEvents
DoCmd.Close acQuery, strQuery, acSaveYes
End If
End Sub
The delay in there was necessary because the queries are slow to open sometimes.
Thanks for the help.
ASKER
sorry.. I guess I was confused. I got to the end where it said something about saving the queries in their broken state and I think I just skipped it. The other solution worked out well though. Made a reasonable form for the person to deal with after I leave this place (in two days!)
No problem. Good luck with you future plans!
(°v°)
(°v°)
Its most annoying, access loves those crappy round brackets that are placed everywhere.