Automate table creation based on data
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 233   1
URL.หัวข้อ / URL
Automate table creation based on data

มีคนถามไว้ที่ 
http://board.quicktechusa.com/cgi-bin/ultimatebb.cgi?ubb=get_topic;f=1;t=001857#000001 
 
มีเนื้อหาของคำถามดังนี้ 
 
I have a table with several records with several region ids. I need to break this table into several regional tables having its own data to send to each region. How would I acomplish this without creating a 150 if statements. 
 
150 regionids  
07500001 
07500033 
07500054 
07500106 
… 
 
Each table needs to be named tblRegion07500001, tblRegion07500033, ….. 
 
I would like to copy the main table structure name each as described above and append the data with the cooresponding regionid (data comes from the main table). 
 
The data in the main table may look like this: 
 
CHAPTERID CUSTOMER MailName 
07500001 02037559 Mr M R Riely 
07500001 07934411 Mr George Wallace 
07500033 07983334 Mr Mark Wells 
07500054 07963253 Ms Kim Bates 
07500054 07974073 Mr Jim Broills 
07500106 05298502 Mrs Kate Jackson 
07500106 07980532 Ms Susan Weather 
07500107 07983167 Mr Lia Simmons 
07500503 05023540 Mrs Micah Brothers 
 
How would I accomplish this programmically? Manual is not preferred.  
-------------------------------------------------------------------------------- 
 
คือเขาต้องการที่จะสร้างตารางแยกข้อมูลออกเป็นตารางตามรหัสพื้นที่ โดยตั้งชื่อให้เป็น tblRegion0750001 ไล่ไปเรื่อยๆ 
 
 
หลักการ 
1. ให้สร้าง SQL เพื่อเลือกเฉพาะหมายเลขรหัสพื้นที่ออกมาก่อนว่ามีกี่นที่  
2. ใช้ MakeTable Query ในการสร้างตารางใหม่ตามชื่อที่ต้องการ 
 
ผมเลยได้เขียนโค้ดตามหลักการดังนี้ 
 
Sub CreateTables() 
Dim dbs As Database, rst As Recordset 
Dim I As Integer 
Set dbs = CurrentDb 
Set rst = dbs.OpenRecordset("SELECT DISTINCT tblMain.ChapterID FROM tblMain;") 
 If Not rst.EOF Then 
    For I = 1 To rst.RecordCount 
        dbs.Execute "SELECT tblMain.ChapterID, tblMain.Customer, tblMain.MailName INTO tblRegion" & rst(0) _ 
            & " FROM tblMain " _ 
            & "WHERE tblMain.ChapterID='" & rst(0) & "';" 
        rst.MoveNext 
    Next I 
End If 
rst.Close 
dbs.Close 
Set rst = Nothing 
Set dbs = Nothing 
 
End Sub 
 
ตัวอย่างจริงอยู่ที่ http://agserver.kku.ac.th/basiceng/maketablequery.zip ครับ 
 
การทดสอบการทำงานของโค้ด ให้เปิด Module1 แล้วเอาเคอร์เซอร์วางในข้อความของโค้ดแล้วกด F5 จากนั้นให้ไปในแถบ Table จะเห็นว่าตารางได้ถูกสร้างขึ้นให้ตามจำนวนรหัสพื้นที่

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

1 @R00434
เขาได้ถามต่ออีกว่า ถ้าต้องการจะลบตารางที่ได้สร้างขึ้นหล่ะ จะทำอย่างไร ตั้ง 150 ไฟล์ ผมเลยได้ให้โค้ดนี้กับเขาไปครับ Sub DeleteTables() Dim dbs As Database, tdf As TableDef Set dbs = CurrentDb For Each tdf In dbs.TableDefs If Left(tdf.Name, 9) = "tblRegion" Then DoCmd.DeleteObject acTable, tdf.Name End If Next Set tdf = Nothing dbs.Close Set dbs = Nothing End Sub
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.0555s