Connecting Tech Pros Worldwide Forums | Help | Site Map

access 2003: division operator problem

JMCN
Guest
 
Posts: n/a
#1: Nov 13 '05
hello
i am not getting the correct percentage results from the following
code. the "prelimpercent" variable = 0? why would that be the case
when i try to divide numrecs by numrecs2?

any advise would be appreciated. thanks in advance - jung
************************************************** ***********************

Dim rst, rst2 As DAO.Recordset
Dim db As DAO.Database
Dim numrecs, numrecs2 As Integer
Dim percent, prelimpercent As Integer
Dim x As String

Set db = CurrentDb
'counts current records
x = Me![SQLStatement]
Set rst = db.OpenRecordset(x)
With rst
If rst.EOF = False Then
rst.MoveLast
numrecs = rst.RecordCount
Debug.Print numrecs '(19)
Else
numrecs = 0
End If
End With

'count records in qryAllOpenItems
Set rst2 = db.OpenRecordset("SELECT [Exception Tracking].*, [Loan
Tracking].[LT DB Database], [Loan Tracking].[LT Internal Number] FROM
[Loan Tracking] INNER JOIN [Exception Tracking] ON [Loan Tracking].[LT
Loan Shortname] = [Exception Tracking].[ET LN Shortname] WHERE
((((((Not [ET Closed]=-1))) AND ((Not [ET Amount]=0))) AND ((Not [ET
Erroneous Entry]=-1))))")
With rst2
If rst2.EOF = False Then
rst2.MoveLast
numrecs2 = rst2.RecordCount
Debug.Print numrecs2 '(74 records)
Else
numrecs2 = 0
End If
End With

prelimpercent = numrecs / numrecs2 '19/74
percent = prelimpercent * 100
Debug.Print percent

MGFoster
Guest
 
Posts: n/a
#2: Nov 13 '05

re: access 2003: division operator problem


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Don't know if this is the solution, just saw it right-off:

Change this:

Dim rst, rst2 As DAO.Recordset
Dim db As DAO.Database
Dim numrecs, numrecs2 As Integer
Dim percent, prelimpercent As Integer

to this:

Dim rst as DAO.Recordset, rst2 As DAO.Recordset
Dim db As DAO.Database
Dim numrecs As Integer, numrecs2 As Integer
Dim percent As Integer, prelimpercent As Integer

Variables dimmed w/o their data type default to Variants.

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQYahTYechKqOuFEgEQLD1QCfegG1dIdt0cioN5RuSKoWN9 yaEOcAoLZX
eYnPuUemIPGtXfXcSp6Xg4TJ
=Dukc
-----END PGP SIGNATURE-----


JMCN wrote:
[color=blue]
> hello
> i am not getting the correct percentage results from the following
> code. the "prelimpercent" variable = 0? why would that be the case
> when i try to divide numrecs by numrecs2?
>
> any advise would be appreciated. thanks in advance - jung
> ************************************************** ***********************
>
> Dim rst, rst2 As DAO.Recordset
> Dim db As DAO.Database
> Dim numrecs, numrecs2 As Integer
> Dim percent, prelimpercent As Integer
> Dim x As String
>
> Set db = CurrentDb
> 'counts current records
> x = Me![SQLStatement]
> Set rst = db.OpenRecordset(x)
> With rst
> If rst.EOF = False Then
> rst.MoveLast
> numrecs = rst.RecordCount
> Debug.Print numrecs '(19)
> Else
> numrecs = 0
> End If
> End With
>
> 'count records in qryAllOpenItems
> Set rst2 = db.OpenRecordset("SELECT [Exception Tracking].*, [Loan
> Tracking].[LT DB Database], [Loan Tracking].[LT Internal Number] FROM
> [Loan Tracking] INNER JOIN [Exception Tracking] ON [Loan Tracking].[LT
> Loan Shortname] = [Exception Tracking].[ET LN Shortname] WHERE
> ((((((Not [ET Closed]=-1))) AND ((Not [ET Amount]=0))) AND ((Not [ET
> Erroneous Entry]=-1))))")
> With rst2
> If rst2.EOF = False Then
> rst2.MoveLast
> numrecs2 = rst2.RecordCount
> Debug.Print numrecs2 '(74 records)
> Else
> numrecs2 = 0
> End If
> End With
>
> prelimpercent = numrecs / numrecs2 '19/74
> percent = prelimpercent * 100
> Debug.Print percent[/color]

MacDermott
Guest
 
Posts: n/a
#3: Nov 13 '05

re: access 2003: division operator problem


You have declared prelimpercent as an integer -
so it will round any value less than .5 to 0.

HTH
- Turtle

"JMCN" <picarama@yahoo.fr> wrote in message
news:2772ee20.0411011229.3f024292@posting.google.c om...[color=blue]
> hello
> i am not getting the correct percentage results from the following
> code. the "prelimpercent" variable = 0? why would that be the case
> when i try to divide numrecs by numrecs2?
>
> any advise would be appreciated. thanks in advance - jung
> ************************************************** ***********************
>
> Dim rst, rst2 As DAO.Recordset
> Dim db As DAO.Database
> Dim numrecs, numrecs2 As Integer
> Dim percent, prelimpercent As Integer
> Dim x As String
>
> Set db = CurrentDb
> 'counts current records
> x = Me![SQLStatement]
> Set rst = db.OpenRecordset(x)
> With rst
> If rst.EOF = False Then
> rst.MoveLast
> numrecs = rst.RecordCount
> Debug.Print numrecs '(19)
> Else
> numrecs = 0
> End If
> End With
>
> 'count records in qryAllOpenItems
> Set rst2 = db.OpenRecordset("SELECT [Exception Tracking].*, [Loan
> Tracking].[LT DB Database], [Loan Tracking].[LT Internal Number] FROM
> [Loan Tracking] INNER JOIN [Exception Tracking] ON [Loan Tracking].[LT
> Loan Shortname] = [Exception Tracking].[ET LN Shortname] WHERE
> ((((((Not [ET Closed]=-1))) AND ((Not [ET Amount]=0))) AND ((Not [ET
> Erroneous Entry]=-1))))")
> With rst2
> If rst2.EOF = False Then
> rst2.MoveLast
> numrecs2 = rst2.RecordCount
> Debug.Print numrecs2 '(74 records)
> Else
> numrecs2 = 0
> End If
> End With
>
> prelimpercent = numrecs / numrecs2 '19/74
> percent = prelimpercent * 100
> Debug.Print percent[/color]


Damien
Guest
 
Posts: n/a
#4: Nov 13 '05

re: access 2003: division operator problem


[re-arranged for readability, bottom posting]
[color=blue]
> JMCN wrote:
>[color=green]
> > hello
> > i am not getting the correct percentage results from the following
> > code. the "prelimpercent" variable = 0? why would that be the case
> > when i try to divide numrecs by numrecs2?
> >
> > any advise would be appreciated. thanks in advance - jung
> > ************************************************** ***********************
> >
> > Dim rst, rst2 As DAO.Recordset
> > Dim db As DAO.Database
> > Dim numrecs, numrecs2 As Integer
> > Dim percent, prelimpercent As Integer
> > Dim x As String
> >
> > Set db = CurrentDb
> > 'counts current records
> > x = Me![SQLStatement]
> > Set rst = db.OpenRecordset(x)
> > With rst
> > If rst.EOF = False Then
> > rst.MoveLast
> > numrecs = rst.RecordCount
> > Debug.Print numrecs '(19)
> > Else
> > numrecs = 0
> > End If
> > End With
> >
> > 'count records in qryAllOpenItems
> > Set rst2 = db.OpenRecordset("SELECT [Exception Tracking].*, [Loan
> > Tracking].[LT DB Database], [Loan Tracking].[LT Internal Number] FROM
> > [Loan Tracking] INNER JOIN [Exception Tracking] ON [Loan Tracking].[LT
> > Loan Shortname] = [Exception Tracking].[ET LN Shortname] WHERE
> > ((((((Not [ET Closed]=-1))) AND ((Not [ET Amount]=0))) AND ((Not [ET
> > Erroneous Entry]=-1))))")
> > With rst2
> > If rst2.EOF = False Then
> > rst2.MoveLast
> > numrecs2 = rst2.RecordCount
> > Debug.Print numrecs2 '(74 records)
> > Else
> > numrecs2 = 0
> > End If
> > End With
> >
> > prelimpercent = numrecs / numrecs2 '19/74
> > percent = prelimpercent * 100
> > Debug.Print percent[/color][/color]

MGFoster <me@privacy.com> wrote in message news:<hjxhd.1178$O11.749@newsread3.news.pas.earthl ink.net>...[color=blue]
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Don't know if this is the solution, just saw it right-off:
>
> Change this:
>
> Dim rst, rst2 As DAO.Recordset
> Dim db As DAO.Database
> Dim numrecs, numrecs2 As Integer
> Dim percent, prelimpercent As Integer
>
> to this:
>
> Dim rst as DAO.Recordset, rst2 As DAO.Recordset
> Dim db As DAO.Database
> Dim numrecs As Integer, numrecs2 As Integer
> Dim percent As Integer, prelimpercent As Integer
>
> Variables dimmed w/o their data type default to Variants.
>
> --
> MGFoster:::mgf00 <at> earthlink <decimal-point> net
> Oakland, CA (USA)
>
> -----BEGIN PGP SIGNATURE-----
> Version: PGP for Personal Privacy 5.0
> Charset: noconv
>
> iQA/AwUBQYahTYechKqOuFEgEQLD1QCfegG1dIdt0cioN5RuSKoWN9 yaEOcAoLZX
> eYnPuUemIPGtXfXcSp6Xg4TJ
> =Dukc
> -----END PGP SIGNATURE-----
>
>[/color]

What you didn't spot is that he's storing his prelimpercent (a number
between 0 and 1) into an INTEGER - which can only be 0 or 1, and so
the result will always be either 0% or 100%, after the multiplication.

Option 1) Store the prelimpercent into a Double,

Option 2) Multiply numrecs by 100 BEFORE doing the division.

Either will work, whatever floats your boat really (although,
obviously, if you want fractional percentages, option 1 is the way to
go).

Damien
JMCN
Guest
 
Posts: n/a
#5: Nov 13 '05

re: access 2003: division operator problem


thank you for your advise! i realized that after writing it that i
should have declared the prelimpercent as double!

thanks again!!!! jung

Damien_The_Unbeliever@hotmail.com (Damien) wrote in message news:<ac70a9d9.0411012342.281f6204@posting.google. com>...[color=blue]
> [re-arranged for readability, bottom posting]
>[color=green]
> > JMCN wrote:
> >[color=darkred]
> > > hello
> > > i am not getting the correct percentage results from the following
> > > code. the "prelimpercent" variable = 0? why would that be the case
> > > when i try to divide numrecs by numrecs2?
> > >
> > > any advise would be appreciated. thanks in advance - jung
> > > ************************************************** ***********************
> > >
> > > Dim rst, rst2 As DAO.Recordset
> > > Dim db As DAO.Database
> > > Dim numrecs, numrecs2 As Integer
> > > Dim percent, prelimpercent As Integer
> > > Dim x As String
> > >
> > > Set db = CurrentDb
> > > 'counts current records
> > > x = Me![SQLStatement]
> > > Set rst = db.OpenRecordset(x)
> > > With rst
> > > If rst.EOF = False Then
> > > rst.MoveLast
> > > numrecs = rst.RecordCount
> > > Debug.Print numrecs '(19)
> > > Else
> > > numrecs = 0
> > > End If
> > > End With
> > >
> > > 'count records in qryAllOpenItems
> > > Set rst2 = db.OpenRecordset("SELECT [Exception Tracking].*, [Loan
> > > Tracking].[LT DB Database], [Loan Tracking].[LT Internal Number] FROM
> > > [Loan Tracking] INNER JOIN [Exception Tracking] ON [Loan Tracking].[LT
> > > Loan Shortname] = [Exception Tracking].[ET LN Shortname] WHERE
> > > ((((((Not [ET Closed]=-1))) AND ((Not [ET Amount]=0))) AND ((Not [ET
> > > Erroneous Entry]=-1))))")
> > > With rst2
> > > If rst2.EOF = False Then
> > > rst2.MoveLast
> > > numrecs2 = rst2.RecordCount
> > > Debug.Print numrecs2 '(74 records)
> > > Else
> > > numrecs2 = 0
> > > End If
> > > End With
> > >
> > > prelimpercent = numrecs / numrecs2 '19/74
> > > percent = prelimpercent * 100
> > > Debug.Print percent[/color][/color]
>
> MGFoster <me@privacy.com> wrote in message news:<hjxhd.1178$O11.749@newsread3.news.pas.earthl ink.net>...[color=green]
> > -----BEGIN PGP SIGNED MESSAGE-----
> > Hash: SHA1
> >
> > Don't know if this is the solution, just saw it right-off:
> >
> > Change this:
> >
> > Dim rst, rst2 As DAO.Recordset
> > Dim db As DAO.Database
> > Dim numrecs, numrecs2 As Integer
> > Dim percent, prelimpercent As Integer
> >
> > to this:
> >
> > Dim rst as DAO.Recordset, rst2 As DAO.Recordset
> > Dim db As DAO.Database
> > Dim numrecs As Integer, numrecs2 As Integer
> > Dim percent As Integer, prelimpercent As Integer
> >
> > Variables dimmed w/o their data type default to Variants.
> >
> > --
> > MGFoster:::mgf00 <at> earthlink <decimal-point> net
> > Oakland, CA (USA)
> >
> > -----BEGIN PGP SIGNATURE-----
> > Version: PGP for Personal Privacy 5.0
> > Charset: noconv
> >
> > iQA/AwUBQYahTYechKqOuFEgEQLD1QCfegG1dIdt0cioN5RuSKoWN9 yaEOcAoLZX
> > eYnPuUemIPGtXfXcSp6Xg4TJ
> > =Dukc
> > -----END PGP SIGNATURE-----
> >
> >[/color]
>
> What you didn't spot is that he's storing his prelimpercent (a number
> between 0 and 1) into an INTEGER - which can only be 0 or 1, and so
> the result will always be either 0% or 100%, after the multiplication.
>
> Option 1) Store the prelimpercent into a Double,
>
> Option 2) Multiply numrecs by 100 BEFORE doing the division.
>
> Either will work, whatever floats your boat really (although,
> obviously, if you want fractional percentages, option 1 is the way to
> go).
>
> Damien[/color]
Closed Thread


Similar Microsoft Access / VBA bytes