กระทู้เก่าบอร์ด อ.Yeadram
929 3
URL.หัวข้อ /
URL
รบกวนช่วยแนะนำย่อ If ให้หนูหน่อยค่ะ
คือหนูเขียน If ไม่ค่อยเป็นน่ะค่ะติดปัญหาอยู่ว่า
หนูเช็คค่า
M = 1,2,3....12 : ตรงนี้คือเดือน 1-12 ค่ะ
และ P = "PRINTER INKJET"
ถ้าใช่ "Update [QR_PlanPrinter] Set [JAN_P] = D Where [HISTYPE]=P"
ถ้าไม่ใช่ "Update [QR_PlanPrinter] Set [JAN_P] = D"
ทำแบบนี้ทั้งหมด 12 เดือนค่ะ
คือตอนนี้ติดที่ว่าถ้าเป็นเดือน 2,3,4...-12 ทำไมมันไม่ทำงานค่ะ
และมีวิธีเขียนให้มันสั้นลงได้หรือไม่ค่ะ หนูได้แนบ Code ที่หนูลอง
เขียนมาด้านล่าง ช่วยหนูดูหน่อยค่ะ
ขอบคุณอาจารย์ค่ะ
Private Sub Command159_Click()
Dim strSQL1A As String
Dim strSQL2A As String
Dim strSQL3A As String
Dim strSQL4A As String
Dim strSQL5A As String
Dim strSQL6A As String
Dim strSQL7A As String
Dim strSQL8A As String
Dim strSQL9A As String
Dim strSQL10A As String
Dim strSQL11A As String
Dim strSQL12A As String
Dim strSQL1B As String
Dim strSQL2B As String
Dim strSQL3B As String
Dim strSQL4B As String
Dim strSQL5B As String
Dim strSQL6B As String
Dim strSQL7B As String
Dim strSQL8B As String
Dim strSQL9B As String
Dim strSQL10B As String
Dim strSQL11B As String
Dim strSQL12B As String
If M.Value = 1 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL1A = "Update [QR_PlanPrinter] Set [JAN_P] = D Where [HISTYPE]=P"
DoCmd.RunSQL strSQL1A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL1B = "Update [QR_PlanPrinter] Set [JAN_P] = D"
DoCmd.RunSQL strSQL1B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
If M.Value = 2 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL2A = "Update [QR_PlanPrinter] Set [FEB_P] = D Where [HISTYPE]=P "
DoCmd.RunSQL strSQL2A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL2B = "Update [QR_PlanPrinter] Set [FEB_P] = D"
DoCmd.RunSQL strSQL2B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
If M.Value = 3 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL3A = "Update [QR_PlanPrinter] Set [MAR_P] = D Where [HISTYPE]=P "
DoCmd.RunSQL strSQL3A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL3B = "Update [QR_PlanPrinter] Set [MAR_P] = D"
DoCmd.RunSQL strSQL3B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
If M.Value = 4 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL4A = "Update [QR_PlanPrinter] Set [APR_P] = D Where [HISTYPE]=P "
DoCmd.RunSQL strSQL4A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL4B = "Update [QR_PlanPrinter] Set [APR_P] = D"
DoCmd.RunSQL strSQL4B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
If M.Value = 5 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL5A = "Update [QR_PlanPrinter] Set [MAY_P] = D Where [HISTYPE]=P "
DoCmd.RunSQL strSQL5A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL5B = "Update [QR_PlanPrinter] Set [MAY_P] = D"
DoCmd.RunSQL strSQL5B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
If M.Value = 6 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL6A = "Update [QR_PlanPrinter] Set [JUN_P] = D Where [HISTYPE]=P "
DoCmd.RunSQL strSQL6A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL6B = "Update [QR_PlanPrinter] Set [JUN_P] = D"
DoCmd.RunSQL strSQL6B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
If M.Value = 7 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL7A = "Update [QR_PlanPrinter] Set [JUL_P] = D Where [HISTYPE]=P "
DoCmd.RunSQL strSQL7A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL7B = "Update [QR_PlanPrinter] Set [JUL_P] = D"
DoCmd.RunSQL strSQL7B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
If M.Value = 8 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL8A = "Update [QR_PlanPrinter] Set [AUG_P] = D Where [HISTYPE]=P "
DoCmd.RunSQL strSQL8A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL8B = "Update [QR_PlanPrinter] Set [AUG_P] = D"
DoCmd.RunSQL strSQL8B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
If M.Value = 9 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL9A = "Update [QR_PlanPrinter] Set [SEP_P] = D Where [HISTYPE]=P "
DoCmd.RunSQL strSQL9A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL9B = "Update [QR_PlanPrinter] Set [SEP_P] = D"
DoCmd.RunSQL strSQL9B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
If M.Value = 10 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL10A = "Update [QR_PlanPrinter] Set [OCT_P] = D Where [HISTYPE]=P "
DoCmd.RunSQL strSQL10A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL10B = "Update [QR_PlanPrinter] Set [OCT_P] = D"
DoCmd.RunSQL strSQL10B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
If M.Value = 11 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL11A = "Update [QR_PlanPrinter] Set [NOV_P] = D Where [HISTYPE]=P "
DoCmd.RunSQL strSQL11A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL11B = "Update [QR_PlanPrinter] Set [NOV_P] = D"
DoCmd.RunSQL strSQL11B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
If M.Value = 12 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL12A = "Update [QR_PlanPrinter] Set [DEC_P] = D Where [HISTYPE]=P "
DoCmd.RunSQL strSQL12A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL12B = "Update [QR_PlanPrinter] Set [DEC_P] = D"
DoCmd.RunSQL strSQL12B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End Sub
หนูเช็คค่า
M = 1,2,3....12 : ตรงนี้คือเดือน 1-12 ค่ะ
และ P = "PRINTER INKJET"
ถ้าใช่ "Update [QR_PlanPrinter] Set [JAN_P] = D Where [HISTYPE]=P"
ถ้าไม่ใช่ "Update [QR_PlanPrinter] Set [JAN_P] = D"
ทำแบบนี้ทั้งหมด 12 เดือนค่ะ
คือตอนนี้ติดที่ว่าถ้าเป็นเดือน 2,3,4...-12 ทำไมมันไม่ทำงานค่ะ
และมีวิธีเขียนให้มันสั้นลงได้หรือไม่ค่ะ หนูได้แนบ Code ที่หนูลอง
เขียนมาด้านล่าง ช่วยหนูดูหน่อยค่ะ
ขอบคุณอาจารย์ค่ะ
Private Sub Command159_Click()
Dim strSQL1A As String
Dim strSQL2A As String
Dim strSQL3A As String
Dim strSQL4A As String
Dim strSQL5A As String
Dim strSQL6A As String
Dim strSQL7A As String
Dim strSQL8A As String
Dim strSQL9A As String
Dim strSQL10A As String
Dim strSQL11A As String
Dim strSQL12A As String
Dim strSQL1B As String
Dim strSQL2B As String
Dim strSQL3B As String
Dim strSQL4B As String
Dim strSQL5B As String
Dim strSQL6B As String
Dim strSQL7B As String
Dim strSQL8B As String
Dim strSQL9B As String
Dim strSQL10B As String
Dim strSQL11B As String
Dim strSQL12B As String
If M.Value = 1 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL1A = "Update [QR_PlanPrinter] Set [JAN_P] = D Where [HISTYPE]=P"
DoCmd.RunSQL strSQL1A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL1B = "Update [QR_PlanPrinter] Set [JAN_P] = D"
DoCmd.RunSQL strSQL1B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
If M.Value = 2 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL2A = "Update [QR_PlanPrinter] Set [FEB_P] = D Where [HISTYPE]=P "
DoCmd.RunSQL strSQL2A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL2B = "Update [QR_PlanPrinter] Set [FEB_P] = D"
DoCmd.RunSQL strSQL2B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
If M.Value = 3 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL3A = "Update [QR_PlanPrinter] Set [MAR_P] = D Where [HISTYPE]=P "
DoCmd.RunSQL strSQL3A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL3B = "Update [QR_PlanPrinter] Set [MAR_P] = D"
DoCmd.RunSQL strSQL3B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
If M.Value = 4 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL4A = "Update [QR_PlanPrinter] Set [APR_P] = D Where [HISTYPE]=P "
DoCmd.RunSQL strSQL4A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL4B = "Update [QR_PlanPrinter] Set [APR_P] = D"
DoCmd.RunSQL strSQL4B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
If M.Value = 5 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL5A = "Update [QR_PlanPrinter] Set [MAY_P] = D Where [HISTYPE]=P "
DoCmd.RunSQL strSQL5A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL5B = "Update [QR_PlanPrinter] Set [MAY_P] = D"
DoCmd.RunSQL strSQL5B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
If M.Value = 6 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL6A = "Update [QR_PlanPrinter] Set [JUN_P] = D Where [HISTYPE]=P "
DoCmd.RunSQL strSQL6A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL6B = "Update [QR_PlanPrinter] Set [JUN_P] = D"
DoCmd.RunSQL strSQL6B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
If M.Value = 7 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL7A = "Update [QR_PlanPrinter] Set [JUL_P] = D Where [HISTYPE]=P "
DoCmd.RunSQL strSQL7A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL7B = "Update [QR_PlanPrinter] Set [JUL_P] = D"
DoCmd.RunSQL strSQL7B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
If M.Value = 8 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL8A = "Update [QR_PlanPrinter] Set [AUG_P] = D Where [HISTYPE]=P "
DoCmd.RunSQL strSQL8A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL8B = "Update [QR_PlanPrinter] Set [AUG_P] = D"
DoCmd.RunSQL strSQL8B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
If M.Value = 9 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL9A = "Update [QR_PlanPrinter] Set [SEP_P] = D Where [HISTYPE]=P "
DoCmd.RunSQL strSQL9A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL9B = "Update [QR_PlanPrinter] Set [SEP_P] = D"
DoCmd.RunSQL strSQL9B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
If M.Value = 10 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL10A = "Update [QR_PlanPrinter] Set [OCT_P] = D Where [HISTYPE]=P "
DoCmd.RunSQL strSQL10A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL10B = "Update [QR_PlanPrinter] Set [OCT_P] = D"
DoCmd.RunSQL strSQL10B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
If M.Value = 11 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL11A = "Update [QR_PlanPrinter] Set [NOV_P] = D Where [HISTYPE]=P "
DoCmd.RunSQL strSQL11A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL11B = "Update [QR_PlanPrinter] Set [NOV_P] = D"
DoCmd.RunSQL strSQL11B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
If M.Value = 12 Then
If P.Value = "PRINTER INKJET" Then
DoCmd.SetWarnings False
strSQL12A = "Update [QR_PlanPrinter] Set [DEC_P] = D Where [HISTYPE]=P "
DoCmd.RunSQL strSQL12A
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Else
DoCmd.SetWarnings False
strSQL12B = "Update [QR_PlanPrinter] Set [DEC_P] = D"
DoCmd.RunSQL strSQL12B
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End Sub
3 Reply in this Topic. Dispaly 1 pages and you are on page number 1
2 @R23521
ใช้ฟังก์ชั่น Format( ) ในการแปลงเลขที่เป็นชื่อเดือน และใช้ฟังก์ชั่น Ucase( ) เพื่อแปลงให้เป็นอักษรตัวใหญ่ ส่วนค่าคงที่ที่เป็นอักษร D และ P ควรต้องมีเครื่องหมาย single quote ครอบด้วยครับ
Dim SQL As String
SQL = "Update [QR_PlanPrinter] Set [" + Ucase(Format(M,"MMM")) + "_P] = 'D' "
DoCmd.SetWarnings False
DoCmd.RunSQL IIf(P = "PRINTER INKJET" , SQL + " Where [HISTYPE] = 'P' " , SQL)
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
Dim SQL As String
SQL = "Update [QR_PlanPrinter] Set [" + Ucase(Format(M,"MMM")) + "_P] = 'D' "
DoCmd.SetWarnings False
DoCmd.RunSQL IIf(P = "PRINTER INKJET" , SQL + " Where [HISTYPE] = 'P' " , SQL)
DoCmd.SetWarnings True
On Error Resume Next
Me.Requery
3 @R23532
ขอบคุณอาจาย์สันติสุขมากค่ะ
ตอนนี้หนูย่อตามอาจารย์เป็นแล้วค่ะ ได้ประโยชน์มากค่ะ
ขอบคุณค่ะ
ตอนนี้หนูย่อตามอาจารย์เป็นแล้วค่ะ ได้ประโยชน์มากค่ะ
ขอบคุณค่ะ
Time: 0.2961s
แต่หนูจะย่อได้แบบไหนมั่งค่ะ เพราะมันยาวมาก
ขอบคุณค่ะ