Edit a Table by VBA
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 280   1
URL.หัวข้อ / URL
Edit a Table by VBA

มีคนถามไว้ที่ 
http://www.access-programmers.co.uk/forums/showthread.php?s=&postid=107689#post107689 
 
ดังนี้ครับ 
I have made a linked table to a text file (which is continuously updated).  
Unfortunately the file is formatted as report. This means that the first values are  
omitted if they are double. For a proper processing I must put those  
values "back". So far I know there is no solution by SQL since if the first value of  
a record Is Null, its value depends on the first (not empty) value of a record  
above (in the same table).  
 
I would like to let VBA loop trough the table. When it encounters a Null-value it  
should add the value of the above record.  
It looks like this:  
1  Record1
Record2 
2  Record3
3  Record4  
Record5  
Record6
4  Record7  
etc.  
It should look like this:  
1 Record1
1 Record2
2 Record3
3 Record4
3 Record5
3 Record6
4 Record7
etc.  

Is there a SOLUTION?  
 
Many thanks! 
 
สรุป เขามีตารางเก็บข้อมูลอย่างนี้ 
 
คือเขามีข้อมูลอย่างนี้

Field1 Field2
1 xxxxxxx
yyyyyy
2 cccccc
3 bbbbb
ddddd
eeeee
4 fffffffffff
etc.

ต้องการให้เป็นแบนนี้

Field1 Field2
1 xxxxxxx
1 yyyyyy
2 cccccc
3 bbbbb
3 ddddd
3 eeeee
4 fffffffffff
 
ผมเคยตอบคำถามลักษณะนี้ครั้งหนึ่งนานมาแล้ว โดยการใช้โค้ดวนไปตั้งแต่ข้อมูลแรกจนถึงข้อมูลสุดท้าย โดยทุกครั้งจะเก็บค่าในฟีลด์ 1 ไว้ตลอด ถ้าเจอว่าฟีลด์นี้ว่างในข้อมูลใด ก็นำค่าที่จำไว้จากข้อมูลก่อนหน้านั้นมาเติมไปแทน ซึ่งข้อมูลที่ 1 จะต้องมีข้อมูลครบทุกฟีลด์ 
 
Private Sub FillBlankFields() 
Dim dbs As Database, rst2 As Recordset 
Dim intValue1 As Integer 
 
Set dbs = CurrentDb 
Set rst2 = dbs.OpenRecordset("YourTargetTableNameHere") 
 
rst2.MoveFirst 
' I assume the Null/blank field is called field1 
intValue1 = rst2!field1 
Debug.Print intValue1  
 
With rst2 
    .MoveNext 
        Do Until .EOF 
            ' Add the previous value to it if Null or blank. 
            ' Assume the first record is not Null or blank. 
            If IsNull(!field1) Or !field1 = "" Then 
                .Edit 
                    !field1 = intValue1  
                     Debug.Print intValue1  
                .Update 
            Else 
                ' Remember the value 
                intValue1 = !field1 
                Debug.Print intValue1  
            End If 
            .MoveNext 
        Loop 
End With 
 
rst2.Close 
dbs.Close 
Set rst2 = Nothing 
Set dbs = Nothing 
 
End Sub *** Edited by Supap Chaiya *** 5/26/2003 10:46:06 AM

1 Reply in this Topic. Dispaly 1 pages and you are on page number 1

1 @R06525
   
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.0456s