全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1957
推到 Plurk!
推到 Facebook!

資料丟出來有多筆但預覽總是只秀第一筆所丟出來的資料

答題得分者是:eaglewolf
AND10518
一般會員


發表:1
回覆:3
積分:0
註冊:2010-04-07

發送簡訊給我
#1 引用回覆 回覆 發表時間:2010-04-07 15:04:23 IP:123.204.xxx.xxx 訂閱

[code delphi]
var
SaleT : TPrintInvoiceForm;
sqlstr,startdate2,enddate2,mId,invoiceno : string;

Recordcnt,InvCount,cnt,idx,idx2,other : integer;
ChkCode : integer;
strYear,strMonth,strDay : String;
strInvNo : string;
SaleSum,iFreight,invQty : integer;
invodate,strSaleSum,strSQL,outstr,dd : string;
tempcount,i,tax : integer;
begin
SaleT := TPrintInvoiceForm.Create(Application);
With SaleT do begin
idx:=Query1['Invoicecount'];
for cnt:=1 to idx do begin
if cnt = 1 then memoInvNote.Text := Query1['InvNote']
else memoInvNote.Text := '';
sqlstr:='select a.*,b.mId,b.Purchaser,b.CompanyId,b.CompAddr,b.Carrier,b.Freight,b.SaleSumTax,b.SalePV,b.Cash,b.Remittance '
',b.CreditCard,b.CheckBond,b.Total,b.Discount,b.PdtPV,b.PaperbagPV,b.InvNote,b.Invoicecount '
',year(a.InvoiceDate)-1911 as myear '
',month(a.InvoiceDate) as mmonth,day(a.InvoiceDate) as mday from InvoiceDtl a,saleMst b where a.InvoiceDate '
'between ''' startdate2 ''' and ''' enddate2 ''' and a.saleId=b.saleId and a.status=0 ' mId '' invoiceno ' '
'order by InvoiceDate,InvoiceNo,InvoiceIndex';
Query1.SQL.Add(sqlstr);
Query1.Active := True;
Query1.first;
while not Query1.eof do begin
strSQl:='select c.IndexNo,c.PdtNo,e.PdtName,c.Qty,c.PdtSalePrice,c.PdtPV,c.Qty*c.PdtSalePrice as TotalPrice '
',c.SalePV*c.Qty as TotalSalePV,c.PdtCost,c.SalePV from saleDtl c,Product e '
'where c.PdtNo=e.PdtNo and c.SaleId = :id '
'and c.IndexNo >= :start and c.IndexNo <= :end order by c.IndexNo ';
end;
Query2.SQL.add(strSQl);

Query2.ParamByName('start').AsInteger := (cnt-1)*13 1;
Query2.ParamByName('end').AsInteger := (cnt)*13;
Query2.ParamByName('id').AsString := Query1['SaleId'];

Query2.Active := True;
mem.Text:=query1.fieldbyname('mid').AsString;
VisualReport1.Print;

//VisualReport1.Preview;
end;
Query1.next;
end;
end;

end;
[/code]


請問大大因為query1抓出來不只一筆而上面是用while 讓他一筆一筆跑但我用VisualReport1.Print; 確總是只有出現第一頁所抓出來的資料
而用 VisualReport1.Preview; 他就直接跳到最後一筆資料所丟出來的資料,不會一筆一筆的show出來,正常不都是一先show第一筆抓出來的資料按關閉
才會在跳下一筆資料所丟出來的資料 ,請問大大還是有其他的指令可以show出來 還是我那邊錯了資料丟出來都沒錯啊 麻煩各位大大了謝謝

eaglewolf
資深會員


發表:4
回覆:268
積分:429
註冊:2006-07-06

發送簡訊給我
#2 引用回覆 回覆 發表時間:2010-04-07 15:47:58 IP:211.75.xxx.xxx 未訂閱
你的程式 執行到 底下這段
[code delphi]
while not Query1.eof do
begin
strSQl:='select c.IndexNo,c.PdtNo,e.PdtName,c.Qty,c.PdtSalePrice,c.PdtPV,c.Qty*c.PdtSalePrice as TotalPrice '
',c.SalePV*c.Qty as TotalSalePV,c.PdtCost,c.SalePV from saleDtl c,Product e '
'where c.PdtNo=e.PdtNo and c.SaleId = :id '
'and c.IndexNo >= :start and c.IndexNo <= :end order by c.IndexNo ';
end;
[/code]
不會限入無限迴圈嗎????
------
先查HELP
再查GOOGLE
最後才發問

沒人有義務替你解答問題
在標題或文章中標明很急
並不會增加網友回答速度

Developing Tool:
1.Delphi 6
2.Visual Studio 2005
3.Visual Studio 2008
DBMS:
MS-SQL
AND10518
一般會員


發表:1
回覆:3
積分:0
註冊:2010-04-07

發送簡訊給我
#3 引用回覆 回覆 發表時間:2010-04-07 16:38:17 IP:123.204.xxx.xxx 訂閱
大大不會勒他完資料庫所抓出來的第一筆,在query1 next,一筆一筆跑跑到最後一筆資料為止
P.D.
版主


發表:603
回覆:4038
積分:3874
註冊:2006-10-31

發送簡訊給我
#4 引用回覆 回覆 發表時間:2010-04-07 21:43:29 IP:118.169.xxx.xxx 未訂閱
這看起來是一個MASTER-DETAIL的輸出方式, 但我不知道你是從那裡學到這樣的寫法
1.在 FOR NEXT 或 WHILE 中不斷的異動 SQL , 改變其值, 最終的結果會造成 NEST DEAD LOCK 的問題,
也會引發 MEMORY LEAK 的問題
2.沒有看到你 VR 的設計內容是如何做的
3.一般在設計MASTER-DETAIL 的報表時, 一定是 QUERY1 去帶動 QUERY2 的連結, 大多數都會寫在 QUERY1 的 AFTERSCORLL 中, 只要 QUERY1 往下一筆, QUYER2 自動變異, 如此做法, 我只要下一次 SQL 給 SQL ENGINE 解譯語法即可, 而在 VR 也有提供 MASTER, DETAIL 的 BAND 模組, 直接引用, 自然就可以產生出來
===================引 用 AND10518 文 章===================

[code delphi]
var
SaleT : TPrintInvoiceForm;
sqlstr,startdate2,enddate2,mId,invoiceno : string;
?
Recordcnt,InvCount,cnt,idx,idx2,other : integer;
ChkCode : integer;
strYear,strMonth,strDay : String;
strInvNo : string;
SaleSum,iFreight,invQty : integer;
invodate,strSaleSum,strSQL,outstr,dd : string;
tempcount,i,tax : integer;
begin
SaleT := TPrintInvoiceForm.Create(Application);
With SaleT do begin
?idx:=Query1['Invoicecount'];
?? for cnt:=1 to idx do begin
if cnt = 1 then memoInvNote.Text := Query1['InvNote']
else memoInvNote.Text := '';
sqlstr:='select a.*,b.mId,b.Purchaser,b.CompanyId,b.CompAddr,b.Carrier,b.Freight,b.SaleSumTax,b.SalePV,b.Cash,b.Remittance '
? ',b.CreditCard,b.CheckBond,b.Total,b.Discount,b.PdtPV,b.PaperbagPV,b.InvNote,b.Invoicecount '
? ',year(a.InvoiceDate)-1911 as myear '
? ',month(a.InvoiceDate) as mmonth,day(a.InvoiceDate) as mday? from InvoiceDtl a,saleMst b where a.InvoiceDate '
? 'between ''' startdate2 ''' and ''' enddate2 ''' and a.saleId=b.saleId? and a.status=0 ' mId '' invoiceno ' '
? 'order by InvoiceDate,InvoiceNo,InvoiceIndex';
? Query1.SQL.Add(sqlstr);
? Query1.Active := True;
? Query1.first;
? while not Query1.eof do begin
?? strSQl:='select c.IndexNo,c.PdtNo,e.PdtName,c.Qty,c.PdtSalePrice,c.PdtPV,c.Qty*c.PdtSalePrice as TotalPrice '
??? ',c.SalePV*c.Qty? as TotalSalePV,c.PdtCost,c.SalePV from saleDtl c,Product e '
??? 'where c.PdtNo=e.PdtNo and c.SaleId = :id '
??? 'and c.IndexNo >= :start and c.IndexNo <= :end? order by c.IndexNo ';
end;
?Query2.SQL.add(strSQl);

Query2.ParamByName('start').AsInteger := (cnt-1)*13 1;
Query2.ParamByName('end').AsInteger := (cnt)*13;
Query2.ParamByName('id').AsString := Query1['SaleId'];

Query2.Active := True;
mem.Text:=query1.fieldbyname('mid').AsString;
VisualReport1.Print;

//VisualReport1.Preview;
?? end;
?? Query1.next;
?? end;
end;

end;
[/code]
??

請問大大因為query1抓出來不只一筆而上面是用while 讓他一筆一筆跑但我用VisualReport1.Print;? 確總是只有出現第一頁所抓出來的資料
而用??VisualReport1.Preview; 他就直接跳到最後一筆資料所丟出來的資料,不會一筆一筆的show出來,正常不都是一先show第一筆抓出來的資料按關閉
才會在跳下一筆資料所丟出來的資料 ,請問大大還是有其他的指令可以show出來?還是我那邊錯了資料丟出來都沒錯啊??? 麻煩各位大大了謝謝
?
eaglewolf
資深會員


發表:4
回覆:268
積分:429
註冊:2006-07-06

發送簡訊給我
#5 引用回覆 回覆 發表時間:2010-04-07 23:36:30 IP:122.116.xxx.xxx 未訂閱
除非你程式碼有貼錯或是query1沒資料
否則 你的while loop 一定會是無窮迴圈
因為你的query1.next 根本不在while loop內
永遠會停在query1的第一筆

------
先查HELP
再查GOOGLE
最後才發問

沒人有義務替你解答問題
在標題或文章中標明很急
並不會增加網友回答速度

Developing Tool:
1.Delphi 6
2.Visual Studio 2005
3.Visual Studio 2008
DBMS:
MS-SQL
AND10518
一般會員


發表:1
回覆:3
積分:0
註冊:2010-04-07

發送簡訊給我
#6 引用回覆 回覆 發表時間:2010-04-12 14:30:08 IP:123.204.xxx.xxx 訂閱

===================引 用 eaglewolf 文 章===================
除非你程式碼有貼錯或是query1沒資料
否則 你的while loop 一定會是無窮迴圈
因為你的query1.next 根本不在while loop內
永遠會停在query1的第一筆


[code delphi]
procedure TEtyPrintInvoiceForm.BtnQryClick(Sender: TObject);
var
SaleT : TPrintInvoiceForm;
sqlstr,startdate2,enddate2,mId,invoiceno : string;

Recordcnt,InvCount,cnt,idx,idx2,other : integer;
ChkCode : integer;
strYear,strMonth,strDay : String;
strInvNo : string;
SaleSum,iFreight,invQty : integer;
invodate,strSaleSum,strSQL,outstr,dd : string;
tempcount,i,tax : integer;
begin
SaleT := TPrintInvoiceForm.Create(Application);
if startdate.enabled then begin
startdate2:=datetostr(startdate.date);
enddate2:=datetostr(enddate.date);
end;
if rgdate.ItemIndex = 0 then begin
Query3.sql.Clear;
Query3.SQL.Add('select convert(char(12),getdate(),111) as dd');
Query3.Active:=true;

startdate2:=datetostr(1899/12/30);
enddate2 :=Query3.fieldByName('dd').AsString;
end;
if medInvoiceNo.enabled then invoiceno:=' and a.invoiceno=''' medInvoiceNo.text ''' '
else invoiceno:='';
if medmId.enabled then mId:=' and b.mId=''' medmId.text ''' '
else mId:='';
With SaleT do begin
Query1.SQL.clear;
sqlstr:='select a.*,b.mId,b.Purchaser,b.CompanyId,b.CompAddr,b.Carrier,b.Freight,b.SaleSumTax,b.SalePV,b.Cash,b.Remittance '
',b.CreditCard,b.CheckBond,b.Total,b.Discount,b.PdtPV,b.PaperbagPV,b.InvNote,b.Invoicecount '
',year(a.InvoiceDate)-1911 as myear '
',month(a.InvoiceDate) as mmonth,day(a.InvoiceDate) as mday from InvoiceDtl a,saleMst b where a.InvoiceDate '
'between ''' startdate2 ''' and ''' enddate2 ''' and a.saleId=b.saleId and a.status=0 ' mId '' invoiceno ' '
'union '
'select a.*,b.mId,b.Purchaser,b.CompanyId,b.CompAddr,b.Carrier,b.Freight,'''' as SaleSumTax,b.SalePV,b.Cash,b.Remittance '
',b.CreditCard,b.CheckBond,b.Total,b.Discount,'''' as PdtPV,'''' as PaperbagPV,b.InvNote '
',(select count(c.InvoiceNo) from InvoiceDtl c where c.saleId=a.saleId and c.status=0 ) as Invoicecount '
',year(a.InvoiceDate)-1911 as myear '
',month(a.InvoiceDate) as mmonth,day(a.InvoiceDate) as mday from InvoiceDtl a,changeMst b where a.InvoiceDate '
'between ''' startdate2 ''' and ''' enddate2 ''' and a.saleId=b.ChangeId and a.status=0 ' mId '' invoiceno ' '
'union '
'select a.*,b.mId,b.Purchaser,b.CompanyId,b.CompAddr '
','''' as Carrier,'''' as Freight,'''' as SaleSumTax,'''' as SalePV,'''' as Cash,'''' as Remittance,'''' as CreditCard '
','''' as CheckBond,'''' as Total,'''' as Discount,'''' as PdtPV,'''' as PaperbagPV,b.InvNote '
',(select count(c.InvoiceNo) from InvoiceDtl c where c.saleId=a.saleId and c.status=0 ) as Invoicecount '
',year(a.InvoiceDate)-1911 as myear '
',month(a.InvoiceDate) as mmonth,day(a.InvoiceDate) as mday from InvoiceDtl a,SaleReturnMst b where a.InvoiceDate'
' between ''' startdate2 ''' and ''' enddate2 ''' and a.saleId=b.ReturnId and a.status=0 ' mId '' invoiceno ' '
'union '
'select a.*,b.mId,b.Purchaser,b.CompanyId,b.CompAddr,b.Carrier,b.Freight,b.SaleSumTax,'''' as SalePV,b.Cash,b.Remittance '
',b.CreditCard,b.CheckBond,b.Total,'''' as Discount,'''' as PdtPV,'''' as PaperbagPV,b.InvNote '
',(select count(c.InvoiceNo) from InvoiceDtl c where c.saleId=a.saleId and c.status=0 ) as Invoicecount '
',year(a.InvoiceDate)-1911 as myear '
',month(a.InvoiceDate) as mmonth,day(a.InvoiceDate) as mday from InvoiceDtl a,SaleDataBag b where a.InvoiceDate '
' between ''' startdate2 ''' and ''' enddate2 ''' and a.saleId=b.saleId and a.status=0 ' mId '' invoiceno ' '
'order by InvoiceDate,InvoiceNo,InvoiceIndex';
Query1.SQL.Add(sqlstr);
Query1.Active := True;
while not Query1.eof do begin
InvCount:=Query1['Invoicecount'];
memoInvNote.BringToFront;
idx:=Query1['Invoicecount'];
Query2.sql.clear;
if copy(Query1['SaleId'],1,1)='C' then begin
strSQl:='select c.IndexNo,c.PdtNo,e.PdtName,c.Qty,c.PdtSalePrice,c.PdtPV,c.Qty*c.PdtSalePrice as TotalPrice '
',c.SalePV*c.Qty as TotalSalePV,c.PdtCost,c.SalePV from ChangeDtl c,Product e '
'where c.PdtNo=e.PdtNo and c.ChangeId = :id '
'and c.IndexNo >= :start and c.IndexNo <= :end order by c.IndexNo ';
end else if copy(Query1['SaleId'],1,1)='R' then begin
strSQl:='select c.IndexNo,c.PdtNo,e.PdtName,c.Qty,c.PdtSalePrice,c.PdtPV,c.Qty*c.PdtSalePrice as TotalPrice '
',c.SalePV*c.Qty as TotalSalePV,c.PdtCost,c.SalePV from SaleReturnDtl c,Product e '
'where c.PdtNo=e.PdtNo and c.ReturnId = :id and c.IndexNo >= :start and c.IndexNo <= :end order by c.IndexNo';
end else if copy(Query1['SaleId'],1,1)='D' then begin
strSQl:='select 1 as IndexNo,c.PdtNo,e.PdtName,1 as Qty,e.PdtSalePrice,e.PdtPV,e.PdtSalePrice as TotalPrice '
',e.SalePV as TotalSalePV,e.PdtCost,e.SalePV from SaleDataBag c,Product e '
'where c.PdtNo=e.PdtNo and c.SaleId = :id order by IndexNo';
end else begin
strSQl:='select c.IndexNo,c.PdtNo,e.PdtName,c.Qty,c.PdtSalePrice,c.PdtPV,c.Qty*c.PdtSalePrice as TotalPrice '
',c.SalePV*c.Qty as TotalSalePV,c.PdtCost,c.SalePV from saleDtl c,Product e '
'where c.PdtNo=e.PdtNo and c.SaleId = :id '
'and c.IndexNo >= :start and c.IndexNo <= :end order by c.IndexNo ';
end;
Query2.SQL.add(strSQl);
for cnt :=1 to idx do begin
if cnt = 1 then memoInvNote.Text := Query1['InvNote']
else memoInvNote.Text := '';
pTotal.Text := IntToStr(idx);
pIndex.Text := IntToStr(cnt);
RptLine1.PenStyle := psClear;

if InvCount >= cnt then
begin
strYear := Query1.fieldbyname('myear').AsString;
strMonth := Query1.fieldbyname('mmonth').AsString;
strDay := Query1.fieldbyname('mday').AsString;
txtYear.Text := strYear;
txtMonth.Text := strMonth;
txtDay.Text := strDay;
// 計算發票號碼 checkcode
txtInvoiceNo.Text:= Query1['InvoiceNo'];
txtChkCode.Text := GetInvoiceCheckCode(txtInvoiceNo.Text);
end;
if cnt = idx then
begin
if Query1['Freight'] > 0 then
begin
iFreight := Query1['Freight'];
strSQL:=strSQl ' union (select ''' 'X01 ' ''',1,' IntToStr(iFreight)
',' IntToStr(iFreight) ','''
'代收款 - 運費 ' ''',max(IndexNo) 1,'
'0,0 from SaleDtl where SaleId = :id)';
Query2.SQL.add(strSQl);
showmessage(Query2.sql.text);
end
if Query1['Discount'] = 0 then
begin
txtDiscountText.Text := '';
txtDiscount.Text := '';
end else begin
txtDiscountText.Text := '折讓';
txtDiscount.Text := IntToStr(-Query1['Discount']);
end;
SaleSum := Query1['SaleSum'];

if (Query1.FieldByName('CompanyId').AsString = '' ) then
begin // 個人
txtSaleSum.Text := '(含稅) ' IntToStr(SaleSum);
txtSaleSum2.Text := IntToStr(SaleSum);
txtTax.Text := '';
end
// 公司
else begin
tax := CalcTax(SaleSum);
txtSaleSum.Text := IntToStr(SaleSum-tax);
txtTax.Text := IntToStr(tax);
txtSaleSum2.Text := IntToStr(SaleSum);
end;
strSaleSum := GetCSum(SaleSum);
M0.Text := copy(strSaleSum,1,2);
M1.Text := copy(strSaleSum,3,2);
M2.Text := copy(strSaleSum,5,2);
M3.Text := copy(strSaleSum,7,2);
M4.Text := copy(strSaleSum,9,2);
M5.Text := copy(strSaleSum,11,2);
M6.Text := copy(strSaleSum,13,2);
M7.Text := copy(strSaleSum,15,2);
memoInvNote.Text := Query1.FieldByName('InvNote').AsString;
end
else begin
// 折讓,金額合計及總積分 = 空白
txtDiscountText.Text := '';
txtDiscount.Text := '';
txtSaleSum.Text := '';
txtSaleSum2.Text := '';
M0.Text := '';
M1.Text := '';
M2.Text := '';
M3.Text := '';
M4.Text := '';
M5.Text := '';
M6.Text := '';
M7.Text := '';
end;

Query2.ParamByName('start').AsInteger := (cnt-1)*13 1;
Query2.ParamByName('end').AsInteger := (cnt)*13;
Query2.ParamByName('id').AsString := Query1['SaleId'];

Query2.Active := True;
txtPurchaser.Text :=Query1.FieldByName('Purchaser').AsString;
txtCompanyId.Text :=Query1.FieldByName('CompanyId').AsString;
lblsaleId.Text := Query1.FieldByName('SaleId').AsString;
if medmId.Text='00-20-004488' then txtCompAddr.Text :=''
else txtCompAddr.Text :=Query1.FieldByName('CompAddr').AsString;

Query2.ParamByName('start').AsInteger := (cnt-1)*13 1;
Query2.ParamByName('end').AsInteger := (cnt)*13;
Query2.ParamByName('id').AsString := Query1['SaleId'];

Query2.Active := True;
mem.Text:=query1.fieldbyname('mid').AsString;
VisualReport1.Print;

end;
Query1.next;
end;
end;

end;

[/code]

大大抱歉那是我貼錯了因為太長了所以就有用大概但這是完整的,這是我拿別人的來修改
我後來研究應該是 for cnt := 1 to idx do 這個回圈就是用來換頁的,但他是寫在 SaleT := TPrintInvoiceForm.Create(Application);這裡面
而我看別人都是先 For cnt := 1 to idx do 在 建立一個報表 SaleT := TPrintInvoiceForm.Create(Application);
在把需要的資料塞上去 ,所以我用上面程式碼方式去跑,
每次都只會show出第一筆資料印出來也都只是同一筆,
請問有什麼辦法可以修改還是建議...在麻煩各位大大

編輯記錄
AND10518 重新編輯於 2010-04-12 14:36:20, 註解 無‧
eaglewolf
資深會員


發表:4
回覆:268
積分:429
註冊:2006-07-06

發送簡訊給我
#7 引用回覆 回覆 發表時間:2010-04-12 17:10:38 IP:211.75.xxx.xxx 未訂閱
簡化你的程式
[code delphi]
With SaleT do begin
while not Query1.eof do begin
//SQL Command Part One
for cnt :=1 to idx do begin
//SQL Command Part Two if in need
VisualReport1.Print;
end;//end of for cnt :=1 to idx
Query1.next;
end;//end of while not Query1.eof
end;
[/code]
你的VisualReport1.Print;是 在for-loop裡面
應該是要在for-loop外才對吧?
for-loop跑完時你的SQL command才算組完
應該這時候才列印報表

------
先查HELP
再查GOOGLE
最後才發問

沒人有義務替你解答問題
在標題或文章中標明很急
並不會增加網友回答速度

Developing Tool:
1.Delphi 6
2.Visual Studio 2005
3.Visual Studio 2008
DBMS:
MS-SQL
編輯記錄
eaglewolf 重新編輯於 2010-04-12 17:11:45, 註解 無‧
eaglewolf 重新編輯於 2010-04-12 17:12:30, 註解 無‧
AND10518
一般會員


發表:1
回覆:3
積分:0
註冊:2010-04-07

發送簡訊給我
#8 引用回覆 回覆 發表時間:2010-04-21 12:50:10 IP:123.204.xxx.xxx 訂閱

===================引 用 eaglewolf 文 章===================
簡化你的程式
[code delphi]
With SaleT do begin
while not Query1.eof do begin
//SQL Command Part One
for cnt :=1 to idx do begin
//SQL Command Part Two if in need
VisualReport1.Print;
end;//end of for cnt :=1 to idx
Query1.next;
end;//end of while not Query1.eof
end;
[/code]
你的VisualReport1.Print;是 在for-loop裡面
應該是要在for-loop外才對吧?
for-loop跑完時你的SQL command才算組完
應該這時候才列印報表

While not Query1.eof do begin
idx:=Query1['Invoicecount'];
For cnt :=1 to idx do begin
SaleT := TPrintInvoiceForm.Create(Application);
With SaleT do begin


大大謝謝後面試了真的是因為我沒吧SaleT := TPrintInvoiceForm.Create(Application); 包在for-loop裡面後來這樣包起來最後在free就ok了感謝大大


系統時間:2024-05-04 2:12:50
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!