?
Solved

Help debugging subroutine

Posted on 2003-02-27
13
Medium Priority
?
227 Views
Last Modified: 2006-11-17
The following subroutine is supposed to dump the Query Object Name and Query Object SQL statement out to a table.

The problem seems to be some of the characters commonly found inside a SQL statement, that are confusing matters.


Public Sub sbOutputQuerySQL()

Dim qd As QueryDef
Dim strTemp As String
Dim intCount As Integer

intCount = 0

For Each qd In CurrentDb.QueryDefs
    ConfirmOff
    DoCmd.RunSQL "insert into tblTomTemp (QueryName, SQL) VALUES ('" & qd.Name & "', '" & qd.Sql & "')"
    ConfirmOn
Next

Set qd = Nothing

End Sub


For example, a SQL statement like this generates an error:

SELECT qryLenderCallsPassThrough.TentativeClosingDate, qryLenderCallsPassThrough.GrantID, qryLenderCallsPassThrough.Buyer, qryLenderCallsPassThrough.[Grant ], qryLenderCallsPassThrough.LO, qryLenderCallsPassThrough.LOAsst, qryLenderCallsPassThrough.TitleAgency, qryLenderCallsPassThrough.TitlePhone
FROM qryLenderCallsPassThrough
WHERE LenderPhone = '8002032035'
ORDER BY qryLenderCallsPassThrough.LO, qryLenderCallsPassThrough.LOAsst, qryLenderCallsPassThrough.TentativeClosingDate;


generates error:

syntax error (missing operator)


How can I properly encapsulate the SQL statement so it will go into the Memo field SQL?
0
Comment
Question by:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 
LVL 54

Expert Comment

by:nico5038
ID: 8037517
Try:
DoCmd.RunSQL "insert into tblTomTemp (QueryName, SQL) VALUES (" & chr(34) & qd.Name & chr(34) & ", " & chr(34) & qd.Sql & chr(34) & ")"

Nic;o)
0
 
LVL 54

Expert Comment

by:nico5038
ID: 8037536
The reason sre the single quotes in:
WHERE LenderPhone = '8002032035'
The will "break" the string you build.

Nic;o)
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 8037545
Sounds good.

Is there anything that can be in a SQL statement that using

chr(34)

won't handle???
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 54

Expert Comment

by:nico5038
ID: 8037579
Yes, a " as that's now inserted :-)

The other solution will be to change all single and double quotes by two occurrences like:
WHERE LenderPhone = ''8002032035''
as they will be seen as one, but the chr(34) normally handles 99,99 % OK.

Nic;o)
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 8037589
Nic;o)

Still getting number of query values and destination fields are not the same:

Maybe after [Last Name] is the problem?

here is the resolved SQL command:

insert into tblTomTemp (QueryName, SQL) VALUES ("~sq_cCopy of frmEntriesForm~sq_cCombo80", "SELECT [tblEntriesByPeople].[EntryID], [LastName] & ", " & [FirstName] & " " & [Initial] AS Expr2
FROM tblEntriesByPeople
ORDER BY [LastName] & ", " & [FirstName] & " " & [Initial];
")

0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 8037610
Here is my changed DoCmd.RunSQL as per your instructions:

DoCmd.RunSQL "insert into tblTomTemp (QueryName, SQL) VALUES (" & Chr(34) & qd.Name & Chr(34) & ", " & Chr(34) & qd.Sql & Chr(34) & ")"
0
 
LVL 54

Accepted Solution

by:
nico5038 earned 400 total points
ID: 8037662
Hmm, indeed proving the 0,01 % <LOL>

Normally just single or double quotes are used, but you seem to have a mix.

I would use the replace in this situation like:
strSQL = qd.SQL
replace(strSQL,"'","''")
replace(strSQL,chr(34),chr(34)&chr(34))

Use the strSQL as using the dq.SQL will also change the query !

Thus all will be "neutralized"

Nic;o)
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 8037739
Here is (I think) a workable solution.

We have a TextCleaner function we can call (my boss wrote, clever fellow) that I think is taking care of it:

Public Sub sbOutputQuerySQL()

Dim qd As QueryDef
Dim strTemp As String

intCount = 0

For Each qd In CurrentDb.QueryDefs
    ConfirmOff
    strTemp = TextCleaner(qd.Sql, "'")
    strTemp = TextCleaner(strTemp, """")
    DoCmd.RunSQL "insert into tblTomTemp (QueryName, SQL) VALUES (" & Chr(34) & qd.Name & Chr(34) & ", " & Chr(34) & strTemp & Chr(34) & ")"
    ConfirmOn
Next

Set qd = Nothing

MsgBox "Done"
End Sub


~~~~~~~~~~~~~~~~~~~~~~~

Here is our TextCleaner function:

Public Function TextCleaner(strDirtyText As String, Optional strType As String) As String
    Dim x As Long
    Dim strCharacter As String
    strType = Left(Nz(strType, ""), 1)
    For x = 1 To Len(strDirtyText)
    strCharacter = Mid(strDirtyText, x, 1)
    Select Case strType
    Case ""
        Select Case strCharacter
            Case """", "'"
                TextCleaner = TextCleaner & strCharacter & strCharacter
            Case Else
                TextCleaner = TextCleaner & strCharacter
        End Select
    Case Else
        Select Case strCharacter
            Case strType
                TextCleaner = TextCleaner & strCharacter & strCharacter
            Case Else
                TextCleaner = TextCleaner & strCharacter
        End Select
    End Select
Next

End Function



What do you think?
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 8037740
Thanks for helping me think through this!
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 8037753
Notice I am cleaning the text for both   '   and    " embedded characters:

strTemp = TextCleaner(qd.Sql, "'")


strTemp = TextCleaner(strTemp, """")
0
 
LVL 54

Expert Comment

by:nico5038
ID: 8037845
Think it will work OK, but the Replace will proably be faster as it's all done internally.
Your function will be needed in A97 as the replace is introduced in A2000 if I remember well.

Glad it's working Tom !

C U

Nic;o)
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 8037892
Understood.

Later,

Tom
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 8037902
btw:

I used to come in here (The MS Access area) all the time.

Then I got pulled of of Access 2000 for about 3 months to work on my first VB .NET project (fun).

Now I am getting back into the swing of things in Access again.

We have some big normalization projects coming up, so that is why I am interested in exported all the embedded SQL code, as many of the queries will be broken by the normalization process.

I guess this is why I make the "big bucks"  :)

Thanks again,

Tom
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question