2009-07-16 120 views
0

使用VB 6和Access數據庫如何訂購表格?

我的查詢。

創建一個表

sql4 = "CREATE TABLE tmp_MOI (RECORDNO varchar(20), PERSONID varchar(20), EmpName varchar(50), TitleName varchar(50), Titlecode varchar(50), Department varchar(50), Nation varchar(50), CardEventDate varchar(50), Intime varchar(20), Outtime varchar(20), PrevDate varchar(50), PrevOut varchar(20))" 
If rstmpCardEvent1.State = 1 Then rstmpCardEvent1.Close 
rstmpCardEvent1.Open sql4, Cn, adOpenStatic, adLockOptimistic 

從表中選擇一個字段,並保存到記錄

sql9 = "SELECT RECORDNO, PERSONID, EMPNAME, TITLENAME, TITLECODE, DEPARTMENT, NATION, CARDEVENTDATE, INTIME, OUTTIME, (select TOP 1 F1.CARDEVENTDATE from tmp_cardevent as F1 where F1.RECORDNO < F2.RECORDNO AND F1.PERSONID = F2.PERSONID order by F1.RECORDNO DESC, F1.PERSONID DESC) AS PrevDate, (select TOP 1 F1.OUTTIME from tmp_cardevent as F1 where F1.RECORDNO < F2.RECORDNO AND F1.PERSONID = F2.PERSONID order by F1.RECORDNO DESC, F1.PERSONID DESC) AS PrevOut FROM tmp_cardevent AS F2 ORDER BY F2.NATION, F2.TITLECODE, F2.PERSONID, F2.CARDEVENTDATE" 
If rsCardEvent1.State = 1 Then rsCardEvent1.Close 
rsCardEvent1.Open sql9, Cn, adOpenStatic, adLockOptimistic 
cmdcardevent1.CommandText = sql9 
Set rsCardEvent1 = cmdcardevent1.Execute 

插入域到表

While Not rsCardEvent1.EOF 

sql10 = "INSERT INTO tmp_MOI values('" & rsCardEvent1(0).Value & "' , '" & rsCardEvent1(1).Value & "', '" & rsCardEvent1(2).Value & "', '" & rsCardEvent1(3).Value & "', '" & rsCardEvent1(4).Value & "', '" & rsCardEvent1(5).Value & "', '" & rsCardEvent1(6).Value & "', '" & StringToDate(rsCardEvent1.Fields(7).Value) & "', '" & rsCardEvent1.Fields(8).Value & "', '" & rsCardEvent1.Fields(9).Value & "', '" & StringToDate(rsCardEvent1.Fields(10).Value) & "', '" & StringToTim(rsCardEvent1(11).Value) & "') ORDER BY '" & rsCardEvent1(4).Value & "'" 
If rstmpCardEvent1.State = 1 Then rstmpCardEvent1.Close 
rstmpCardEvent1.Open sql10, Cn, adOpenStatic, adLockOptimistic 
rsCardEvent1.MoveNext 

Wend 

當我檢查這個查詢在訪問數據庫中 - 它顯示正確,按國家排序,類似於001,002,003秒o在...,PERSONID,cardeventdate

國家,PERSONID,cardeventdate正確地顯示在tmp_MOI表

,但我不會在tmp_MOI表正確獲得冠軍的代碼,它顯示像001在tmp_MOI表titlecode, 002,001,003,002等...

我的代碼有什麼問題,是我的代碼中的任何問題?

需要查詢或代碼幫助

回答

0

首先 - 我不知道爲什麼你會

ORDER BY '" & rsCardEvent1(4).Value & "'" 
在INSERT語句

,當你在一個記錄環插入並在這樣做插入一個一行一次。

第二 - 你有正確的行數。難道你的連接是不正確的,所以你會得到每個titlecode多行?

第三 - 如果你有正確的行數,請記住RDBMS的設計應使表中的記錄順序應該是獨立的。因此,如果您沒有訂單條款選擇,則沒有保證數據訂單。當您需要查看數據時添加Order By子句。

+0

如何使用ORDER BY'「&rsCardEvent1(4).Value&」'「,當我插入值的順序時,它顯示錯誤 – Gopal 2009-07-16 04:58:37