PC Review


Reply
Thread Tools Rate Thread

CreateQueryDef question

 
 
=?Utf-8?B?SmVmZkg=?=
Guest
Posts: n/a
 
      23rd Mar 2006
I need to locate the last page number in an "Indexes" table that I have and
then use it as the beginning number in a second module. The "Indexes" table
has two fields, a description and page number.

Specifically I am unclear what the 1st argument in the CreateQueryDef should
be. I have "PageNumberLast" in the CreateQueryDef but I this can't be
correct. I thought that I would actually be creating this variable that I
could then use to reset the page count (intPageCounter).

Here is what I have tried (I tried to supply only what we need):

Dim db5 As DAO.Database
Dim intPageCounter as Integer
Dim intLastPageNo as Integer
Dim strSQL as String

Function InitToc3()
'Called from the OnOpen property of the report.

Dim qd5 As DAO.QueryDef
Set db5=CurrentDB()

'Set the page number to the last page used in the "Indexes" table (I believe
this is were my problems begin)
strSQL="Select Max(Indexes,[PageNumber] from [Indexes]'
Set qd5=db5.CreateQueryDef("PageNumberLast",strSQL)

qd5.Execute
qd5.Close

intPageCounter=PageNumberLast

I was hoping to have a query that would identify the maximum page number in
the "Indexes" table and then use that to set "intPageCounter"

I am a newbie, but learning lots. If there is an easier way I open to making
life simplier.
 
Reply With Quote
 
 
 
 
Marshall Barton
Guest
Posts: n/a
 
      23rd Mar 2006
JeffH wrote:

>I need to locate the last page number in an "Indexes" table that I have and
>then use it as the beginning number in a second module. The "Indexes" table
>has two fields, a description and page number.
>
>Specifically I am unclear what the 1st argument in the CreateQueryDef should
>be. I have "PageNumberLast" in the CreateQueryDef but I this can't be
>correct. I thought that I would actually be creating this variable that I
>could then use to reset the page count (intPageCounter).
>
>Here is what I have tried (I tried to supply only what we need):
>
>Dim db5 As DAO.Database
>Dim intPageCounter as Integer
>Dim intLastPageNo as Integer
>Dim strSQL as String
>
>Function InitToc3()
> 'Called from the OnOpen property of the report.
>
> Dim qd5 As DAO.QueryDef
> Set db5=CurrentDB()
>
>'Set the page number to the last page used in the "Indexes" table (I believe
>this is were my problems begin)
> strSQL="Select Max(Indexes,[PageNumber] from [Indexes]'
> Set qd5=db5.CreateQueryDef("PageNumberLast",strSQL)
>
> qd5.Execute
> qd5.Close
>
> intPageCounter=PageNumberLast
>
>I was hoping to have a query that would identify the maximum page number in
>the "Indexes" table and then use that to set "intPageCounter"
>
>I am a newbie, but learning lots. If there is an easier way I open to making
>life simplier.



This is the long way around, even ignoring that Execute only
operates on action queries, not on Select queries.

I think all you need to use is the DMax function:

Dim intLastPageNo as Integer
intPageCounter = DMax("PageNumber", "Indexes")

--
Marsh
MVP [MS Access]
 
Reply With Quote
 
Brian Wilson
Guest
Posts: n/a
 
      23rd Mar 2006

"JeffH" <(E-Mail Removed)> wrote in message
news:6A7DFE08-693D-486D-8148-(E-Mail Removed)...
>I need to locate the last page number in an "Indexes" table that I have and
> then use it as the beginning number in a second module. The "Indexes"
> table
> has two fields, a description and page number.
>
> Specifically I am unclear what the 1st argument in the CreateQueryDef
> should
> be. I have "PageNumberLast" in the CreateQueryDef but I this can't be
> correct. I thought that I would actually be creating this variable that I
> could then use to reset the page count (intPageCounter).
>
> Here is what I have tried (I tried to supply only what we need):
>
> Dim db5 As DAO.Database
> Dim intPageCounter as Integer
> Dim intLastPageNo as Integer
> Dim strSQL as String
>
> Function InitToc3()
> 'Called from the OnOpen property of the report.
>
> Dim qd5 As DAO.QueryDef
> Set db5=CurrentDB()
>
> 'Set the page number to the last page used in the "Indexes" table (I
> believe
> this is were my problems begin)
> strSQL="Select Max(Indexes,[PageNumber] from [Indexes]'
> Set qd5=db5.CreateQueryDef("PageNumberLast",strSQL)
>
> qd5.Execute
> qd5.Close
>
> intPageCounter=PageNumberLast
>
> I was hoping to have a query that would identify the maximum page number
> in
> the "Indexes" table and then use that to set "intPageCounter"
>
> I am a newbie, but learning lots. If there is an easier way I open to
> making
> life simplier.



Well, if we start by making it a bit simpler then how about one line?

intLastPageNo = CInt(Nz(DMax("PageNumber", "Indexes"), 0))

If you are looking up the maximum value in a table, then this is all you
need. Your use of dbs.Execute and the QueryDef object are both pretty far
from how they should be used, but if you wanted an example of how to do
things the not-so-simple way, see the code below. The reason for avoiding
the easy dmax function shown above is to increase speed, but if you call the
function only once per routine you will not notice any difference:


Public Function GetMaxIndex() As Long

On Error GoTo Err_Handler

Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Dim lngReturn As Long

lngReturn = -1

strSQL = "SELECT Max(PageNumber) FROM Indexes"

Set dbs = CurrentDb

Set rst = dbs.OpenRecordset(strSQL, dbOpenForwardOnly, dbReadOnly)

If Not rst.EOF Then
lngReturn = Nz(rst.Fields(0), 0)
End If

Exit_Handler:

GetMaxIndex = lngReturn

If Not rst Is Nothing Then
rst.Close
Set rst = Nothing
End If

If Not dbs Is Nothing Then
Set dbs = Nothing
End If

Exit Function

Err_Handler:
' No error message just let the function return a negative number
Resume Exit_Handler

End Function




 
Reply With Quote
 
=?Utf-8?B?SmVmZkg=?=
Guest
Posts: n/a
 
      24th Mar 2006
Thanks I tried the simple solution but then I got the error message:

Run time error 3075
Syntax error (missing operator) in query expression 'Max(Page Number)'

My one line of code is :

intPageCounter = CInt(Nz(DMax("Page Number", "Indexes"), 0))


"Brian Wilson" wrote:

>
> "JeffH" <(E-Mail Removed)> wrote in message
> news:6A7DFE08-693D-486D-8148-(E-Mail Removed)...
> >I need to locate the last page number in an "Indexes" table that I have and
> > then use it as the beginning number in a second module. The "Indexes"
> > table
> > has two fields, a description and page number.
> >
> > Specifically I am unclear what the 1st argument in the CreateQueryDef
> > should
> > be. I have "PageNumberLast" in the CreateQueryDef but I this can't be
> > correct. I thought that I would actually be creating this variable that I
> > could then use to reset the page count (intPageCounter).
> >
> > Here is what I have tried (I tried to supply only what we need):
> >
> > Dim db5 As DAO.Database
> > Dim intPageCounter as Integer
> > Dim intLastPageNo as Integer
> > Dim strSQL as String
> >
> > Function InitToc3()
> > 'Called from the OnOpen property of the report.
> >
> > Dim qd5 As DAO.QueryDef
> > Set db5=CurrentDB()
> >
> > 'Set the page number to the last page used in the "Indexes" table (I
> > believe
> > this is were my problems begin)
> > strSQL="Select Max(Indexes,[PageNumber] from [Indexes]'
> > Set qd5=db5.CreateQueryDef("PageNumberLast",strSQL)
> >
> > qd5.Execute
> > qd5.Close
> >
> > intPageCounter=PageNumberLast
> >
> > I was hoping to have a query that would identify the maximum page number
> > in
> > the "Indexes" table and then use that to set "intPageCounter"
> >
> > I am a newbie, but learning lots. If there is an easier way I open to
> > making
> > life simplier.

>
>
> Well, if we start by making it a bit simpler then how about one line?
>
> intLastPageNo = CInt(Nz(DMax("PageNumber", "Indexes"), 0))
>
> If you are looking up the maximum value in a table, then this is all you
> need. Your use of dbs.Execute and the QueryDef object are both pretty far
> from how they should be used, but if you wanted an example of how to do
> things the not-so-simple way, see the code below. The reason for avoiding
> the easy dmax function shown above is to increase speed, but if you call the
> function only once per routine you will not notice any difference:
>
>
> Public Function GetMaxIndex() As Long
>
> On Error GoTo Err_Handler
>
> Dim dbs As DAO.Database
> Dim rst As DAO.Recordset
> Dim strSQL As String
> Dim lngReturn As Long
>
> lngReturn = -1
>
> strSQL = "SELECT Max(PageNumber) FROM Indexes"
>
> Set dbs = CurrentDb
>
> Set rst = dbs.OpenRecordset(strSQL, dbOpenForwardOnly, dbReadOnly)
>
> If Not rst.EOF Then
> lngReturn = Nz(rst.Fields(0), 0)
> End If
>
> Exit_Handler:
>
> GetMaxIndex = lngReturn
>
> If Not rst Is Nothing Then
> rst.Close
> Set rst = Nothing
> End If
>
> If Not dbs Is Nothing Then
> Set dbs = Nothing
> End If
>
> Exit Function
>
> Err_Handler:
> ' No error message just let the function return a negative number
> Resume Exit_Handler
>
> End Function
>
>
>
>
> .
>

 
Reply With Quote
 
=?Utf-8?B?SmVmZkg=?=
Guest
Posts: n/a
 
      24th Mar 2006
I just figured out my problem. It works great!!!! You were a huge help.

"JeffH" wrote:

> Thanks I tried the simple solution but then I got the error message:
>
> Run time error 3075
> Syntax error (missing operator) in query expression 'Max(Page Number)'
>
> My one line of code is :
>
> intPageCounter = CInt(Nz(DMax("Page Number", "Indexes"), 0))
>
>
> "Brian Wilson" wrote:
>
> >
> > "JeffH" <(E-Mail Removed)> wrote in message
> > news:6A7DFE08-693D-486D-8148-(E-Mail Removed)...
> > >I need to locate the last page number in an "Indexes" table that I have and
> > > then use it as the beginning number in a second module. The "Indexes"
> > > table
> > > has two fields, a description and page number.
> > >
> > > Specifically I am unclear what the 1st argument in the CreateQueryDef
> > > should
> > > be. I have "PageNumberLast" in the CreateQueryDef but I this can't be
> > > correct. I thought that I would actually be creating this variable that I
> > > could then use to reset the page count (intPageCounter).
> > >
> > > Here is what I have tried (I tried to supply only what we need):
> > >
> > > Dim db5 As DAO.Database
> > > Dim intPageCounter as Integer
> > > Dim intLastPageNo as Integer
> > > Dim strSQL as String
> > >
> > > Function InitToc3()
> > > 'Called from the OnOpen property of the report.
> > >
> > > Dim qd5 As DAO.QueryDef
> > > Set db5=CurrentDB()
> > >
> > > 'Set the page number to the last page used in the "Indexes" table (I
> > > believe
> > > this is were my problems begin)
> > > strSQL="Select Max(Indexes,[PageNumber] from [Indexes]'
> > > Set qd5=db5.CreateQueryDef("PageNumberLast",strSQL)
> > >
> > > qd5.Execute
> > > qd5.Close
> > >
> > > intPageCounter=PageNumberLast
> > >
> > > I was hoping to have a query that would identify the maximum page number
> > > in
> > > the "Indexes" table and then use that to set "intPageCounter"
> > >
> > > I am a newbie, but learning lots. If there is an easier way I open to
> > > making
> > > life simplier.

> >
> >
> > Well, if we start by making it a bit simpler then how about one line?
> >
> > intLastPageNo = CInt(Nz(DMax("PageNumber", "Indexes"), 0))
> >
> > If you are looking up the maximum value in a table, then this is all you
> > need. Your use of dbs.Execute and the QueryDef object are both pretty far
> > from how they should be used, but if you wanted an example of how to do
> > things the not-so-simple way, see the code below. The reason for avoiding
> > the easy dmax function shown above is to increase speed, but if you call the
> > function only once per routine you will not notice any difference:
> >
> >
> > Public Function GetMaxIndex() As Long
> >
> > On Error GoTo Err_Handler
> >
> > Dim dbs As DAO.Database
> > Dim rst As DAO.Recordset
> > Dim strSQL As String
> > Dim lngReturn As Long
> >
> > lngReturn = -1
> >
> > strSQL = "SELECT Max(PageNumber) FROM Indexes"
> >
> > Set dbs = CurrentDb
> >
> > Set rst = dbs.OpenRecordset(strSQL, dbOpenForwardOnly, dbReadOnly)
> >
> > If Not rst.EOF Then
> > lngReturn = Nz(rst.Fields(0), 0)
> > End If
> >
> > Exit_Handler:
> >
> > GetMaxIndex = lngReturn
> >
> > If Not rst Is Nothing Then
> > rst.Close
> > Set rst = Nothing
> > End If
> >
> > If Not dbs Is Nothing Then
> > Set dbs = Nothing
> > End If
> >
> > Exit Function
> >
> > Err_Handler:
> > ' No error message just let the function return a negative number
> > Resume Exit_Handler
> >
> > End Function
> >
> >
> >
> >
> > .
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Createquerydef dcollins_92117 Microsoft Access ADP SQL Server 1 4th Jun 2009 10:09 PM
CreateQueryDef JimP Microsoft Access VBA Modules 6 10th Apr 2007 05:21 PM
CreateQueryDef =?Utf-8?B?U3RldmVu?= Microsoft Access VBA Modules 3 11th Mar 2007 02:58 PM
CreateQueryDef Problem =?Utf-8?B?Q2hyaXMgR3V5bm4=?= Microsoft Access 2 17th Nov 2006 05:48 PM
question about CreateQueryDef Daama via AccessMonster.com Microsoft Access Form Coding 1 6th Jun 2006 02:10 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:45 AM.