แก้ไขฟังก์ชัน
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 293   5
URL.หัวข้อ / URL
แก้ไขฟังก์ชัน

วันก่อนผมได้สร้างฟังก์ชัน MyReplace() สำหรับใช้ใน Access 97 ไว้ในคำถามที่
http://agserver.kku.ac.th/basiceng/webboard/Question.asp?GID=236

วันนี้มีคนถามคำถามที่ต้องใช้ Replace() ที่

http://board.quicktechusa.com/cgi-bin/ultimatebb.cgi?ubb=get_topic;f=1;t=001974

คำถามเขามีดังนี้ 

Would it be possible to add to an update query, something like replacing "Ca" with "CA" using any part of a large field? 

 

tia  

 

"ต้องการที่จะใช้ Update Query ในการค้นหา "Ca" แล้วแทนด้วย "CA" ในฟีลด์ที่มีขนาดใหญ่จะได้มั๊ย"
 

 

ผมได้ตอบเขาไปดังนี้ 

 

Yes, try the SQL below. 

 

UPDATE Table1 SET Table1.MemoField = Replace(Table1.MemoField,"Ca","CA") 

WHERE (((Table1.MemoField) Like "*")); 

 

In A2K or higher, Replace() is built-in. You have to create your own Replace() in A97. 

 

เช่นกันครับ ถ้าเขาใช้ Access 2K ขึ้นไปจะไม่มีปัญหา เพราะมีฟังก์ชัน Replace ใส่มาให้แล้ว 

แต่ถ้าเป็น 97 จะต้องเขียนขึ้นมาเอง 

 

ผมลองทดสอบฟังก์ชัน MyReplace() เก่าของผม จะไม่สามารถใช้ได้กับการค้นหาตัวอักษรที่มีมากกว่า 1 คำได้ ผมเลยได้ปรับปรุงใหม่ตามโค้ดข้างล่างครับ 

 

Function MyReplace2(strString As String, strWhat As String, strWith As String) As String 

Dim I As Integer, strAll As String, strOut As String 

Dim intLen As Integer 

intLen = Len(strWhat) 

If InStr(strString, strWhat) <> 0 Then 

    For I = 1 To Len(strString) Step intLen 

        strOut = Mid(strString, I, intLen

        If strOut = strWhat Then 

            strOut = strWith 

        End If 

        strAll = strAll & strOut 

    Next I 

End If 

MyReplace2 = strAll 

End Function 

 

โดยใช้ความกว้างของตัวอักษรที่ต้องการจะแทนเข้ามาช่วยด้วย 

 

ถ้าใครนำไปใช้ แล้วเจอบัก ช่วยแจ้งด้วยนะครับ

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

1 @R00466
ผมไปค้นดูคำถามเก่า ว่ามีคนแนะนำฟังก์ชัน Replace ไว้บ้างหรือไม่ ซึ่งผมเคยเห็นผ่านตา

ไปเจออยู่อันหนึ่งที่
http://board.quicktechusa.com/cgi-bin/ultimatebb.cgi?ubb=get_topic;f=1;t=001074

ซึ่งจะมีบักเหมือน MyReplace() อันแรกของผม โดยเขาแนะนำไว้ดังนี้

Public Function ReplaceWithChar(str As String, strFind As String, strNew As String)

Dim intLen As Integer, i As Integer, intPos As Integer

str = Trim(str)
intLen = Len(str)
For i = 1 To intLen
If InStr(i, str, strFind) <> 0 Then
intPos = InStr(i, str, strFind)
str = Left(str, intPos - 1) & " " & Right(str, intLen - intPos)
i = intPos
End If
Next i
ReplaceWithChar = Trim(str)

End Function

ที่ผมนำมาให้ตรงนี้ ไม่ได้หมายความว่า จะเอาเขามาประจาน

แต่ผมต้องการให้ดูการเขียนฟังก์ชัน ทีมีวัตถุประสงค์อันเดียวกัน แต่ใช้คำสั่งต่างกัน
และทุกฟังก์ชันอาจจะมีบักได้ ขึ้นอยู่กับว่าจะนำไปใช้ประโยชน์อย่างไร และการพัฒนาโปรแกรมต่างๆ ต้องเตรียมพร้อมที่จะรับมือกับบักที่จะเกิดขึ้น

ผมว่ายิ่งมีคนแจ้งบักเข้ามามาก ยิ่งทำให้เราแก้ปัญหาได้ง่ายขึ้น และการเรียนรู้จะเกิดตรงนี้แหล่ะครับ
2 @R00478
เจอบักแล้วครับ เหตุเกิดเมื่อผมไปค้นหาว่ามีใครเขียนฟังก์ชันอย่างนี้ไว้บ้าง ก็ไปเจออีกที่หนึ่งที่ http://board.quicktechusa.com/cgi-bin/ultimatebb.cgi?ubb=get_topic;f=1;t=001074 โดยโค้ดของเขาเป็นดังนี้ Public Function ReplaceWithChar(str As String, strFind As String, strNew As String) Dim intLen As Integer, I As Integer, intPos As Integer str = Trim(str) intLen = Len(str) For I = 1 To intLen If InStr(I, str, strFind) <> 0 Then intPos = InStr(I, str, strFind) str = Left(str, intPos - 1) & " " & Right(str, intLen - intPos) I = intPos End If Next I ReplaceWithChar = Trim(str) End Function เมื่อผลลองทดสอบด้วยความนี้ Ca is not CA Ca, but I want to replace Ca with CA โดยการแทน Ca ด้วย CA จะได้ผลลัพธ์ที่ผิดครับ เมื่อทดสอบฟังก์ชัน myreplace2 กับข้อความเดียวกันก็ให้ผลลัพธ์ที่ผิดเช่นกัน ลองดูนะครับว่าผิดอย่างไร ? myreplace2("Ca is not CA Ca, but I want to replace Ca with CA","Ca","CA") ผมเลยได้แก้ไขฟังก์ชัน แต่ยังไม่เสร็จนะครับ แต่อยากให้มาช่วยกันลองแก้บักดูครับ Function MyReplace2(strString As String, strWhat As String, strWith As String) As String Dim I As Integer, strAll As String, strOut As String Dim intLen As Integer, J As Integer intLen = Len(strWhat) J = 0 If InStr(strString, strWhat) <> 0 Then For I = 1 To Len(strString) Step intLen On Error Resume Next strOut = Mid(strString, I - J, Len(strWhat)) If strOut = strWhat Then strOut = strWith If I > 1 Then strOut = Mid(strOut, 2) End If intLen = Len(strWhat) J = 0 Else intLen = 0 strOut = Mid(strString, I, Len(strWhat)) J = 1 End If strAll = strAll & strOut Debug.Print I & " (" & strOut & ") " & strAll Next I End If MyReplace2 = strAll End Function ช่วยกันหน่อยนะครับ ต้องขอตัวไปนอนก่อนแย้ววว...
3 @R00494
แฮะ แฮะ เจอแล้วสำหรับ Replace() ที่ใช้ใน Access97 โดย Microsoft มาเองเลยครับ ที่ http://support.microsoft.com/default.aspx?scid=kb;en-us;q109330 นี่เลย Function Replace(ByVal Valuein As String, ByVal WhatToReplace As _ String, ByVal Replacevalue As String) As String Dim Temp As String, P As Long Temp = Valuein P = InStr(Temp, WhatToReplace) Do While P > 0 Temp = Left(Temp, P - 1) & Replacevalue & _ Mid(Temp, P + Len(WhatToReplace)) P = InStr(P + Len(Replacevalue), Temp, WhatToReplace, 1) Loop Replace = Temp End Function สั้นและเข้าใจง่ายดีครับ เดี๋ยวผมจะปรับของผมมาสู้บ้าง คอยติดตามนะครับ
4 @R00495
เมื่อมองโค้ดที่ Microsoft ใช้ ก็ทำให้ผมนึกถึงโค้ดตัวหนึ่งของผม ที่ผมเขียนขึ้นมาเมื่อเกือบ 2 ปีมาแล้ว คือ การตัดสัญญาณเคาะขึ้นบันทัดใหม่ของฟีลด์ที่เป็นแบบ Memo ออก ตอนนั้นผมต้องการที่จะตัดบันทัดที่มีการเคาะเว้นบันทัดเกินไป 3 บรรทัดออก ซึ่งโค้ดมีดังนี้ Function TrimText3(temp As String) As String Do While Len(temp) <> 0 If InStr(temp, vbCrLf & vbCrLf & vbCrLf) <> 0 Then temp = Left(temp, InStr(temp, vbCrLf & vbCrLf & vbCrLf) - 1) & Right(temp, Len(temp) - InStr(temp, vbCrLf & vbCrLf & vbCrLf)) Else Exit Do End If Loop TrimText3 = temp End Function และผมเลยได้ปรับให้มาใช้เป็นฟังก์ชันสำหรับจำลองการทำงานเหมือน Replace() ของไมโครซอฟตมั่ง Ta Da!!! ออกมาแล้วครับ Function MyReplace4(strText As String, strFind As String, strReplace As String) As String Dim strOut As String Do While Len(strText) <> 0 If InStr(strText, strFind) <> 0 Then strOut = strOut & Left(strText, InStr(strText, strFind) - 1) & strReplace strText = Right(strText, Len(strText) - InStr(strText, strFind) - 1) Else Exit Do End If Loop MyReplace4 = strOut End Function ถ้าใครว่างๆ ลองช่วยทดสอบให้ด้วยนะครับ
5 @R00496
ตู-แว่วววววๆๆๆๆๆ แก้ไขครับ Function MyReplace4(strText As String, strFind As String, strReplace As String) As String Dim strOut As String Do While Len(strText) <> 0 If InStr(strText, strFind) <> 0 Then strOut = strOut & Left(strText, InStr(strText, strFind) - 1) & strReplace strText = Right(strText, Len(strText) - InStr(strText, strFind) - (Len(strFind) - 1)) Else strOut = strOut & strText Exit Do End If Loop MyReplace4 = strOut End Function
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.0961s