finding the max value in a database with delphi
I am working with Delphi 2010. I have a Microsoft Access database called tblUserInfo, in it there is a field called NUMOFREPORTS, containing 11 numbers. I need to search through the data items and find the maximum number and store it in a variable.
My plan was to assign the values in the database to an array of integers and then sort through the array to find the maximum.
This is what I have so far:
i:=1;
while NOT tblUserInfo.eof do
begin
arrNumOfReps[i]:= tblUserInfo['NUMOFREPORTS'];
tblUserInfo.Next;
INC(i);
end;
NumOfReps:= arrNumOfReps[1];
for I := 1 to length(arrNumOfReps) do
begin
if arrNumOfReps[i] > NumOfReps then
begin
NumOfReps:= arrNumOfReps[i];
end;
showmessage(inttostr(NumOfReps));
end;
When I run the program it does not work and breaks at NumOfReps:= arrNumOfReps[1];
and I get an error that says "Access violation".
Does anyone have any corrections to my code or alternative solutions?
Thanks
arrays database delphi delphi-2010
add a comment |
I am working with Delphi 2010. I have a Microsoft Access database called tblUserInfo, in it there is a field called NUMOFREPORTS, containing 11 numbers. I need to search through the data items and find the maximum number and store it in a variable.
My plan was to assign the values in the database to an array of integers and then sort through the array to find the maximum.
This is what I have so far:
i:=1;
while NOT tblUserInfo.eof do
begin
arrNumOfReps[i]:= tblUserInfo['NUMOFREPORTS'];
tblUserInfo.Next;
INC(i);
end;
NumOfReps:= arrNumOfReps[1];
for I := 1 to length(arrNumOfReps) do
begin
if arrNumOfReps[i] > NumOfReps then
begin
NumOfReps:= arrNumOfReps[i];
end;
showmessage(inttostr(NumOfReps));
end;
When I run the program it does not work and breaks at NumOfReps:= arrNumOfReps[1];
and I get an error that says "Access violation".
Does anyone have any corrections to my code or alternative solutions?
Thanks
arrays database delphi delphi-2010
10
You're going about this completely the wrong way. You should be using a Select Max(<columnname.) SQL query agains your table, which is easy to do in Delphi.
– MartynA
Nov 13 '18 at 20:55
1
As @MartynA pointed out, let the database do the work. And read this: stackoverflow.com/questions/17308246/access-violation-in-delphi, look at Mason Wheeler's answer.
– Nic3500
Nov 13 '18 at 21:00
Why do this on the slowest way possible ? Just doselect max(NUMOFREPORTS) from tblUserInfo
and your done. Take @MartynA 's advice
– GuidoG
Nov 14 '18 at 7:50
I have a feelingselect count(NUMOFREPORTS) from tblUserInfo
is more-so what you need. What if you had a gap where a historical record was removed? TheMAX
has nothing to do with theCOUNT
.
– Jerry Dodge
Nov 19 '18 at 0:07
add a comment |
I am working with Delphi 2010. I have a Microsoft Access database called tblUserInfo, in it there is a field called NUMOFREPORTS, containing 11 numbers. I need to search through the data items and find the maximum number and store it in a variable.
My plan was to assign the values in the database to an array of integers and then sort through the array to find the maximum.
This is what I have so far:
i:=1;
while NOT tblUserInfo.eof do
begin
arrNumOfReps[i]:= tblUserInfo['NUMOFREPORTS'];
tblUserInfo.Next;
INC(i);
end;
NumOfReps:= arrNumOfReps[1];
for I := 1 to length(arrNumOfReps) do
begin
if arrNumOfReps[i] > NumOfReps then
begin
NumOfReps:= arrNumOfReps[i];
end;
showmessage(inttostr(NumOfReps));
end;
When I run the program it does not work and breaks at NumOfReps:= arrNumOfReps[1];
and I get an error that says "Access violation".
Does anyone have any corrections to my code or alternative solutions?
Thanks
arrays database delphi delphi-2010
I am working with Delphi 2010. I have a Microsoft Access database called tblUserInfo, in it there is a field called NUMOFREPORTS, containing 11 numbers. I need to search through the data items and find the maximum number and store it in a variable.
My plan was to assign the values in the database to an array of integers and then sort through the array to find the maximum.
This is what I have so far:
i:=1;
while NOT tblUserInfo.eof do
begin
arrNumOfReps[i]:= tblUserInfo['NUMOFREPORTS'];
tblUserInfo.Next;
INC(i);
end;
NumOfReps:= arrNumOfReps[1];
for I := 1 to length(arrNumOfReps) do
begin
if arrNumOfReps[i] > NumOfReps then
begin
NumOfReps:= arrNumOfReps[i];
end;
showmessage(inttostr(NumOfReps));
end;
When I run the program it does not work and breaks at NumOfReps:= arrNumOfReps[1];
and I get an error that says "Access violation".
Does anyone have any corrections to my code or alternative solutions?
Thanks
arrays database delphi delphi-2010
arrays database delphi delphi-2010
edited Nov 14 '18 at 7:47
GuidoG
5,50031844
5,50031844
asked Nov 13 '18 at 20:51
KaleKale
11
11
10
You're going about this completely the wrong way. You should be using a Select Max(<columnname.) SQL query agains your table, which is easy to do in Delphi.
– MartynA
Nov 13 '18 at 20:55
1
As @MartynA pointed out, let the database do the work. And read this: stackoverflow.com/questions/17308246/access-violation-in-delphi, look at Mason Wheeler's answer.
– Nic3500
Nov 13 '18 at 21:00
Why do this on the slowest way possible ? Just doselect max(NUMOFREPORTS) from tblUserInfo
and your done. Take @MartynA 's advice
– GuidoG
Nov 14 '18 at 7:50
I have a feelingselect count(NUMOFREPORTS) from tblUserInfo
is more-so what you need. What if you had a gap where a historical record was removed? TheMAX
has nothing to do with theCOUNT
.
– Jerry Dodge
Nov 19 '18 at 0:07
add a comment |
10
You're going about this completely the wrong way. You should be using a Select Max(<columnname.) SQL query agains your table, which is easy to do in Delphi.
– MartynA
Nov 13 '18 at 20:55
1
As @MartynA pointed out, let the database do the work. And read this: stackoverflow.com/questions/17308246/access-violation-in-delphi, look at Mason Wheeler's answer.
– Nic3500
Nov 13 '18 at 21:00
Why do this on the slowest way possible ? Just doselect max(NUMOFREPORTS) from tblUserInfo
and your done. Take @MartynA 's advice
– GuidoG
Nov 14 '18 at 7:50
I have a feelingselect count(NUMOFREPORTS) from tblUserInfo
is more-so what you need. What if you had a gap where a historical record was removed? TheMAX
has nothing to do with theCOUNT
.
– Jerry Dodge
Nov 19 '18 at 0:07
10
10
You're going about this completely the wrong way. You should be using a Select Max(<columnname.) SQL query agains your table, which is easy to do in Delphi.
– MartynA
Nov 13 '18 at 20:55
You're going about this completely the wrong way. You should be using a Select Max(<columnname.) SQL query agains your table, which is easy to do in Delphi.
– MartynA
Nov 13 '18 at 20:55
1
1
As @MartynA pointed out, let the database do the work. And read this: stackoverflow.com/questions/17308246/access-violation-in-delphi, look at Mason Wheeler's answer.
– Nic3500
Nov 13 '18 at 21:00
As @MartynA pointed out, let the database do the work. And read this: stackoverflow.com/questions/17308246/access-violation-in-delphi, look at Mason Wheeler's answer.
– Nic3500
Nov 13 '18 at 21:00
Why do this on the slowest way possible ? Just do
select max(NUMOFREPORTS) from tblUserInfo
and your done. Take @MartynA 's advice– GuidoG
Nov 14 '18 at 7:50
Why do this on the slowest way possible ? Just do
select max(NUMOFREPORTS) from tblUserInfo
and your done. Take @MartynA 's advice– GuidoG
Nov 14 '18 at 7:50
I have a feeling
select count(NUMOFREPORTS) from tblUserInfo
is more-so what you need. What if you had a gap where a historical record was removed? The MAX
has nothing to do with the COUNT
.– Jerry Dodge
Nov 19 '18 at 0:07
I have a feeling
select count(NUMOFREPORTS) from tblUserInfo
is more-so what you need. What if you had a gap where a historical record was removed? The MAX
has nothing to do with the COUNT
.– Jerry Dodge
Nov 19 '18 at 0:07
add a comment |
2 Answers
2
active
oldest
votes
As it was pointed out by MartynA in the comments you should let database to find you the maximum value rather than retrieving all the values first and then search for the maximum value by yourself. Why?
- In your case you claim that you only have 11 values in this field so retrieving that data from database wouldn't be difficult. But what if you would have one million values there. Retrieving 1M values from database can be quite taxing on the database and would also consume significant amount of memory on your client computer if you store these values in an array for further processing (finding maximum value).
- Database would surely be able to find the maximum value much faster than you since it can easily split this task between multiple cores. Not to mention the fact that if that specific field is indexed the database most likely already has the maximum value stored as one of the index values in which case database could return the maximum value instantly. And even if the maximum value isn't directly stored as one of the index values database would only have to check part of the records instead of checking each and every one.
And if for some special reason you really can't use database built-in Select Max
function (perhaps value isn't stored as numeric value but as special string and the maximum value is determined by some part in the middle of the string) and have to really find the max value by yourself don't bother storing all of the values into an array. Instead do the needed comparison for finding the max value directly while retrieving records from database. Perhaps something like this where I just modified your loop for retrieving records from the database:
while NOT tblUserInfo.eof do
begin
if tblUserInfo['NUMOFREPORTS'] > NumOfReps then
begin
NumOfReps:= tblUserInfo['NUMOFREPORTS'];
end;
tblUserInfo.Next;
end;
I've upvoted this because of your good explanation of the issues, but I think you should improve your answer by not using the ['FieldName'] construct in a loop, especially twice per iteration. Better to capture the field's indentity in a local TField variable before starting the loop and do the reads against that.
– MartynA
Nov 14 '18 at 22:20
@MartynA My intention with my answer was to make it easily understandable. But I totally agree with your proposed optimization of the code example.
– SilverWarior
Nov 16 '18 at 23:56
add a comment |
How did you declare your array? Based on the way you work with it, it should probably be declared like this:
var
ArrNumOfReps: array[1..12] of Integer;
But that's limited, since the program only supports a maximum of 11 values. Rather use a dynamic array
var
ArrNumOfReps: array of Integer;
i:=0;
while NOT tblUserInfo.eof do
begin
SetLength(ArrNumOfReps, Length(ArrNumOfReps) + 1);
arrNumOfReps[i]:= tblUserInfo.FieldByName('NUMOFREPORTS').AsInteger;
tblUserInfo.Next;
INC(i);
end;
This will allow the program to work with a variable number of reps.
Seriously? Calling SetLength each time around a loop which is going to be executed an indeterminate, possibly huge, number ot times? In any case not a good idea to encourage the OP to persist in the folly of using this approach
– MartynA
Nov 14 '18 at 22:25
@MartynA Agreed. I was answering OP's question specifically, not trying to give him a tutorial how to write better code.
– nolaspeaker
Nov 15 '18 at 5:53
@nolaspeaker The problem with that is that OP will not learn and will continue to return here and ask poor questions.
– Jerry Dodge
Nov 19 '18 at 0:10
@Jerry Dodge I'll give him a little more credit than that.
– nolaspeaker
Nov 20 '18 at 12:41
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289316%2ffinding-the-max-value-in-a-database-with-delphi%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As it was pointed out by MartynA in the comments you should let database to find you the maximum value rather than retrieving all the values first and then search for the maximum value by yourself. Why?
- In your case you claim that you only have 11 values in this field so retrieving that data from database wouldn't be difficult. But what if you would have one million values there. Retrieving 1M values from database can be quite taxing on the database and would also consume significant amount of memory on your client computer if you store these values in an array for further processing (finding maximum value).
- Database would surely be able to find the maximum value much faster than you since it can easily split this task between multiple cores. Not to mention the fact that if that specific field is indexed the database most likely already has the maximum value stored as one of the index values in which case database could return the maximum value instantly. And even if the maximum value isn't directly stored as one of the index values database would only have to check part of the records instead of checking each and every one.
And if for some special reason you really can't use database built-in Select Max
function (perhaps value isn't stored as numeric value but as special string and the maximum value is determined by some part in the middle of the string) and have to really find the max value by yourself don't bother storing all of the values into an array. Instead do the needed comparison for finding the max value directly while retrieving records from database. Perhaps something like this where I just modified your loop for retrieving records from the database:
while NOT tblUserInfo.eof do
begin
if tblUserInfo['NUMOFREPORTS'] > NumOfReps then
begin
NumOfReps:= tblUserInfo['NUMOFREPORTS'];
end;
tblUserInfo.Next;
end;
I've upvoted this because of your good explanation of the issues, but I think you should improve your answer by not using the ['FieldName'] construct in a loop, especially twice per iteration. Better to capture the field's indentity in a local TField variable before starting the loop and do the reads against that.
– MartynA
Nov 14 '18 at 22:20
@MartynA My intention with my answer was to make it easily understandable. But I totally agree with your proposed optimization of the code example.
– SilverWarior
Nov 16 '18 at 23:56
add a comment |
As it was pointed out by MartynA in the comments you should let database to find you the maximum value rather than retrieving all the values first and then search for the maximum value by yourself. Why?
- In your case you claim that you only have 11 values in this field so retrieving that data from database wouldn't be difficult. But what if you would have one million values there. Retrieving 1M values from database can be quite taxing on the database and would also consume significant amount of memory on your client computer if you store these values in an array for further processing (finding maximum value).
- Database would surely be able to find the maximum value much faster than you since it can easily split this task between multiple cores. Not to mention the fact that if that specific field is indexed the database most likely already has the maximum value stored as one of the index values in which case database could return the maximum value instantly. And even if the maximum value isn't directly stored as one of the index values database would only have to check part of the records instead of checking each and every one.
And if for some special reason you really can't use database built-in Select Max
function (perhaps value isn't stored as numeric value but as special string and the maximum value is determined by some part in the middle of the string) and have to really find the max value by yourself don't bother storing all of the values into an array. Instead do the needed comparison for finding the max value directly while retrieving records from database. Perhaps something like this where I just modified your loop for retrieving records from the database:
while NOT tblUserInfo.eof do
begin
if tblUserInfo['NUMOFREPORTS'] > NumOfReps then
begin
NumOfReps:= tblUserInfo['NUMOFREPORTS'];
end;
tblUserInfo.Next;
end;
I've upvoted this because of your good explanation of the issues, but I think you should improve your answer by not using the ['FieldName'] construct in a loop, especially twice per iteration. Better to capture the field's indentity in a local TField variable before starting the loop and do the reads against that.
– MartynA
Nov 14 '18 at 22:20
@MartynA My intention with my answer was to make it easily understandable. But I totally agree with your proposed optimization of the code example.
– SilverWarior
Nov 16 '18 at 23:56
add a comment |
As it was pointed out by MartynA in the comments you should let database to find you the maximum value rather than retrieving all the values first and then search for the maximum value by yourself. Why?
- In your case you claim that you only have 11 values in this field so retrieving that data from database wouldn't be difficult. But what if you would have one million values there. Retrieving 1M values from database can be quite taxing on the database and would also consume significant amount of memory on your client computer if you store these values in an array for further processing (finding maximum value).
- Database would surely be able to find the maximum value much faster than you since it can easily split this task between multiple cores. Not to mention the fact that if that specific field is indexed the database most likely already has the maximum value stored as one of the index values in which case database could return the maximum value instantly. And even if the maximum value isn't directly stored as one of the index values database would only have to check part of the records instead of checking each and every one.
And if for some special reason you really can't use database built-in Select Max
function (perhaps value isn't stored as numeric value but as special string and the maximum value is determined by some part in the middle of the string) and have to really find the max value by yourself don't bother storing all of the values into an array. Instead do the needed comparison for finding the max value directly while retrieving records from database. Perhaps something like this where I just modified your loop for retrieving records from the database:
while NOT tblUserInfo.eof do
begin
if tblUserInfo['NUMOFREPORTS'] > NumOfReps then
begin
NumOfReps:= tblUserInfo['NUMOFREPORTS'];
end;
tblUserInfo.Next;
end;
As it was pointed out by MartynA in the comments you should let database to find you the maximum value rather than retrieving all the values first and then search for the maximum value by yourself. Why?
- In your case you claim that you only have 11 values in this field so retrieving that data from database wouldn't be difficult. But what if you would have one million values there. Retrieving 1M values from database can be quite taxing on the database and would also consume significant amount of memory on your client computer if you store these values in an array for further processing (finding maximum value).
- Database would surely be able to find the maximum value much faster than you since it can easily split this task between multiple cores. Not to mention the fact that if that specific field is indexed the database most likely already has the maximum value stored as one of the index values in which case database could return the maximum value instantly. And even if the maximum value isn't directly stored as one of the index values database would only have to check part of the records instead of checking each and every one.
And if for some special reason you really can't use database built-in Select Max
function (perhaps value isn't stored as numeric value but as special string and the maximum value is determined by some part in the middle of the string) and have to really find the max value by yourself don't bother storing all of the values into an array. Instead do the needed comparison for finding the max value directly while retrieving records from database. Perhaps something like this where I just modified your loop for retrieving records from the database:
while NOT tblUserInfo.eof do
begin
if tblUserInfo['NUMOFREPORTS'] > NumOfReps then
begin
NumOfReps:= tblUserInfo['NUMOFREPORTS'];
end;
tblUserInfo.Next;
end;
edited Nov 17 '18 at 0:03
answered Nov 14 '18 at 21:44
SilverWariorSilverWarior
4,3152915
4,3152915
I've upvoted this because of your good explanation of the issues, but I think you should improve your answer by not using the ['FieldName'] construct in a loop, especially twice per iteration. Better to capture the field's indentity in a local TField variable before starting the loop and do the reads against that.
– MartynA
Nov 14 '18 at 22:20
@MartynA My intention with my answer was to make it easily understandable. But I totally agree with your proposed optimization of the code example.
– SilverWarior
Nov 16 '18 at 23:56
add a comment |
I've upvoted this because of your good explanation of the issues, but I think you should improve your answer by not using the ['FieldName'] construct in a loop, especially twice per iteration. Better to capture the field's indentity in a local TField variable before starting the loop and do the reads against that.
– MartynA
Nov 14 '18 at 22:20
@MartynA My intention with my answer was to make it easily understandable. But I totally agree with your proposed optimization of the code example.
– SilverWarior
Nov 16 '18 at 23:56
I've upvoted this because of your good explanation of the issues, but I think you should improve your answer by not using the ['FieldName'] construct in a loop, especially twice per iteration. Better to capture the field's indentity in a local TField variable before starting the loop and do the reads against that.
– MartynA
Nov 14 '18 at 22:20
I've upvoted this because of your good explanation of the issues, but I think you should improve your answer by not using the ['FieldName'] construct in a loop, especially twice per iteration. Better to capture the field's indentity in a local TField variable before starting the loop and do the reads against that.
– MartynA
Nov 14 '18 at 22:20
@MartynA My intention with my answer was to make it easily understandable. But I totally agree with your proposed optimization of the code example.
– SilverWarior
Nov 16 '18 at 23:56
@MartynA My intention with my answer was to make it easily understandable. But I totally agree with your proposed optimization of the code example.
– SilverWarior
Nov 16 '18 at 23:56
add a comment |
How did you declare your array? Based on the way you work with it, it should probably be declared like this:
var
ArrNumOfReps: array[1..12] of Integer;
But that's limited, since the program only supports a maximum of 11 values. Rather use a dynamic array
var
ArrNumOfReps: array of Integer;
i:=0;
while NOT tblUserInfo.eof do
begin
SetLength(ArrNumOfReps, Length(ArrNumOfReps) + 1);
arrNumOfReps[i]:= tblUserInfo.FieldByName('NUMOFREPORTS').AsInteger;
tblUserInfo.Next;
INC(i);
end;
This will allow the program to work with a variable number of reps.
Seriously? Calling SetLength each time around a loop which is going to be executed an indeterminate, possibly huge, number ot times? In any case not a good idea to encourage the OP to persist in the folly of using this approach
– MartynA
Nov 14 '18 at 22:25
@MartynA Agreed. I was answering OP's question specifically, not trying to give him a tutorial how to write better code.
– nolaspeaker
Nov 15 '18 at 5:53
@nolaspeaker The problem with that is that OP will not learn and will continue to return here and ask poor questions.
– Jerry Dodge
Nov 19 '18 at 0:10
@Jerry Dodge I'll give him a little more credit than that.
– nolaspeaker
Nov 20 '18 at 12:41
add a comment |
How did you declare your array? Based on the way you work with it, it should probably be declared like this:
var
ArrNumOfReps: array[1..12] of Integer;
But that's limited, since the program only supports a maximum of 11 values. Rather use a dynamic array
var
ArrNumOfReps: array of Integer;
i:=0;
while NOT tblUserInfo.eof do
begin
SetLength(ArrNumOfReps, Length(ArrNumOfReps) + 1);
arrNumOfReps[i]:= tblUserInfo.FieldByName('NUMOFREPORTS').AsInteger;
tblUserInfo.Next;
INC(i);
end;
This will allow the program to work with a variable number of reps.
Seriously? Calling SetLength each time around a loop which is going to be executed an indeterminate, possibly huge, number ot times? In any case not a good idea to encourage the OP to persist in the folly of using this approach
– MartynA
Nov 14 '18 at 22:25
@MartynA Agreed. I was answering OP's question specifically, not trying to give him a tutorial how to write better code.
– nolaspeaker
Nov 15 '18 at 5:53
@nolaspeaker The problem with that is that OP will not learn and will continue to return here and ask poor questions.
– Jerry Dodge
Nov 19 '18 at 0:10
@Jerry Dodge I'll give him a little more credit than that.
– nolaspeaker
Nov 20 '18 at 12:41
add a comment |
How did you declare your array? Based on the way you work with it, it should probably be declared like this:
var
ArrNumOfReps: array[1..12] of Integer;
But that's limited, since the program only supports a maximum of 11 values. Rather use a dynamic array
var
ArrNumOfReps: array of Integer;
i:=0;
while NOT tblUserInfo.eof do
begin
SetLength(ArrNumOfReps, Length(ArrNumOfReps) + 1);
arrNumOfReps[i]:= tblUserInfo.FieldByName('NUMOFREPORTS').AsInteger;
tblUserInfo.Next;
INC(i);
end;
This will allow the program to work with a variable number of reps.
How did you declare your array? Based on the way you work with it, it should probably be declared like this:
var
ArrNumOfReps: array[1..12] of Integer;
But that's limited, since the program only supports a maximum of 11 values. Rather use a dynamic array
var
ArrNumOfReps: array of Integer;
i:=0;
while NOT tblUserInfo.eof do
begin
SetLength(ArrNumOfReps, Length(ArrNumOfReps) + 1);
arrNumOfReps[i]:= tblUserInfo.FieldByName('NUMOFREPORTS').AsInteger;
tblUserInfo.Next;
INC(i);
end;
This will allow the program to work with a variable number of reps.
answered Nov 14 '18 at 17:29
nolaspeakernolaspeaker
9751127
9751127
Seriously? Calling SetLength each time around a loop which is going to be executed an indeterminate, possibly huge, number ot times? In any case not a good idea to encourage the OP to persist in the folly of using this approach
– MartynA
Nov 14 '18 at 22:25
@MartynA Agreed. I was answering OP's question specifically, not trying to give him a tutorial how to write better code.
– nolaspeaker
Nov 15 '18 at 5:53
@nolaspeaker The problem with that is that OP will not learn and will continue to return here and ask poor questions.
– Jerry Dodge
Nov 19 '18 at 0:10
@Jerry Dodge I'll give him a little more credit than that.
– nolaspeaker
Nov 20 '18 at 12:41
add a comment |
Seriously? Calling SetLength each time around a loop which is going to be executed an indeterminate, possibly huge, number ot times? In any case not a good idea to encourage the OP to persist in the folly of using this approach
– MartynA
Nov 14 '18 at 22:25
@MartynA Agreed. I was answering OP's question specifically, not trying to give him a tutorial how to write better code.
– nolaspeaker
Nov 15 '18 at 5:53
@nolaspeaker The problem with that is that OP will not learn and will continue to return here and ask poor questions.
– Jerry Dodge
Nov 19 '18 at 0:10
@Jerry Dodge I'll give him a little more credit than that.
– nolaspeaker
Nov 20 '18 at 12:41
Seriously? Calling SetLength each time around a loop which is going to be executed an indeterminate, possibly huge, number ot times? In any case not a good idea to encourage the OP to persist in the folly of using this approach
– MartynA
Nov 14 '18 at 22:25
Seriously? Calling SetLength each time around a loop which is going to be executed an indeterminate, possibly huge, number ot times? In any case not a good idea to encourage the OP to persist in the folly of using this approach
– MartynA
Nov 14 '18 at 22:25
@MartynA Agreed. I was answering OP's question specifically, not trying to give him a tutorial how to write better code.
– nolaspeaker
Nov 15 '18 at 5:53
@MartynA Agreed. I was answering OP's question specifically, not trying to give him a tutorial how to write better code.
– nolaspeaker
Nov 15 '18 at 5:53
@nolaspeaker The problem with that is that OP will not learn and will continue to return here and ask poor questions.
– Jerry Dodge
Nov 19 '18 at 0:10
@nolaspeaker The problem with that is that OP will not learn and will continue to return here and ask poor questions.
– Jerry Dodge
Nov 19 '18 at 0:10
@Jerry Dodge I'll give him a little more credit than that.
– nolaspeaker
Nov 20 '18 at 12:41
@Jerry Dodge I'll give him a little more credit than that.
– nolaspeaker
Nov 20 '18 at 12:41
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289316%2ffinding-the-max-value-in-a-database-with-delphi%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
10
You're going about this completely the wrong way. You should be using a Select Max(<columnname.) SQL query agains your table, which is easy to do in Delphi.
– MartynA
Nov 13 '18 at 20:55
1
As @MartynA pointed out, let the database do the work. And read this: stackoverflow.com/questions/17308246/access-violation-in-delphi, look at Mason Wheeler's answer.
– Nic3500
Nov 13 '18 at 21:00
Why do this on the slowest way possible ? Just do
select max(NUMOFREPORTS) from tblUserInfo
and your done. Take @MartynA 's advice– GuidoG
Nov 14 '18 at 7:50
I have a feeling
select count(NUMOFREPORTS) from tblUserInfo
is more-so what you need. What if you had a gap where a historical record was removed? TheMAX
has nothing to do with theCOUNT
.– Jerry Dodge
Nov 19 '18 at 0:07