Error 3075 sql string (1 Viewer)

Ruzz2k

Registered User.
Local time
Today, 21:41
Joined
Apr 28, 2012
Messages
102
Hi I get runtime error 3075 with the following sql, trying to open query and then create table in email based on query. Any ideas thanks
Code:
Sub ReportToOutlookBody()
    Dim rst As DAO.Recordset
    Dim strSQL As String
    Dim olApp As Outlook.Application
    Dim objMail As Outlook.MailItem
    Dim strTableBeg As String
    Dim strTableBody As String
    Dim strTableEnd As String
    Dim strFntNormal As String
    Dim strTableHeader As String
    Dim strFntEnd As String
        
    'Define format for output
    strTableBeg = "<br><br><table border=1 cellpadding=3 cellspacing=0>"
    strTableEnd = "</table><br><br>Sincerely,<br><br>The Big Guy"
    strTableHeader = "<font size=3 face=" & Chr(34) & "Arial" & Chr(34) & "><b>" & _
                        "<tr bgcolor=lightblue>" & _
                            TD("Section") & _
                            TD("Document Code") & _
                            TD("Document Title") & _
                            TD("Document Type") & _
                            TD("Reason for Change") & _
                            TD("Issue Number") & _
                            TD("Issue Date") & _
                        "</tr></b></font>"
    strFntNormal = "<font color=black face=" & Chr(34) & "Arial" & Chr(34) & " size=2>"
    strFntEnd = "</font>"
    
    strSQL = " SELECT tblDocs.Section, tblDocs.[Document Code], tblDocs.[Document Title], tblDocs.[Document Type], qryCurrent.[Reason for Change], qryCurrent.[Issue Number], qryCurrent.[Issue Date], tblDocumentType.[Intranet Prefix], tblDocs.[Intranet Suffix], IIf([tblDocs].[Active]=0,'Withdrawn',IIf([tblDocs].[Section]='Group',DLookUp('[Intranet Prefix]','[tblDocumentType]','[Document Type] = 'Quality Manual'') & [tblDocs].[Document Code] & [Intranet Suffix],IIf([tblDocs].[Section]='HSE',DLookUp('[Intranet Prefix]','[tblDocumentType]','[Document Type] = 'LWI'') & [tblDocs].[Document Code] & [Intranet Suffix],([Intranet Prefix] & [tblDocs].[Document Code] & [tblDocs].[Intranet Suffix])))) AS iLink FROM (tblDocumentType INNER JOIN tblDocs ON tblDocumentType.[Document Type] = tblDocs.[Document Type]) INNER JOIN qryCurrent ON tblDocs.[Document Code] = qryCurrent.[Document Code]"
strSQL = strSQL & " WHERE (((tblDocs.Section) <> [Forms]![frmDocumentRegister]![frmSetup2].[Form]![Group Filter]) And ((tblDocs.Active) = True) And ((IIf(IsNull([Forms]![frmDocumentRegister]![FilterBy]), True, [tblDocs].[Section] = [Forms]![frmDocumentRegister]![FilterBy])) <> False) And ((IIf(IsNull([Forms]![frmDocumentRegister]![FilterBy0]), True, [tblDocs].[Document Type] = [Forms]![frmDocumentRegister]![FilterBy0])) <> False) And ((IIf(IsNull([Forms]![frmDocumentRegister]![Like Doc]), True, [tblDocs].[Document Code] Like [Forms]![frmDocumentRegister]![Like Doc])) <> False) And ((IIf(IsNull([Forms]![frmDocumentRegister]![Like Title]), True, [tblDocs].[Document Title] Like [Forms]![frmDocumentRegister]![Like Title])) <> False))"

    Set rst = CurrentDb.OpenRecordset(strSQL)
    
    'Build HTML Output for the DataSet
    strTableBody = strTableBeg & strFntNormal & strTableHeader
    
    Do Until rst.EOF
        strTableBody = strTableBody & _
                        "<tr>" & _
                            TD(rst!Section) & _
                            TD("<a href =" & (rst!iLink) & ">" & (rst!Document_Code) & "</a>") & _
                            TD(rst!Document_Title) & _
                            TD(rst!Document_Type) & _
                            TD(rst!Reason_for_Change) & _
                            TD(rst!Issue_Number) & _
                            TD(rst!Issue_Date) & _
                        "</tr>"
    
        rst.MoveNext
    Loop

    
    rst.Close
    
    'Create e-mail item
    Set olApp = Outlook.Application
    Set objMail = olApp.CreateItem(olMailItem)
    With objMail
        'Set body format to HTML
        .To = " "
        .Subject = "Past Due Item"
        .BodyFormat = olFormatHTML
        .HTMLBody = "<HTML><BODY>" & strFntNormal & strTableBody & " </BODY></HTML>"
        .Display
    End With
    
Clean_Up:
    Set rst = Nothing
    
End Sub
Function TD(strIn As String) As String
    TD = "<TD nowrap>" & strIn & "</TD>"
End Function

error occurs on line Set rst = CurrentDb.OpenRecordset(strSQL)
assuming somehting wrong with sql string
 

pr2-eugin

Super Moderator
Local time
Today, 21:41
Joined
Nov 30, 2011
Messages
8,494
Use a Debug.Print and see what you are getting, copy and paste the generated SQL into a Query window, see where the error is.
 

Ruzz2k

Registered User.
Local time
Today, 21:41
Joined
Apr 28, 2012
Messages
102
Error is with the quotes in the strSQL first line, but no clue how to fix this?

Thanks
 

namliam

The Mailman - AWF VIP
Local time
Today, 22:41
Joined
Aug 11, 2003
Messages
11,695
Well lets try basics 101, complicated code or unreadable code is unmaintanable....

Make it readable and find your problem, I did most of the work for you:
Code:
    strSQL = "     SELECT tblDocs.Section " 
    strSQL = strSQL & " , tblDocs.[Document Code] " 
    strSQL = strSQL & " , tblDocs.[Document Title] " 
    strSQL = strSQL & " , tblDocs.[Document Type] " 
    strSQL = strSQL & " , qryCurrent.[Reason for Change] " 
    strSQL = strSQL & " , qryCurrent.[Issue Number] " 
    strSQL = strSQL & " , qryCurrent.[Issue Date] " 
    strSQL = strSQL & " , tblDocumentType.[Intranet Prefix] " 
    strSQL = strSQL & " , tblDocs.[Intranet Suffix] " 
    strSQL = strSQL & " , IIf([tblDocs].[Active]=0 " 
    strSQL = strSQL & "       ,'Withdrawn' " 
    strSQL = strSQL & "       ,IIf([tblDocs].[Section]='Group' " 
    strSQL = strSQL & "            ,    DLookUp('[Intranet Prefix]','[tblDocumentType]','[Document Type] = 'Quality Manual'') & [tblDocs].[Document Code] & [Intranet Suffix] " 
    strSQL = strSQL & "            ,IIf([tblDocs].[Section]='HSE' " 
    strSQL = strSQL & "                ,DLookUp('[Intranet Prefix]','[tblDocumentType]','[Document Type] = 'LWI''           ) & [tblDocs].[Document Code] & [Intranet Suffix] " 
    strSQL = strSQL & "                ,                                                                   ([Intranet Prefix] & [tblDocs].[Document Code] & [Intranet Suffix])))) AS iLink  " 
    strSQL = strSQL & " FROM       (   tblDocumentType  " 
    strSQL = strSQL & " INNER JOIN     tblDocs           ON tblDocumentType.[Document Type] = tblDocs.[Document Type])  " 
    strSQL = strSQL & " INNER JOIN     qryCurrent        ON tblDocs.[Document Code] = qryCurrent.[Document Code] "

    strSQL = strSQL & " WHERE   (((tblDocs.Section) <> [Forms]![frmDocumentRegister]![frmSetup2].[Form]![Group Filter])  " 
    strSQL = strSQL & "   And    ((tblDocs.Active) = True)  " 
    strSQL = strSQL & "   And    ((IIf(IsNull([Forms]![frmDocumentRegister]![FilterBy])  , True, [tblDocs].[Section]        =    [Forms]![frmDocumentRegister]![FilterBy  ])) <> False)  " 
    strSQL = strSQL & "   And    ((IIf(IsNull([Forms]![frmDocumentRegister]![FilterBy0]) , True, [tblDocs].[Document Type]  =    [Forms]![frmDocumentRegister]![FilterBy0] )) <> False)  " 
    strSQL = strSQL & "   And    ((IIf(IsNull([Forms]![frmDocumentRegister]![Like Doc])  , True, [tblDocs].[Document Code]  Like [Forms]![frmDocumentRegister]![Like Doc]  )) <> False)  " 
    strSQL = strSQL & "   And    ((IIf(IsNull([Forms]![frmDocumentRegister]![Like Title]), True, [tblDocs].[Document Title] Like [Forms]![frmDocumentRegister]![Like Title])) <> False))"
 

Ruzz2k

Registered User.
Local time
Today, 21:41
Joined
Apr 28, 2012
Messages
102
thanks, will have a look when I go home. I do not know how to show the generated SQL? hence you would roll your eyes at me again. but I will look through the code before asking any more questions.
 

namliam

The Mailman - AWF VIP
Local time
Today, 22:41
Joined
Aug 11, 2003
Messages
11,695
Should be pretty simple to find the quoting problem in the re-formatted SQL I posted.

Even Paul should be able to find it !
 

Ruzz2k

Registered User.
Local time
Today, 21:41
Joined
Apr 28, 2012
Messages
102
Sorry namliam I am struggling, I debug printed it and get the following. I copied this sql from the sql window for my qry called qryOutputL.

It does not error until the 9th line strSQL = strSQL & " , IIf([tblDocs].[Active]=0 "

Code:
 SELECT tblDocs.Section  , tblDocs.[Document Code]  , tblDocs.[Document Title]  , tblDocs.[Document Type]  , qryCurrent.[Reason for Change]  , qryCurrent.[Issue Number]  , qryCurrent.[Issue Date]  , tblDocumentType.[Intranet Prefix]  , tblDocs.[Intranet Suffix]  , IIf([tblDocs].[Active]=0  , 'Withdrawn'  , IIf([tblDocs].[Section]='Group'  , DLookUp('[Intranet Prefix]','[tblDocumentType]','[Document Type] = 'Quality Manual'') & [tblDocs].[Document Code] & [Intranet Suffix]  ,IIf([tblDocs].[Section]='HSE'  ,DLookUp('[Intranet Prefix]','[tblDocumentType]','[Document Type] = 'LWI'') & [tblDocs].[Document Code] & [Intranet Suffix]  ([Intranet Prefix] & [tblDocs].[Document Code] & [Intranet Suffix])))) AS iLink  FROM (tblDocumentType  INNER JOIN tblDocs ON tblDocumentType.[Document Type] = tblDocs.[Document Type])  INNER JOIN qryCurrent ON tblDocs.[Document Code] = qryCurrent.[Document Code]  WHERE (((tblDocs.Section) <> [Forms]![frmDocumentRegister]![frmSetup2].[Form]![Group Filter])  And ((tblDocs.Active) = True
)   And ((IIf(IsNull([Forms]![frmDocumentRegister]![FilterBy]) , True, [tblDocs].[Section] = [Forms]![frmDocumentRegister]![FilterBy])) <> False)  And ((IIf(IsNull([Forms]![frmDocumentRegister]![FilterBy0]) , True, [tblDocs].[Document Type] = [Forms]![frmDocumentRegister]![FilterBy0])) <> False)  And ((IIf(IsNull([Forms]![frmDocumentRegister]![Like Doc]) , True, [tblDocs].[Document Code]  Like [Forms]![frmDocumentRegister]![Like Doc])) <> False)  And ((IIf(IsNull([Forms]![frmDocumentRegister]![Like Title]), True, [tblDocs].[Document Title] Like [Forms]![frmDocumentRegister]![Like Title])) <> False))

I am not just asking for the answer but would like to understand why aswell.

Thanks
 

namliam

The Mailman - AWF VIP
Local time
Today, 22:41
Joined
Aug 11, 2003
Messages
11,695
Tip1: So let met ask you this question, why are you using / mixing ' and " in your code?
Tip2: Look closely at your DLookup's (which I hate with a pasion!)
 

Ruzz2k

Registered User.
Local time
Today, 21:41
Joined
Apr 28, 2012
Messages
102
To differentiate text from the sql string?
is this bit the issue ,'[Document Type] = 'Quality Manual'') from the dlookup?
am i close?
 

pr2-eugin

Super Moderator
Local time
Today, 21:41
Joined
Nov 30, 2011
Messages
8,494
Why are you using Domain functions in Queries? It is inefficient, clumsy and messy. Try to JOIN them, much faster ans simpler to understand.
 

Ruzz2k

Registered User.
Local time
Today, 21:41
Joined
Apr 28, 2012
Messages
102
Hi Namilam, how do I fix this? I still not sure how to fix the quotes.
Thanks
 

namliam

The Mailman - AWF VIP
Local time
Today, 22:41
Joined
Aug 11, 2003
Messages
11,695
The reason you are using single quotes inside the double quotes to start with....

normally if you use double quotes inside double quotes you need to "escape them"
Breaks: msgbox "this is a "quote" test"
Works: msgbox "this is a "quote" test"
Works: msgbox "this is a 'quote' test"

Prefered method to fix this is to replace the ' by ""
I believe something along these lines should work
"....,""[Document Type] = """"Quality Manual"""" "") ... "
Or perhaps
"....,""[Document Type] = ""Quality Manual"" "") ... "
or if you prefer you can try working with CHR(34)
 

Ruzz2k

Registered User.
Local time
Today, 21:41
Joined
Apr 28, 2012
Messages
102
Hi, thanks for that I have used the theory and now I get the following error run time error 3065 too few parameters. Expected 5

any ideas what this means?

Thanks
 

namliam

The Mailman - AWF VIP
Local time
Today, 22:41
Joined
Aug 11, 2003
Messages
11,695
Post your sql, impossible to say without your current SQL. Odds are you messed up your quoting someplace.
 

namliam

The Mailman - AWF VIP
Local time
Today, 22:41
Joined
Aug 11, 2003
Messages
11,695
Would also like to (re)point you to the post of PR2:
Why are you using Domain functions in Queries? It is inefficient, clumsy and messy. Try to JOIN them, much faster ans simpler to understand.
 

Ruzz2k

Registered User.
Local time
Today, 21:41
Joined
Apr 28, 2012
Messages
102
Hi Namilam, I dont know how to do that as I believe I would have to rewrite the whole access database. I am only using domain functions as I feel there is no easy way around what I am trying to acheive.

here is the code with my quotes like you said, it says error 3061 expected 5 I am presuming this means I have syntax error in 5 places?

Problem is my source is a query and thus is based on the form and is pulling data from tables and querys a combination of places and I just dont know where I am going wrong.

Thanks for your help.

Code:
strSQL = " SELECT tblDocs.Section "
    strSQL = strSQL & " , tblDocs.[Document Code] "
    strSQL = strSQL & " , tblDocs.[Document Title] "
    strSQL = strSQL & " , tblDocs.[Document Type] "
    strSQL = strSQL & " , qryCurrent.[Reason for Change] "
    strSQL = strSQL & " , qryCurrent.[Issue Number] "
    strSQL = strSQL & " , qryCurrent.[Issue Date] "
    strSQL = strSQL & " , tblDocumentType.[Intranet Prefix] "
    strSQL = strSQL & " , tblDocs.[Intranet Suffix] "
    strSQL = strSQL & " , IIf([tblDocs].[Active]=0 "
    strSQL = strSQL & " , ""Withdrawn"" "
    strSQL = strSQL & " , IIf([tblDocs].[Section]=""Group"" "
    strSQL = strSQL & " , DLookUp(""[Intranet Prefix]"",""tblDocumentType"", ""[Document Type] = """"Quality Manual"""" "") & [tblDocs].[Document Code] & [Intranet Suffix] "
    strSQL = strSQL & " , IIf([tblDocs].[Section]=""HSE"" "
    strSQL = strSQL & " , DLookUp(""[Intranet Prefix]"",""tblDocumentType"",""[Document Type] = """"LWI"""" "") & [tblDocs].[Document Code] & [Intranet Suffix] "
    strSQL = strSQL & " , ([Intranet Prefix] & [tblDocs].[Document Code] & [Intranet Suffix])))) AS iLink  "
    strSQL = strSQL & " FROM (tblDocumentType  "
    strSQL = strSQL & " INNER JOIN tblDocs ON tblDocumentType.[Document Type] = tblDocs.[Document Type])  "
    strSQL = strSQL & " INNER JOIN qryCurrent ON tblDocs.[Document Code] = qryCurrent.[Document Code] "
    strSQL = strSQL & " WHERE (((tblDocs.Section) <> [Forms]![frmDocumentRegister]![frmSetup2].[Form]![Group Filter])  "
    strSQL = strSQL & " And ((tblDocs.Active) = True)  "
    strSQL = strSQL & " And ((IIf(IsNull([Forms]![frmDocumentRegister]![FilterBy]) , True, [tblDocs].[Section] = [Forms]![frmDocumentRegister]![FilterBy])) <> False) "
    strSQL = strSQL & " And ((IIf(IsNull([Forms]![frmDocumentRegister]![FilterBy0]) , True, [tblDocs].[Document Type] = [Forms]![frmDocumentRegister]![FilterBy0])) <> False) "
    strSQL = strSQL & " And ((IIf(IsNull([Forms]![frmDocumentRegister]![Like Doc]) , True, [tblDocs].[Document Code]  Like [Forms]![frmDocumentRegister]![Like Doc])) <> False) "
    strSQL = strSQL & " And ((IIf(IsNull([Forms]![frmDocumentRegister]![Like Title]), True, [tblDocs].[Document Title] Like [Forms]![frmDocumentRegister]![Like Title])) <> False)) "
 

Users who are viewing this thread

Top Bottom