235
ห้อง MS Access / : Hide Duplicates
« เมื่อ: 23 ธ.ค. 61 , 15:10:15 »
=F(PID, Visit_No, Visit_Date)
โพสต์นี้ได้รับคำขอบคุณจาก: Un
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
' โค้ดนี้เป็นแค่แนวทาง ต้องไปปรับปรุงเองนะครับ
Public Function F(aPK1 as Variant , ... , aPKn as Variant , aF as Variant) as Variant
Dim RS As DAO.Recordset
' กรณีเป็นบรรทัดของ New Record ค่าที่ส่งมาจะเป็น NULL
' ก็ไม่ต้องทำอะไร ออกจากฟังก์ชั่นได้เลย
If IsNull(aPK1) Then Exit Function
' กำหนด recordset ของฟอร์มที่โค้ดจะใช้ในการค้นหาเพื่อเปรียบเทียบ
Set RS = Forms("ชื่อฟอร์ม").RecordsetClone
' ทำการรวบรวมทุกเรคอร์ดให้ recordset รู้จัก
RS.MoveLast
' ค้นหาเรคอร์ดที่มีค่าตรงกับ Primary Key ของบรรทัดนั้นๆ
RS.FindFirst " ฟิลด์1 = " & aPK1 & " and ฟิลด์2 = " & aPK2 & ... & " and ฟิลด์n = " & aPKn
' ถ้าเป็นบรรทัดแรก ค่าที่ส่งกลับไปแสดงก็คือค่าที่ส่งเข้ามานั่นเอง
If RS.AbsolutePosition = 0 Then
F = aF
Exit Function
End If
' ย้อนกลับไป 1 เรคอร์ด (บรรทัด)
RS.MovePrevious
' ถ้าค่าของฟิลด์ในบรรทัดก่อนหน้าไม่เท่ากับค่าที่ส่งมา ก็ให้ส่งกลับไปแสดงค่าที่ส่งเข้ามาเช่นกัน
If RS("ชื่อฟิลด์") < > aF Then
F = RS("ชื่อฟิลด์")
End If
' แต่ถ้าเท่ากัน ก็ไม่ต้องส่งอะไรกลับ มันจะเป็น NULL เป็นค่าว่างๆ
End Function
Public Sub CutLastCRLF(aFullFileNM As String)
Dim B As String * 1
Dim S As String
Dim L As Long
Open aFullFileNM For Binary Access Read As #1
Do Until EOF(1)
Get #1, , B
S = S & B
Loop
Close #1
If Right(S, 3) <> vbCr & vbLf & vbNullChar Then
Exit Sub
End If
S = Left(S, Len(S) - 3)
On Error Resume Next
Kill aFullFileNM
On Error GoTo 0
Open aFullFileNM For Binary Access Write As #1
Put #1, , S
Close #1
End Sub
จะเรียกใช้ก็สั่ง Call CutLastCRLF("Drive:\Path\File.txt") มันจะเขียนทับไฟล์เดิมครับ
Public Function fnGetDiscount(aCusCD As Variant, aProdCD As Variant) As Variant
Dim DB As DAO.Database
Dim RS As DAO.Recordset
Dim SQL As String
If IsNull(aCusCD) or IsNull(aProdCD) Then Exit Sub
SQL = "SELECT D.Discount FROM [Prod] AS P " _
& " INNER JOIN ([Cust] AS C INNER JOIN [Disc] AS D ON C.[Menber Grade] = D.[Menber Grade])" _
& " ON P.[Catagory Name] = D.[Catagory Name] " _
& " WHERE C.[Customer Code] = '" & cStr(aCusCD) & "' AND P.[Product Code] = '" & cStr(aProdCD) & "' "
Set DB = CurrentDb
Set RS = DB.OpenRecordset(SQL)
If Not RS.EOF Then
fnGetDiscount = RS![Discount]
End If
RS.Close: Set RS = Nothing
End Sub
Private Sub cb_Product_Code_AfterUpdate()
Me.[tx Discount] = fnGetDiscount(Parent.[cb Customer Code], Me.[cb Product Code])
End Sub