ใช้ For Each ตรวจสอบ Textbox เพื่อเปลี่ยนสีตามคำที่กำหนด ติดปัญหานิดนึงครับ


0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้

26 ต.ค. 64 , 14:59:46
อ่าน 640 ครั้ง

001Jetnipit





ไม่ทราบว่า ผมติดตรงที่ใส่สีให้ Textbox แต่ผมเขียนโค้ดผิดหรือเปล่าครับเลย Error ตรงนั้น

 

26 ต.ค. 64 , 18:03:55
ตอบกลับ #1

UnKnown

ลองแก้ Ctl.BackColor เป็น StatusSamsung_Age.BackColor = RGB(0, 255, 0) ดูครับ
StatusSamsung_Age คือชื่อ
หรือไม่ก็คงตรงที่มีคำสั่งให้ Exit For (ผมไม่รู้จักหรือเคยใช้ :question:)


ปล.ส่วนที่เป็นโค้ดก็อบมาวางเลยก็จะดีนะ :dizzy:
« แก้ไขครั้งสุดท้าย: 26 ต.ค. 64 , 19:15:15 โดย UnKnown »
:ninja:     ลองคิด,ลองทำแนวคนไม่เก่งแอคเซส
หมายเหตุ เพื่อความปลอดภัยโปรดสำรองข้อมูลใว้ก่อนการแก้ไข
 
โพสต์นี้ได้รับคำขอบคุณจาก: 001Jetnipit

26 ต.ค. 64 , 20:23:34
ตอบกลับ #2

SakDa

มีคนถามว่า อยากให้เช็คเท็กซ์บ็อกซ์ ถ้าว่างให้เปลี่ยนสีให้หน่อย
Text box name = HomeMobile

What event should I use on the report: • Private Sub Report_Load() • Private Sub Report_Open(Cancel As Integer)

Private Sub Report_Open(Cancel As Integer)

If IsNull(Textbox.Value) = True Then
Textbox.BackColor = vbRed

End If

End Sub

อีกคำตอบ


You could try with the Format or Print event, and also arrange for the colour to be reset:

Textbox.BackColor = IIf(IsNull(Textbox.Value), vbRed, vbWhite)

อันนี้คำถามว่า อยากให้ฟอร์มโหลดขึ้นมาแล้วเปลี่ยนสีทุกกล่องข้อความ

You can place this code in the Onload event of your form and this will change all of your boxes at one time.
Delcare the control:
Dim tb as Control
Code:
 For Each tb In Me.Controls
        If TypeOf tb Is TextBox Then
            tb.BackColor = 'your color goes here
        End If
    Next
-----------------------------------------------------------------------------
อันนี้โค้ดของคนถาม
For Each Ctl In Detail.Controls
'ตรวจสอบเฉพาะที่เป็นประเภท TextBox / Combo Box
If Ctl.ControlType = acTextBox Or Ctl.ControlType = acComboBox Then 'Or ctl.ControlType = acCheckBox
'และเฉพาะที่ชื่อชื้นต้นด้วย Status
If Ctl.Name Like "Status*" Then
'ถ้าได้ค่าว่างๆ
If Ctl &"" = "Less than 1 day" Then
isBlank = "LT"
Elsef Ctl &"" = "More than 1 day" Then
isBlank = "MT"
Elsef Ctl &"" = "Card expired" Then
isBlank = "C"
Exit For
End If
End If
Next
If isBlank = "LT" Then
Ctl.BackColor = vbYellow
Elself isBlank = "MT" Then
Ctl.BackColor = vbGreen
ElseIf isBlank = "C" Then
Ctl.BackColor = vbRed
Exit Sub
End If
End if


 
โพสต์นี้ได้รับคำขอบคุณจาก: 001Jetnipit

27 ต.ค. 64 , 08:28:19
ตอบกลับ #3

001Jetnipit

เบื้องต้นเมื่อวานลอง งมๆมั่วๆดูครับได้โค้ดออกมาเป็นแบบนี้

โค๊ด: [Select]

Private Sub Detail_Paint()

For Each Ctl In Detail.Controls
        If Ctl.ControlType = acTextBox Or Ctl.ControlType = acComboBox Then
            If Ctl.Name Like "Status*" Then
               If Ctl & "" = "Less than 1 day" Then
                        Ctl.BackColor = vbYellow
               ElseIf Ctl & "" = "More than 1 day" Then
                        Ctl.BackColor = vbGreen
                ElseIf Ctl & "" = "Card expired" Then
                        Ctl.BackColor = vbRed
                  Exit For
               End If
           End If
        End If
Next

End Sub


แต่ติดปัญหาตรงคำว่า Card Expire มันไม่ลงสีทุกช่อง



พอลองลบคำว่า Exit For ออก ตอนนี้หายแล้วครับ สำเร็จแล้วครับ ขอบคุณครับผม

« แก้ไขครั้งสุดท้าย: 27 ต.ค. 64 , 08:50:21 โดย 001Jetnipit »

 


บอร์ดเรียนรู้ Access สำหรับคนไทย


 

Sitemap 1 2 3 4 5