กระทู้เก่าบอร์ด อ.Yeadram
8,538 14
URL.หัวข้อ /
URL
ปัญหาการสร้างฟอร์ม login โดยใช้ Code Builder ค่ะ
ใช้ Access 2010 เขียนโปรแกรม Helpdesk ค่ะ และซื้อหนังสือ "สร้างระบบ Helpdesk ด้วย Access 2007" มาเป็นคู่มือค่ะ
มีปัญหาเรื่องการสร้างฟอร์ม Login ค่ะ คือ พิมพ์โค้ดใน code builder เสร็จและถูกต้อง แต่พอมาเปิดหน้า form view พิมพ์ User & Password แล้วคลิก login หรือ cancel ไม่ไปเลยค่ะ ไม่มีการแจ้งเตือนหรือ window อะไรขึ้นมาเลยค่ะ
แต่พอลองมาเปลี่ยนไปใช้ Macro builder พิมพ์คำสั่งง่ายๆไป คลิกได้นะคะ
ปัญหาคือ ใช้ macro builder ไม่เป็นค่ะ ในหนังสือไม่มีบอกด้วย
มีแต่ Code ใน Code builder ต้องทำยังไงดีคะ?
มีปัญหาเรื่องการสร้างฟอร์ม Login ค่ะ คือ พิมพ์โค้ดใน code builder เสร็จและถูกต้อง แต่พอมาเปิดหน้า form view พิมพ์ User & Password แล้วคลิก login หรือ cancel ไม่ไปเลยค่ะ ไม่มีการแจ้งเตือนหรือ window อะไรขึ้นมาเลยค่ะ
แต่พอลองมาเปลี่ยนไปใช้ Macro builder พิมพ์คำสั่งง่ายๆไป คลิกได้นะคะ
ปัญหาคือ ใช้ macro builder ไม่เป็นค่ะ ในหนังสือไม่มีบอกด้วย
มีแต่ Code ใน Code builder ต้องทำยังไงดีคะ?
14 Reply in this Topic. Dispaly 1 pages and you are on page number 1
2 @R16087
ลองพิมพ์โค๊ดที่เขียนให้ดูหน่อยครับ
3 @R16098
อันนี้ใน module (modGlobals)
Option Compare Database
Public Const gstrAppTitle As String = "Helpdesk Management System"
Public gstrThisUser As String
Public gstrThisUserRole As String
อันนี้ใน frmLogin ค่ะ
Private Sub cmdLogin_Click()
Static intPswdCount As Integer
Dim strThisUserPassword As String
If IsNull(Me.txtUserID) Then
MsgBox "You must enter UserID", vbCritical, gstrAppTitle
Me.txtUserID.SetFocus
ElseIf IsNull(DLookup("[Password]", "[tblUsers]", "[UserID] = [txtUserID]")) Then
MsgBox "UserID You entered is incorrect.", vbCritical, gstrAppTitle
Me.txtUserID = ""
Me.txtPassword = ""
Me.txtUserID.SetFocus
Else
strThisUserPassword = DLookup("[Password]", "[tblUsers]", "[UserID] = [txtUserID]")
If Me.txtPassword = strThisUserPassword Or strThisUserPassword = "" Then
gstrThisUser = DLookup("[UserName]", "[tblUsers]", "[UserID] = [txtUserID]")
gstrThisUserRole = DLookup("[Role]", "[tblUsers]", "[UserID] = [txtUserID]")
DoCmd.Close acForm, Me.Name
MsgBox "Hello " & gstrThisUser & vbNewLine & "(" & gstrThisUserRole & ")", _
vbInformation, gstrAppTitle
Else
intPswdCount = intPswdCount + 1
MsgBox "Password you entered is incorrect.", vbCritical, gstrAppTitle
If intPswdCount < 3 Then
Me.txtPassword = ""
Me.txtPassword.SetFocus
Else
DoCmd.Close acForm, Me.Name, acSaveNo
End If
End If
End If
End Sub
Option Compare Database
Public Const gstrAppTitle As String = "Helpdesk Management System"
Public gstrThisUser As String
Public gstrThisUserRole As String
อันนี้ใน frmLogin ค่ะ
Private Sub cmdLogin_Click()
Static intPswdCount As Integer
Dim strThisUserPassword As String
If IsNull(Me.txtUserID) Then
MsgBox "You must enter UserID", vbCritical, gstrAppTitle
Me.txtUserID.SetFocus
ElseIf IsNull(DLookup("[Password]", "[tblUsers]", "[UserID] = [txtUserID]")) Then
MsgBox "UserID You entered is incorrect.", vbCritical, gstrAppTitle
Me.txtUserID = ""
Me.txtPassword = ""
Me.txtUserID.SetFocus
Else
strThisUserPassword = DLookup("[Password]", "[tblUsers]", "[UserID] = [txtUserID]")
If Me.txtPassword = strThisUserPassword Or strThisUserPassword = "" Then
gstrThisUser = DLookup("[UserName]", "[tblUsers]", "[UserID] = [txtUserID]")
gstrThisUserRole = DLookup("[Role]", "[tblUsers]", "[UserID] = [txtUserID]")
DoCmd.Close acForm, Me.Name
MsgBox "Hello " & gstrThisUser & vbNewLine & "(" & gstrThisUserRole & ")", _
vbInformation, gstrAppTitle
Else
intPswdCount = intPswdCount + 1
MsgBox "Password you entered is incorrect.", vbCritical, gstrAppTitle
If intPswdCount < 3 Then
Me.txtPassword = ""
Me.txtPassword.SetFocus
Else
DoCmd.Close acForm, Me.Name, acSaveNo
End If
End If
End If
End Sub
4 @R16099
Option Compare Database
Option Explicit
Private Sub cmdCancel_Click()
DoCmd.Quit acQuitSaveNone
End Sub
Private Sub cmdLogin_Click()
Static intPswdCount As Integer
Dim strThisUserPassword As String
If IsNull(Me.txtUserID) Then
MsgBox "You must enter UserID", vbCritical, gstrAppTitle
Me.txtUserID.SetFocus
ElseIf IsNull(DLookup("[Password]", "[tblUsers]", "[UserID] = [txtUserID]")) Then
MsgBox "UserID You entered is incorrect.", vbCritical, gstrAppTitle
Me.txtUserID = ""
Me.txtPassword = ""
Me.txtUserID.SetFocus
Else
strThisUserPassword = DLookup("[Password]", "[tblUsers]", "[UserID] = [txtUserID]")
If Me.txtPassword = strThisUserPassword Or strThisUserPassword = "" Then
gstrThisUser = DLookup("[UserName]", "[tblUsers]", "[UserID] = [txtUserID]")
gstrThisUserRole = DLookup("[Role]", "[tblUsers]", "[UserID] = [txtUserID]")
DoCmd.Close acForm, Me.Name
MsgBox "Hello " & gstrThisUser & vbNewLine & "(" & gstrThisUserRole & ")", _
vbInformation, gstrAppTitle
Else
intPswdCount = intPswdCount + 1
MsgBox "Password you entered is incorrect.", vbCritical, gstrAppTitle
If intPswdCount < 3 Then
Me.txtPassword = ""
Me.txtPassword.SetFocus
Else
DoCmd.Close acForm, Me.Name, acSaveNo
End If
End If
End If
End Sub
Option Compare Database
Option Explicit
Private Sub cmdCancel_Click()
DoCmd.Quit acQuitSaveNone
End Sub
Private Sub cmdLogin_Click()
Static intPswdCount As Integer
Dim strThisUserPassword As String
If IsNull(Me.txtUserID) Then
MsgBox "You must enter UserID", vbCritical, gstrAppTitle
Me.txtUserID.SetFocus
ElseIf IsNull(DLookup("[Password]", "[tblUsers]", "[UserID] = [txtUserID]")) Then
MsgBox "UserID You entered is incorrect.", vbCritical, gstrAppTitle
Me.txtUserID = ""
Me.txtPassword = ""
Me.txtUserID.SetFocus
Else
strThisUserPassword = DLookup("[Password]", "[tblUsers]", "[UserID] = [txtUserID]")
If Me.txtPassword = strThisUserPassword Or strThisUserPassword = "" Then
gstrThisUser = DLookup("[UserName]", "[tblUsers]", "[UserID] = [txtUserID]")
gstrThisUserRole = DLookup("[Role]", "[tblUsers]", "[UserID] = [txtUserID]")
DoCmd.Close acForm, Me.Name
MsgBox "Hello " & gstrThisUser & vbNewLine & "(" & gstrThisUserRole & ")", _
vbInformation, gstrAppTitle
Else
intPswdCount = intPswdCount + 1
MsgBox "Password you entered is incorrect.", vbCritical, gstrAppTitle
If intPswdCount < 3 Then
Me.txtPassword = ""
Me.txtPassword.SetFocus
Else
DoCmd.Close acForm, Me.Name, acSaveNo
End If
End If
End If
End Sub
5 @R16100
โค๊ดถูกต้องแล้วนะครับ เอางี้ครับ ผมยกตัวอย่างให้ดูนะครับ จากโค๊ดคุณ
1. ผมต้องมีตารางชื่อ tblUsers และในตารางก็จะต้องมีฟิลด์ชื่อ
UserID : Number
UserName : Text
Password : Text
Role : Text
2. ลองทดสอบใส่ข้อมูลลงในตารางดังนี้:
UserID Password Role UserName
111 1234 TEST Viewa
112 4567 TEST2 TTT
3. ผมสร้าง Module ชื่อ modGlobals และใส่โค๊ดดังนี้:
Public Const gstrAppTitle As String = "Helpdesk Management System"
Public gstrThisUser As String
Public gstrThisUserRole As String
4. ผมสร้างฟอร์มสำหรับให้ผู้ใช้ใส่ UserID แบะ Password โดยฟอร์มผมต้องสร้าง Text Box 2 ตัว โดยมีชื่อดังนี้:
Text Box ตัวที่ 1 ชื่อ: txtUserID
Text Box ตัวที่ 2 ชื่อ: txtPassword
5. ผมต้องสร้างปุ่ม Command Button ขึ้นมาสำหรับกด Login ที่คุณใช้ชื่อว่า cmdLogin โดยใน Event > On Click ใส่โค๊ดดังนี้: (เหมือนที่คุณโพสมาแหละครับ)
'Private Sub cmdLogin_Click()
Static intPswdCount As Integer
Dim strThisUserPassword As String
If IsNull(Me.txtUserID) Then
MsgBox "You must enter UserID", vbCritical, gstrAppTitle
Me.txtUserID.SetFocus
ElseIf IsNull(DLookup("[Password]", "[tblUsers]", "[UserID] = [txtUserID]")) Then
MsgBox "UserID You entered is incorrect.", vbCritical, gstrAppTitle
Me.txtUserID = ""
Me.txtPassword = ""
Me.txtUserID.SetFocus
Else
strThisUserPassword = DLookup("[Password]", "[tblUsers]", "[UserID] = [txtUserID]")
If Me.txtPassword = strThisUserPassword Or strThisUserPassword = "" Then
gstrThisUser = DLookup("[UserName]", "[tblUsers]", "[UserID] = [txtUserID]")
gstrThisUserRole = DLookup("[Role]", "[tblUsers]", "[UserID] = [txtUserID]")
DoCmd.Close acForm, Me.Name
MsgBox "Hello " & gstrThisUser & vbNewLine & "(" & gstrThisUserRole & ")", _
vbInformation, gstrAppTitle
Else
intPswdCount = intPswdCount + 1
MsgBox "Password you entered is incorrect.", vbCritical, gstrAppTitle
If intPswdCount < 3 Then
Me.txtPassword = ""
Me.txtPassword.SetFocus
Else
DoCmd.Close acForm, Me.Name, acSaveNo
End If
End If
End If
'End Sub
- จากนั้น Run ฟอร์มดู
- หากคุณใส่รหัสที่ช่อง txtUserID เป็น 111 หรือ 112
- และใส่รหัสที่ช่อง txtPassword เป็น 1234 หรือ 4567
- โปรแกรมก็จะปิดฟอร์ม แล้วขึ้น MsgBox ข้อความว่า
Hello Viewa
(TEST)
หรือ
Hello TTT
(TEST2)
- แต่หากใส่ผิดไม่ตรงกับค่าที่เขียนในตาราง 3 ครั้งก็จะปิดฟอร์มอัตโนมัติ
- ตรงที่ผมทำอักษรสีแดงตรงโค๊ดนี่แหละครับ สำคัญคือ หากคุณต้องการให้มันเปลี่ยนจากการแสดง Msgbox คุณก็ต้องเขียนเป็นคำสั่งอย่างอื่น ซึ่งผมว่าหนังสือคงบอกต่อไปว่าจะต้องทำอย่างไร เช่นหากคุณจะให้เปิดฟอร์มชื่อ Form1 คุณก็อาจเขียนเป็น
DoCmd.OpenForm "Form1"
เป็นต้น
1. ผมต้องมีตารางชื่อ tblUsers และในตารางก็จะต้องมีฟิลด์ชื่อ
UserID : Number
UserName : Text
Password : Text
Role : Text
2. ลองทดสอบใส่ข้อมูลลงในตารางดังนี้:
UserID Password Role UserName
111 1234 TEST Viewa
112 4567 TEST2 TTT
3. ผมสร้าง Module ชื่อ modGlobals และใส่โค๊ดดังนี้:
Public Const gstrAppTitle As String = "Helpdesk Management System"
Public gstrThisUser As String
Public gstrThisUserRole As String
4. ผมสร้างฟอร์มสำหรับให้ผู้ใช้ใส่ UserID แบะ Password โดยฟอร์มผมต้องสร้าง Text Box 2 ตัว โดยมีชื่อดังนี้:
Text Box ตัวที่ 1 ชื่อ: txtUserID
Text Box ตัวที่ 2 ชื่อ: txtPassword
5. ผมต้องสร้างปุ่ม Command Button ขึ้นมาสำหรับกด Login ที่คุณใช้ชื่อว่า cmdLogin โดยใน Event > On Click ใส่โค๊ดดังนี้: (เหมือนที่คุณโพสมาแหละครับ)
'Private Sub cmdLogin_Click()
Static intPswdCount As Integer
Dim strThisUserPassword As String
If IsNull(Me.txtUserID) Then
MsgBox "You must enter UserID", vbCritical, gstrAppTitle
Me.txtUserID.SetFocus
ElseIf IsNull(DLookup("[Password]", "[tblUsers]", "[UserID] = [txtUserID]")) Then
MsgBox "UserID You entered is incorrect.", vbCritical, gstrAppTitle
Me.txtUserID = ""
Me.txtPassword = ""
Me.txtUserID.SetFocus
Else
strThisUserPassword = DLookup("[Password]", "[tblUsers]", "[UserID] = [txtUserID]")
If Me.txtPassword = strThisUserPassword Or strThisUserPassword = "" Then
gstrThisUser = DLookup("[UserName]", "[tblUsers]", "[UserID] = [txtUserID]")
gstrThisUserRole = DLookup("[Role]", "[tblUsers]", "[UserID] = [txtUserID]")
DoCmd.Close acForm, Me.Name
MsgBox "Hello " & gstrThisUser & vbNewLine & "(" & gstrThisUserRole & ")", _
vbInformation, gstrAppTitle
Else
intPswdCount = intPswdCount + 1
MsgBox "Password you entered is incorrect.", vbCritical, gstrAppTitle
If intPswdCount < 3 Then
Me.txtPassword = ""
Me.txtPassword.SetFocus
Else
DoCmd.Close acForm, Me.Name, acSaveNo
End If
End If
End If
'End Sub
- จากนั้น Run ฟอร์มดู
- หากคุณใส่รหัสที่ช่อง txtUserID เป็น 111 หรือ 112
- และใส่รหัสที่ช่อง txtPassword เป็น 1234 หรือ 4567
- โปรแกรมก็จะปิดฟอร์ม แล้วขึ้น MsgBox ข้อความว่า
Hello Viewa
(TEST)
หรือ
Hello TTT
(TEST2)
- แต่หากใส่ผิดไม่ตรงกับค่าที่เขียนในตาราง 3 ครั้งก็จะปิดฟอร์มอัตโนมัติ
- ตรงที่ผมทำอักษรสีแดงตรงโค๊ดนี่แหละครับ สำคัญคือ หากคุณต้องการให้มันเปลี่ยนจากการแสดง Msgbox คุณก็ต้องเขียนเป็นคำสั่งอย่างอื่น ซึ่งผมว่าหนังสือคงบอกต่อไปว่าจะต้องทำอย่างไร เช่นหากคุณจะให้เปิดฟอร์มชื่อ Form1 คุณก็อาจเขียนเป็น
DoCmd.OpenForm "Form1"
เป็นต้น
6 @R16103
ไม่ได้ค่ะ เป็นแบบเดิม
ลองเปิด access ทำฐานข้อมูลใหม่ ก็คลิกไม่ไปเช่นเดียวกัน
ลองเปิด access ทำฐานข้อมูลใหม่ ก็คลิกไม่ไปเช่นเดียวกัน
7 @R16104
รบกวนขอ email ได้มั๊ยคะ? จะส่งไฟล์ไปให้ช่วยดูให้หน่อยค่ะ
ว่าผิดตรงส่วนไหนบ้าง และที่ใส่โค้ดไปแล้วคลิกไม่ไป
เกิดจากอะไร
คือตอนนี้หนูกำลังทำโปรแกรมส่งอาจารย์ค่ะ
ซึ่งโปรแกรมนี้ก็เป็นส่วนหนึ่งของบัณฑิตนิพนธ์
กำหนดส่งสิ้นเดือนนี้ หนูไม่แน่ใจว่าจะทำทันมั๊ย
ติดที่ Login นี่แหละค่ะ
ว่าผิดตรงส่วนไหนบ้าง และที่ใส่โค้ดไปแล้วคลิกไม่ไป
เกิดจากอะไร
คือตอนนี้หนูกำลังทำโปรแกรมส่งอาจารย์ค่ะ
ซึ่งโปรแกรมนี้ก็เป็นส่วนหนึ่งของบัณฑิตนิพนธ์
กำหนดส่งสิ้นเดือนนี้ หนูไม่แน่ใจว่าจะทำทันมั๊ย
ติดที่ Login นี่แหละค่ะ
8 @R16106
ใน MS Access คุณได้กำหนดแบบที่ คุณ Su แนะนำหรือยังครับ
9 @R16108
ตั้งค่าเรียบร้อยแล้วค่ะ
10 @R16109
ทำให้ผมก็อยากรู้เหมือนกันว่าอะไร นึกไม่ออก
ส่งมาที่นี่ครับ
api@live.com
ส่งมาที่นี่ครับ
api@live.com
11 @R16111
ลองตั้งค่า Securities ให้เป็น medium ด้วยครับ
12 @R16119
รันได้แล้วค่ะ ขอบคุณ คุณ TTT ,Su และคุณ Akung มากนะคะ
13 @R16120
ตกลงสรุปว่าเป็นไรครับ บอกให้หายสงสัยหน่อยครับ
14 @R16131
หนูก็ไม่ทราบเหมือนกันค่ะ พอเปิดเครื่องมา เปิดโปรแกรมแล้วก็รันได้เลยค่ะ
น่าจะเป็นที่ ความน่าเชื่อถือค่ะ เพราะวันนั้น ทำแล้ว ไม่ได้รีสาร์ทเครื่อง
น่าจะเป็นที่ ความน่าเชื่อถือค่ะ เพราะวันนั้น ทำแล้ว ไม่ได้รีสาร์ทเครื่อง
Time: 0.2165s
http://www.mediafire.com/view/?zc2cliy9bsxe2k8