CreatQueryDef In Access 2000 Doesn't Work? 
Author Message
 CreatQueryDef In Access 2000 Doesn't Work?

I am using Access 2000 and want to create a query using VBA that is the data
source for a report.  When I do this with Access 97 I have been very
successful. Unfortunately, Access 2000 does not add the SQL property to the
created query. All I get for the SQL statement is "SELECT; ".  Reviewed many
references, including MSDN and "Access 2000 Developers Handbook" authored by
Ken Getz and Paul Litwin with no success.
Any assistance you can provide will be of great help.
The following example I wrote in Access 2000 does not work. The query is
created, but the SQL statement (qryDef.SQL = strSQL) has only the first word
from strSQL ("SELECT; ").
*******begin Access 2000 example*****************************
Function CreateVisitReport(BeginDt As Date, EndDt As Date)
    Dim strSQL as String
    Dim strSQL1 as String
    Dim Dbm As DAO.Database
    Set Dbm = CurrentDb()
    On Error Resume Next
    Dbm.QueryDefs.Delete "qryVisitReport" 'Just in case there is an old copy
in the DB
    Dim qryDef As DAO.QueryDef
    Set qryDef = Dbm.CreateQueryDef("qryVisitReport")
    strSQL1 = " SELECT tblVisitData.Session_Date, tblVisitData.Visitor, "
    strSQL1 = strSQL1 & " tblVisitData.Agency, tblVisitData.POC_PhoneNmr, "
    strSQL1 = strSQL1 & " tblVisitData.POC_OrgNmr,
tblVisitData.Business_Grp, "
    strSQL1 = strSQL1 & " tblVisitData.TypeUsage, tblVisitData.Qty_Visitors,
"
    strSQL1 = strSQL1 & " tblVisitData.POC_LastName "
    strSQL1 = strSQL1 & " tblVisitData.POC_FirstName "
    strSQL1 = strSQL1 & " FROM tblVisitData "
    strSQL1 = strSQL1 & " WHERE tblVisitData.Session_Date "
    strSQL = strSQL1 & " Between #" & BeginDt & "# And #" & EndDt & "#; "
    'The following line results in an SQL statement that contains only
"SELECT; "
    qryDef.SQL = strSQL
    Select Case Form_frmMenu!optRptGroup
        Case 1
            DoCmd.OpenReport "rptVisitDataAgency", acViewPreview,
"qryVisitReport"
        Case 2
            DoCmd.OpenReport "rptVisitDataBusGrp", acViewPreview,
"qryVisitReport"
        Case 3
            DoCmd.OpenReport "rptVisitDataType", acViewPreview,
"qryVisitReport"
    End Select
    Dbm.QueryDefs.Delete "qryVisitReport"
    Set qryDef = Nothing
    Dbm.Close
    Set Dbm = Nothing
End Function
********end of Access 2000 example**************************************
The following example from my Access 97 database works just fine.

********begin Access 97 example************************************
Sub CreateAllRecordsReport(ProjStr As String, ProjTbl As String)
    Dim strSQL as String
    Dim strSQL1 as String
    Dim qryDef As QueryDef
    FindBookDate 'This is a routine to find specific book dates selected on
a form for the query
    If Len(ProjStr) < 30 Then
        ProjStr1 = ProjStr & ".*"
    Else
        ProjStr1 = ProjStr
    End If
    Set Db = CurrentDb '("DeptAcctAnal.mdb")
    Set qryDef = Db.CreateQueryDef("qryAllRecords")
    strSQL1 = " SELECT " & ProjTbl & ".[Book Date], " & ProjTbl & ".Account,
"
    strSQL1 = strSQL1 & ProjTbl & ".Project, PL_LineNmr.LineNmr, " & ProjTbl
& ".Organization, "
    strSQL1 = strSQL1 & ProjTbl & ".[Transaction Amount], " & ProjTbl &
".[Project Labor Category (PLC)], "
    strSQL1 = strSQL1 & ProjTbl & ".Hours, " & ProjTbl & ".[Vend, Empl, Cust
or Subcontr ID], "
    strSQL1 = strSQL1 & ProjTbl & ".Name, " & ProjTbl & ".[Timesheet Date],
PL_LineNmr.PLGrpNmr, ([PL_Acct].[LineNmr]) AS IntLineNmr, "
    strSQL1 = strSQL1 & " PL_Acct.AccountDescription, PL_LineNmr.PL_Line,
tblDateNumber.BeginCalDt, "
    strSQL1 = strSQL1 & " tblDateNumber.BeginNmrChk "
    strSQL1 = strSQL1 & " FROM ((PL_Acct INNER JOIN PL_LineNmr ON
PL_Acct.LineNmr = PL_LineNmr.LineNmr) "
    strSQL1 = strSQL1 & " INNER JOIN " & ProjTbl & " ON PL_Acct.Account = "
& ProjTbl & ".Account) "
    strSQL1 = strSQL1 & " INNER JOIN tblDateNumber ON " & ProjTbl & ".[Book
Date] = tblDateNumber.BeginCalDt "
    strSQL1 = strSQL1 & " WHERE ((( " & ProjTbl & ".Project) Like " &
conQuote & ProjStr1 & conQuote
    strSQL1 = strSQL1 & " ) AND ((tblDateNumber.BeginNmrChk) Between " &
BeginBkDtChk & " And " & EndBkDtChk & " )) "
    strSQL1 = strSQL1 & " AND PL_LineNmr.LineNmr Between 1 And 14 Or "
    strSQL1 = strSQL1 & " ((( " & ProjTbl & ".Project) Like " & conQuote &
ProjStr & conQuote
    strSQL1 = strSQL1 & " ) AND ((tblDateNumber.BeginNmrChk) Between " &
BeginBkDtChk & " And " & EndBkDtChk & " )) "
    strSQL1 = strSQL1 & " AND PL_LineNmr.LineNmr Between 1 And 14 "
    strSQL = strSQL1 & " ORDER BY " & ProjTbl & ".Organization;"
    qryDef.SQL = strSQL
    Select Case Form_frmMenu!optGroupBy
        Case 1
            DoCmd.OpenReport "rptAllRecords", acViewPreview, "qryAllRecords"
        Case 2
            DoCmd.OpenReport "rptAllRecordsProj", acViewPreview,
"qryAllRecords"
        Case 3
            DoCmd.OpenReport "rptAllRecordsPLC", acViewPreview,
"qryAllRecords"
    End Select
    Db.QueryDefs.Delete "qryAllRecords"
    Set qryDef = Nothing
    Set Db = Nothing
End Sub
**********end Access 97 example********************



Sun, 22 Jun 2003 00:24:14 GMT  
 CreatQueryDef In Access 2000 Doesn't Work?
Problem solved !!!!
Thank you for you interest and help.  The help you gave me made me revisit
the problem and discover the answer.
I ignored a rule of thumb for developers --- that it is good practice to
build in Error Traping in all your procedures, but you should TURN OFF error
traping during the development phase.  My small mistake was masked by the On
error Resume Next command.  Access 2000 is working as advertised and my
routine works just like it did in Access 97.  The problem  --  my SQL
statement had a missing operator, the comma between fields in the SELECT
portion of the SQL statement.  With the On Error Resume Next active the
software correctly resumed next.  Turning On Error off allowed VBA to tell
me that the SQL statement was missing an operator.  I corrected the
statement and all works well.

Your humble servant,
Henry


Quote:
> I am using Access 2000 and want to create a query using VBA that is the
data
> source for a report.  When I do this with Access 97 I have been very
> successful. Unfortunately, Access 2000 does not add the SQL property to
the
> created query. All I get for the SQL statement is "SELECT; ".  Reviewed
many
> references, including MSDN and "Access 2000 Developers Handbook" authored
by
> Ken Getz and Paul Litwin with no success.
> Any assistance you can provide will be of great help.
> The following example I wrote in Access 2000 does not work. The query is
> created, but the SQL statement (qryDef.SQL = strSQL) has only the first
word
> from strSQL ("SELECT; ").
> *******begin Access 2000 example*****************************
> Function CreateVisitReport(BeginDt As Date, EndDt As Date)
>     Dim strSQL as String
>     Dim strSQL1 as String
>     Dim Dbm As DAO.Database
>     Set Dbm = CurrentDb()
>     On Error Resume Next
>     Dbm.QueryDefs.Delete "qryVisitReport" 'Just in case there is an old
copy
> in the DB
>     Dim qryDef As DAO.QueryDef
>     Set qryDef = Dbm.CreateQueryDef("qryVisitReport")
>     strSQL1 = " SELECT tblVisitData.Session_Date, tblVisitData.Visitor, "
>     strSQL1 = strSQL1 & " tblVisitData.Agency, tblVisitData.POC_PhoneNmr,
"
>     strSQL1 = strSQL1 & " tblVisitData.POC_OrgNmr,
> tblVisitData.Business_Grp, "
>     strSQL1 = strSQL1 & " tblVisitData.TypeUsage,

tblVisitData.Qty_Visitors,

- Show quoted text -

Quote:
> "
>     strSQL1 = strSQL1 & " tblVisitData.POC_LastName "
>     strSQL1 = strSQL1 & " tblVisitData.POC_FirstName "
>     strSQL1 = strSQL1 & " FROM tblVisitData "
>     strSQL1 = strSQL1 & " WHERE tblVisitData.Session_Date "
>     strSQL = strSQL1 & " Between #" & BeginDt & "# And #" & EndDt & "#; "
>     'The following line results in an SQL statement that contains only
> "SELECT; "
>     qryDef.SQL = strSQL
>     Select Case Form_frmMenu!optRptGroup
>         Case 1
>             DoCmd.OpenReport "rptVisitDataAgency", acViewPreview,
> "qryVisitReport"
>         Case 2
>             DoCmd.OpenReport "rptVisitDataBusGrp", acViewPreview,
> "qryVisitReport"
>         Case 3
>             DoCmd.OpenReport "rptVisitDataType", acViewPreview,
> "qryVisitReport"
>     End Select
>     Dbm.QueryDefs.Delete "qryVisitReport"
>     Set qryDef = Nothing
>     Dbm.Close
>     Set Dbm = Nothing
> End Function
> ********end of Access 2000 example**************************************
> The following example from my Access 97 database works just fine.

> ********begin Access 97 example************************************
> Sub CreateAllRecordsReport(ProjStr As String, ProjTbl As String)
>     Dim strSQL as String
>     Dim strSQL1 as String
>     Dim qryDef As QueryDef
>     FindBookDate 'This is a routine to find specific book dates selected
on
> a form for the query
>     If Len(ProjStr) < 30 Then
>         ProjStr1 = ProjStr & ".*"
>     Else
>         ProjStr1 = ProjStr
>     End If
>     Set Db = CurrentDb '("DeptAcctAnal.mdb")
>     Set qryDef = Db.CreateQueryDef("qryAllRecords")
>     strSQL1 = " SELECT " & ProjTbl & ".[Book Date], " & ProjTbl &
".Account,
> "
>     strSQL1 = strSQL1 & ProjTbl & ".Project, PL_LineNmr.LineNmr, " &
ProjTbl
> & ".Organization, "
>     strSQL1 = strSQL1 & ProjTbl & ".[Transaction Amount], " & ProjTbl &
> ".[Project Labor Category (PLC)], "
>     strSQL1 = strSQL1 & ProjTbl & ".Hours, " & ProjTbl & ".[Vend, Empl,
Cust
> or Subcontr ID], "
>     strSQL1 = strSQL1 & ProjTbl & ".Name, " & ProjTbl & ".[Timesheet
Date],
> PL_LineNmr.PLGrpNmr, ([PL_Acct].[LineNmr]) AS IntLineNmr, "
>     strSQL1 = strSQL1 & " PL_Acct.AccountDescription, PL_LineNmr.PL_Line,
> tblDateNumber.BeginCalDt, "
>     strSQL1 = strSQL1 & " tblDateNumber.BeginNmrChk "
>     strSQL1 = strSQL1 & " FROM ((PL_Acct INNER JOIN PL_LineNmr ON
> PL_Acct.LineNmr = PL_LineNmr.LineNmr) "
>     strSQL1 = strSQL1 & " INNER JOIN " & ProjTbl & " ON PL_Acct.Account =
"
> & ProjTbl & ".Account) "
>     strSQL1 = strSQL1 & " INNER JOIN tblDateNumber ON " & ProjTbl &
".[Book
> Date] = tblDateNumber.BeginCalDt "
>     strSQL1 = strSQL1 & " WHERE ((( " & ProjTbl & ".Project) Like " &
> conQuote & ProjStr1 & conQuote
>     strSQL1 = strSQL1 & " ) AND ((tblDateNumber.BeginNmrChk) Between " &
> BeginBkDtChk & " And " & EndBkDtChk & " )) "
>     strSQL1 = strSQL1 & " AND PL_LineNmr.LineNmr Between 1 And 14 Or "
>     strSQL1 = strSQL1 & " ((( " & ProjTbl & ".Project) Like " & conQuote &
> ProjStr & conQuote
>     strSQL1 = strSQL1 & " ) AND ((tblDateNumber.BeginNmrChk) Between " &
> BeginBkDtChk & " And " & EndBkDtChk & " )) "
>     strSQL1 = strSQL1 & " AND PL_LineNmr.LineNmr Between 1 And 14 "
>     strSQL = strSQL1 & " ORDER BY " & ProjTbl & ".Organization;"
>     qryDef.SQL = strSQL
>     Select Case Form_frmMenu!optGroupBy
>         Case 1
>             DoCmd.OpenReport "rptAllRecords", acViewPreview,
"qryAllRecords"
>         Case 2
>             DoCmd.OpenReport "rptAllRecordsProj", acViewPreview,
> "qryAllRecords"
>         Case 3
>             DoCmd.OpenReport "rptAllRecordsPLC", acViewPreview,
> "qryAllRecords"
>     End Select
>     Db.QueryDefs.Delete "qryAllRecords"
>     Set qryDef = Nothing
>     Set Db = Nothing
> End Sub
> **********end Access 97 example********************



Sun, 22 Jun 2003 02:17:05 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. An access 95 program doesn't work on access 2000

2. ADO Help doesn't work from within Access 2000

3. OpenDatabase doesn't work in Access 2000

4. ADO Help doesn't appear (Access 2000, Windows 2000)

5. Win 2000 Offline files doesn't work with mdb

6. Why doesn't this code work on win 2000

7. Code doesn't work on windows 2000

8. Gettype for Windows 2000 doesn't work?

9. ListType Property doesn't work with Word 2000

10. GetOpenFileName32 doesn't work under Win 2000

11. How ironic, the HPC 2000 SDK Download page doesn't work

12. Program from access 97 won't work in access 2000

 

 
Powered by phpBB® Forum Software