Split() in VBA
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 305   1
URL.หัวข้อ / URL
Split() in VBA

เขาถามไว้ที่ http://www.utteraccess.com/forums/showflat.php?Cat=&Board=access_any_version&Number=159529&page=0&view=collapsed&sb=5&o=7&fpart=&vc=1&PHPSESSID= 
 
โดยเขาแนบตัวอย่างไฟล์ของเขามาให้ดูด้วย ที่ http://www.utteraccess.com/forums/download.php?Number=158519 
 
เขาต้องการที่จะแยกหมายเลขโทรศัพท์ ซึ่งเป็นไว้ในฟีลด์เดียวกัน แต่ใส่หลายเบอร์ จึงต้องการที่จะแยกแต่ละเบอร์ให้อยู่กันคนละฟีลด์ แต่ในตารางเดิม 
 
ผมแนะนำให้เขาสร้างเป็นตารางใหม่เพิ่มขึ้นมาอีกอันหนึ่ง เพื่อเก็บหมายเลขโทรศัพท์โดยเฉพาะ เพื่อให้การจัดการข้อมูลทำได้ง่ายขึ้น เพราะถ้าเกิดมีบริษัทใดมีหมายเลขโทร. มากกว่าจำนวนฟีลด์ที่กำหนดไว้ จะทำอย่างไร 
 
แต่เขายังยืนยันที่จะทำตามที่ตั้งใจไว้ คือ แยกไปใส่ฟีลด์ใหม่ที่เพิ่มเข้าไปในตารางเดียวกัน 
 
ผมเลยให้โค้ดเขาไปดังนี้ครับ 
 
Private Sub SplitTelNo() 
   Dim dbs As DAO.Database, rst As DAO.Recordset 
   Dim strTest As String 
   Dim strArray() As String 
   Dim intCount As Integer, I As Integer 
    
   Set dbs = CurrentDb 
   Set rst = dbs.OpenRecordset("ProfessionalUser1Test") 
   If Not rst.EOF Then 
    rst.MoveFirst 
        For I = 1 To rst.RecordCount 
            strTest = rst("Tel Number") 
            ' If there are 2 tel. no. in this field. 
            If InStr(strTest, "``") > 0 Then 
                strArray = Split(strTest, "``") 
                 
                For intCount = LBound(strArray) To UBound(strArray) 
                   rst.Edit 
                        rst("TelNum" & intCount + 2) = strArray(intCount) 
                   rst.Update 
                Next 
            Else 
                ' If only one tel. no., write it to TelNum2 
                ' so you can delete the original tel. no. field when finished. 
                rst.Edit 
                    rst("TelNum2") = rst("Tel Number") 
                rst.Update 
            End If 
            rst.MoveNext 
        Next I 
    End If 
 
End Sub 
 
*** Edited by Supap Chaiya *** 30/5/2546 22:28:05

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

1 @R03324
อีกตัวอย่างหนึ่งใน Access97 ที่ http://www.utteraccess.com/forums/showflat.php?Cat=&Board=access_97&Number=223618
เป็นตัวอย่างใน 97 ครับ
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.0570s