Converse Text To Date
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 327   1
URL.หัวข้อ / URL
Converse Text To Date

มีคนถามไว้ที่ 
 
คำถามมีดังนี้ 
I have a problem where by I need to change values in an access table which  
actually represents a date, they are stored like yymmdd, I have preceeding  
zeros to represent 2000 etc...ie. 000101 = jan 1st 2000. The problem I have is  
when I try to convert these dates into a new field name using:-  
 
CDate(Left(myFld,2) & "/" & Mid(myFld,3,2) & "/" & Right(myFld,2))  
 
I am getting a problem with the conversion some dates it does fine eg. 970125  
converts to 25/01/1997 whereas others don't..eg. 000818 converts to 18/08/1981  
whereas it should be 18/08/2000. Is there anyway of stopping this or would it be  
an idea to change the original number to ddmmyy format then do the same  
conversion. Any help would be much appreciated .  
 
Ps. If I do need to switch the date order any help with how to do it would also be handy. 
 
สรุป ต้องการแปลงวันที่ซึ่งเก็บไว้แบบ Text ในรูปแบบ yymmdd แต่ต้องการแปลงให้เป็นวันที่จริง เข้าใช้สูตรแปลงข้างบน ซึ่งจะไม่ปัญหาอะไรถ้า 2 ตัวแรกซึ่งเป็น ปี มีค่าเป็น 97, 98, 99  
แต่จะมีปัญหากับปีที่เป็นแบบ 00 หรือปี 2000 
 
ปัญหานี้แก้โดยการใช้ฟังก์ชัน DateSerial() เข้ามาช่วยครับ ดังนี้ 
 
ผมสมมติข้อมูลในตารางเป็นดังนี้ 
 
TextDate 
000101 
000818 
970125 
 
Function Conver2Date2(strDate As String) 
Dim NewDate As Date 
    NewDate = DateSerial(Left(strDate, 2), Mid(strDate, 3, 2), Right(strDate, 2)) 
    Conver2Date2 = NewDate 
End Function 
 
เมื่อลองใช้ฟังก์ชันที่เขาให้มา และฟังก์ชันที่ผมสร้างขึ้น จะได้ผมดังนี้ครับ 
 
TextDate ฟังก์ชันของเขา MyFunction 
000101 1/1/2500                        1/1/2543 
000818 18/8/2500                        18/8/2543 
970125 25/1/2497                         25/1/2540 
 
ให้ศึกษาฟังก์ชัน DateSerial เพิ่มเติมใน Help นะครับ 

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

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