การสร้าง Control ใน Form
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 271   3
URL.หัวข้อ / URL
การสร้าง Control ใน Form

มี Table1 ประกอบด้วย ฟิลด์และตัวอย่างข้อมูลดังนี้
Model Part1 Part2 Part9
A 1 2 3
B 3 1 4
C 6 7 9
แล้วสร้างฟอร์มแบบ Datasheet โดยใช้ชื่อ Textbox เหมือนชื่อฟิลด์ใน Table1
ต่อมาต้องการเพิ่มฟิลด์ Part3,Part4 ใน Table1
จะเขียน Code อย่างไร เพื่อเพิ่มฟิลด์ Part3,Part4 ใน Table1 แล้วสร้าง TextBox Part3,Part4 ใน Form1 ด้วยโดยเรียงลำดับ Tab ใหม่ จะได้
Model Part1 Part2 Part3 Part4 Part9
A 1 2 3
B 3 1 4
C 6 7 9

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

1 @R02232
ให้ดูกระทู้ http://www.thai-access.com/suphap.php?topic_id=591 จะมีตัวอย่างสำหรับเพิ่มฟีลด์เข้าไปในตารางที่สร้างไว้แล้ว

และกระทู้ http://www.thai-access.com/suphap.php?topic_id=597 จะเป็นการตัวอย่างการสร้าง control เข้าไปใน form

แต่ต้องเปิด form เป้าหมายในโหมดออกแบบ แล้วจึงจะสามารถเพิ่ม control ใหม่ลงไปได้ครับ

2 @R02236
ลองดูโค้ดนี้ครับ

Sub AddAutoNoField()
Dim dbs As Database
Set dbs = CurrentDb
dbs.Execute "ALTER TABLE Table1 " _
& "ADD COLUMN Part3 Long;"
dbs.Execute "ALTER TABLE Table1 " _
& "ADD COLUMN Part4 Long;"
dbs.Close
Set dbs = Nothing
End Sub

Sub CreateCommandButton1()
Dim frm As Form
Dim ctlTextBox As Control
Dim intLeft As Integer, intTop As Integer

DoCmd.OpenForm "Form1", acDesign
Set frm = Forms("form1")

Set ctlTextBox = CreateControl(frm.Name, acTextBox, , "", "", _
intLeft, intTop)
With ctlTextBox
.Name = "Part3"
.ControlSource = "Part3"
.TabIndex = 3
End With

Set ctlTextBox = CreateControl(frm.Name, acTextBox, , "", "", _
intLeft, intTop)
With ctlTextBox
.Name = "Part4"
.ControlSource = "Part4"
.TabIndex = 4
End With

' เปลี่ยน Tab Order ของ Part9 ให้อยู่หลังสุด
frm.Part9.TabIndex = 5

DoCmd.Close acForm, frm.Name, acSaveYes

End Sub
3 @R02283
ขอบคุณคำแนะนำของอาจารย์สุภาพเป็นอย่างสูง
เดี๋ยวผมจะลองนำไปปฏิบัติตามที่อาจารย์แนะนำ
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.0791s