EXCEL TOOLS
Excel Add-ins ที่พัฒนาโดยคุณสันติพงศ์ ณสุย (MVP Excel 2010-2020) ด้วยภาษา C# เพื่อแก้ไขปัญหาไฟล์ใหญ่ คำนวณนาน ทำงานช้า จัดการข้อมูลต่าง ๆ ที่ทำงานประจำวันได้อย่างสะดวกรวดเร็ว สนใจคลิกไปดูได้ที่นี่ครับ => Excel Tools
Excel Add-ins ที่พัฒนาโดยคุณสันติพงศ์ ณสุย (MVP Excel 2010-2020) ด้วยภาษา C# เพื่อแก้ไขปัญหาไฟล์ใหญ่ คำนวณนาน ทำงานช้า จัดการข้อมูลต่าง ๆ ที่ทำงานประจำวันได้อย่างสะดวกรวดเร็ว สนใจคลิกไปดูได้ที่นี่ครับ => Excel Tools
[code]
และปิดด้วย [/code]
ตัวอย่างเช่น [code]dim r as range[/code]
เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่างเพิ่มเติม)Code: Select all
Private Sub Command14_Click()
Dim fd As Object ' Late-bound FileDialog object
Dim strFilePath As String
Dim cnn As Object ' Late-bound ADODB connection object
Dim rs As Object ' Late-bound ADODB recordset object
Dim strSql As String
' Create a FileDialog object
Set fd = Application.FileDialog(3) ' msoFileDialogFilePicker
' Set the title and filters for the dialog box
With fd
.Title = "Select Excel File"
.Filters.Clear
.Filters.Add "Excel Files", "*.xlsx;*.xls"
End With
' Show the dialog box and get the file path
If fd.Show = -1 Then
strFilePath = fd.SelectedItems(1)
' Open the Excel file using an ADODB connection
Set cnn = CreateObject("ADODB.Connection")
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFilePath & ";Extended Properties=""Excel 12.0 Xml;HDR=YES;"""
' Read the data from the Excel file into a recordset object
Set rs = CreateObject("ADODB.Recordset")
rs.Open "SELECT * FROM [Sheet1$]", cnn, adOpenStatic, adLockOptimistic
' Iterate through the recordset object and insert or update the data in the Access table as necessary.
Do While Not rs.EOF
strSql = "SELECT * FROM Tb_Employee WHERE [Employee ID]='" & rs("Employee ID").Value & "'"
If DCount("*", "Tb_Employee", strSql) > 0 Then
' Record already exists in Access, update the record
strSql = "UPDATE Tb_Employee SET [Pre_Name_TH]='" & rs("Pre_Name_TH").Value & "', [Firstname_TH]='" & rs("Firstname_TH").Value & "', [Lastname_TH]='" & rs("Lastname_TH").Value & "', [Pre_Name_EN]='" & rs("Pre_Name_EN").Value & "', [Firstname_EN]='" & rs("Firstname_EN").Value & "', [Lastname_EN]='" & rs("Lastname_EN").Value & "', [Date of Birth]=" & rs("Date of Birth").Value & ", [Gender]='" & rs("Gender").Value & "', [Nationality]='" & rs("Nationality").Value & "', [Contact Number]='" & rs("Contact Number").Value & "', [Department]='" & rs("Department").Value & "', [Position]='" & rs("Position").Value & "', [Address]='" & rs("Address").Value & "', [ZIP Code]='" & rs("ZIP Code").Value & "', [ID_card]='" & rs("ID_card").Value & "', [Image]=rs('Image').Value WHERE [Employee ID]='" & rs("Employee ID").Value & "'"
CurrentDb.Execute strSql
Else
' Record does not exist in Access, insert a new record
strSql = "INSERT INTO Tb_Employee ([Employee ID], [Pre_Name_TH], [Firstname_TH], [Lastname_TH], [Pre_Name_EN], [Firstname_EN], [Lastname_EN], [Date of Birth], [Gender], [Nationality], [Contact Number], [Department], [Position], [Address], [ZIP Code], [ID_card], [Image]) VALUES ('" & rs("Employee ID").Value & "', '" & rs("Pre_Name_TH").Value & "', '" & rs("Firstname_TH").Value & "', '" & rs("Lastname_TH").Value & "', '" & rs("Pre_Name_EN").Value & "', '" & rs("Firstname_EN").Value & "', '" & rs("Lastname_EN").Value & "', #" & Format(rs("Date of Birth").Value, "yyyy\/mm\/dd") & "#, '" & rs("Gender").Value & "', '" & rs("Nationality").Value & "', '" & rs("Contact Number").Value & "', '" & rs("Department").Value & "', '" & rs("Position").Value & "', '" & rs("Address").Value & "', '" & rs("ZIP Code").Value & "', '" & rs("ID_card").Value & "', rs('Image').Value)"
CurrentDb.Execute strSql
End If
End Sub
Do
ใส่ Loop
ไว้ครบถ้วนแล้วหรือไม่ครับCode: Select all
rs.Open "SELECT * FROM [Sheet1$]", cnn, adOpenStatic, adLockOptimistic
ขอเพิ่มเติมรูปภาพครับ +ไฟล์ที่ import ครับ
ไฟล์ที่ปรับปรุงแล้วครับsnasui wrote: Mon Apr 03, 2023 5:09 pm แนบไฟล์ Access ล่าสุดที่ได้ปรับ Code มาแล้วด้วยจะได้ตอบต่อไปจากนั้นครับ
rs.Open "SELECT * FROM [Sheet1$]", cnn , adOpenStatic, adLockOptimistic
ปรับเปํน rs.Open "SELECT * FROM [Sheet1$]", cnn
Code: Select all
Do While Not rs.EOF
strSql = "SELECT * FROM Tb_Employee WHERE [Employee ID]='" & rs("Employee ID").Value & "'"
If DCount("*", "Tb_Employee", strSql) > 0 Then
' Record already exists in Access, update the record
strSql = "UPDATE Tb_Employee SET [Pre_Name_TH]='" & rs("Pre_Name_TH").Value & "', [Firstname_TH]='" & rs("Firstname_TH").Value & "', [Lastname_TH]='" & rs("Lastname_TH").Value & "', [Pre_Name_EN]='" & rs("Pre_Name_EN").Value & "', [Firstname_EN]='" & rs("Firstname_EN").Value & "', [Lastname_EN]='" & rs("Lastname_EN").Value & "', [Date of Birth]=" & rs("Date of Birth").Value & ", [Gender]='" & rs("Gender").Value & "', [Nationality]='" & rs("Nationality").Value & "', [Contact Number]='" & rs("Contact Number").Value & "', [Department]='" & rs("Department").Value & "', [Position]='" & rs("Position").Value & "', [Address]='" & rs("Address").Value & "', [ZIP Code]='" & rs("ZIP Code").Value & "', [ID_card]='" & rs("ID_card").Value & "', [Image]=rs('Image').Value WHERE [Employee ID]='" & rs("Employee ID").Value & "'"
CurrentDb.Execute strSql
Else
' Record does not exist in Access, insert a new record
strSql = "INSERT INTO Tb_Employee ([Employee ID], [Pre_Name_TH], [Firstname_TH], [Lastname_TH], [Pre_Name_EN], [Firstname_EN], [Lastname_EN], [Date of Birth], [Gender], [Nationality], [Contact Number], [Department], [Position], [Address], [ZIP Code], [ID_card], [Image]) VALUES ('" & rs("Employee ID").Value & "', '" & rs("Pre_Name_TH").Value & "', '" & rs("Firstname_TH").Value & "', '" & rs("Lastname_TH").Value & "', '" & rs("Pre_Name_EN").Value & "', '" & rs("Firstname_EN").Value & "', '" & rs("Lastname_EN").Value & "', #" & Format(rs("Date of Birth").Value, "yyyy\/mm\/dd") & "#, '" & rs("Gender").Value & "', '" & rs("Nationality").Value & "', '" & rs("Contact Number").Value & "', '" & rs("Department").Value & "', '" & rs("Position").Value & "', '" & rs("Address").Value & "', '" & rs("ZIP Code").Value & "', '" & rs("ID_card").Value & "', rs('Image').Value)"
CurrentDb.Execute strSql
End If
Loop
ผมลอง Import ไฟล์ Excel 123 เข้าไปใหม่อีกรอบนึง pop up เด้งขึ้นมาตามรูปภาพเลยครับ พอกด debug เด้งไปที่ตรงเส้นสีเหลืองครับsnasui wrote: Mon Apr 03, 2023 4:56 pm บรรทัดที่ Highlight กับ Popup Error ไม่สอดคล้องกัน กรุณาตรวจสอบใหม่ว่า Error ที่บรรทัดไหนครับ
Code: Select all
Do While Not rs.EOF
strSql = "SELECT * FROM Tb_Employee WHERE [Employee ID]='" & rs("Employee ID").Value & "'"
If DCount("*", "Tb_Employee", strSq) > 0 Then
' Record already exists in Access, update the record
strSql = "UPDATE Tb_Employee SET [Pre_Name_TH]='" & rs("Pre_Name_TH").Value & "', [Firstname_TH]='" & rs("Firstname_TH").Value & "', [Lastname_TH]='" & rs("Lastname_TH").Value & "', [Pre_Name_EN]='" & rs("Pre_Name_EN").Value & "', [Firstname_EN]='" & rs("Firstname_EN").Value & "', [Lastname_EN]='" & rs("Lastname_EN").Value & "', [Date of Birth]=" & rs("Date of Birth").Value & ", [Gender]='" & rs("Gender").Value & "', [Nationality]='" & rs("Nationality").Value & "', [Contact Number]='" & rs("Contact Number").Value & "', [Department]='" & rs("Department").Value & "', [Position]='" & rs("Position").Value & "', [Address]='" & rs("Address").Value & "', [ZIP Code]='" & rs("ZIP Code").Value & "', [ID_card]='" & rs("ID_card").Value & "', [Image]=rs('Image').Value WHERE [Employee ID]='" & rs("Employee ID").Value & "'"
CurrentDb.Execute strSql
Else
' Record does not exist in Access, insert a new record
strSql = "INSERT INTO Tb_Employee ([Employee ID], [Pre_Name_TH], [Firstname_TH], [Lastname_TH], [Pre_Name_EN], [Firstname_EN], [Lastname_EN], [Date of Birth], [Gender], [Nationality], [Contact Number], [Department], [Position], [Address], [ZIP Code], [ID_card], [Image]) VALUES ('" & rs("Employee ID").Value & "', '" & rs("Pre_Name_TH").Value & "', '" & rs("Firstname_TH").Value & "', '" & rs("Lastname_TH").Value & "', '" & rs("Pre_Name_EN").Value & "', '" & rs("Firstname_EN").Value & "', '" & rs("Lastname_EN").Value & "', #" & Format(rs("Date of Birth").Value, "yyyy\/mm\/dd") & "#, '" & rs("Gender").Value & "', '" & rs("Nationality").Value & "', '" & rs("Contact Number").Value & "', '" & rs("Department").Value & "', '" & rs("Position").Value & "', '" & rs("Address").Value & "', '" & rs("ZIP Code").Value & "', '" & rs("ID_card").Value & "', rs('Image').Value)"
CurrentDb.Execute strSql
End If
Loop
Code: Select all
strSql = "UPDATE Tb_Employee SET [Pre_Name_TH]='" & rs("Pre_Name_TH").Value & "', [Firstname_TH]='" & rs("Firstname_TH").Value & "', [Lastname_TH]='" & rs("Lastname_TH").Value & "', [Pre_Name_EN]='" & rs("Pre_Name_EN").Value & "', [Firstname_EN]='" & rs("Firstname_EN").Value & "', [Lastname_EN]='" & rs("Lastname_EN").Value & "', [Date of Birth]=" & rs("Date of Birth").Value & ", [Gender]='" & rs("Gender").Value & "', [Nationality]='" & rs("Nationality").Value & "', [Contact Number]='" & rs("Contact Number").Value & "', [Department]='" & rs("Department").Value & "', [Position]='" & rs("Position").Value & "', [Address]='" & rs("Address").Value & "', [ZIP Code]='" & rs("ZIP Code").Value & "', [ID_card]='" & rs("ID_card").Value & "', [Image]=rs('Image').Value WHERE [Employee ID]='" & rs("Employee ID").Value & "'"
rs("Pre_Name_TH").Value
ให้ค่าเป็นอะไรrs("Firstname_TH").Value
ให้ค่าเป็นอะไร? strSql
Code: Select all
strSql = "UPDATE Tb_Employee SET [Pre_Name_TH]='" & rs("Pre_Name_TH").Value & "', [Firstname_TH]='" & rs("Firstname_TH").Value & "', [Lastname_TH]='" & rs("Lastname_TH").Value & "', [Pre_Name_EN]='" & rs("Pre_Name_EN").Value & "', [Firstname_EN]='" & rs("Firstname_EN").Value & "', [Lastname_EN]='" & rs("Lastname_EN").Value & "', [Date of Birth]=" & rs("Date of Birth").Value & ", [Gender]='" & rs("Gender").Value & "', [Nationality]='" & rs("Nationality").Value & "', [Contact Number]='" & rs("Contact Number").Value & "', [Department]='" & rs("Department").Value & "', [Position]='" & rs("Position").Value & "', [Address]='" & rs("Address").Value & "', [ZIP Code]='" & rs("ZIP Code").Value & "', [ID_card]='" & rs("ID_card").Value & "', [Image]= '" & rs("Image").Value & "' WHERE [Employee ID]='" & rs("Employee ID").Value & "'"
CurrentDb.Execute strSql
strSql
ได้เป็นค่าใดครับ '
ในการรัน Query หากไม่ผ่านอาจทดสอบด้วยการครอบด้วย '
ดูอีกรอบว่าผ่านหรือไม่ลองแก้วันที่แล้วครับ Error ตลอดครับ แต่ถ้านำ CurrentDb.Execute strSql ออกจะเด้ง pop-up ดังรูปครับ ส่วนโค็ดที่ทดสอบคือsnasui wrote: Tue Apr 04, 2023 4:41 pm ดูแล้วสามารถรันได้ ลองทดสอบเอา Statement นี้ไปรันใน Query ของ Access (ซึ่งจะต้องเข้ามุมมอง SQL) เพื่อดูว่าได้ผลเป็นเช่นไรครับ
หากรันไม่ผ่านแสดงว่า Statement นี้มีปัญหา ให้แจ้งปัญหานั้นมาอีกรอบครับ
้สังเกตว่าวันที่ไม่ได้ครอบด้วย'
ในการรัน Query หากไม่ผ่านอาจทดสอบด้วยการครอบด้วย'
ดูอีกรอบว่าผ่านหรือไม่
Code: Select all
UPDATE Tb_Employee SET [Pre_Name_TH]='" & rs("Pre_Name_TH").Value & "', [Firstname_TH]='" & rs("Firstname_TH").Value & "', [Lastname_TH]='" & rs("Lastname_TH").Value & "', [Pre_Name_EN]='" & rs("Pre_Name_EN").Value & "', [Firstname_EN]='" & rs("Firstname_EN").Value & "', [Lastname_EN]='" & rs("Lastname_EN").Value & "', [Date of Birth]='" & rs("Date of Birth").Value & "', [Gender]='" & rs("Gender").Value & "', [Nationality]='" & rs("Nationality").Value & "', [Contact Number]='" & rs("Contact Number").Value & "', [Department]='" & rs("Department").Value & "', [Position]='" & rs("Position").Value & "', [Address]='" & rs("Address").Value & "', [ZIP Code]='" & rs("ZIP Code").Value & "', [ID_card]='" & rs("ID_card").Value & "', [Image]= '" & rs("Image").Value & "' WHERE [Employee ID]='" & rs("Employee ID").Value & "'
strSql
ไปทดสอบ ไม่ใช่นำ Statement ของ VBA ไปใช้โดยตรงครับ