:D snasui.com ยินดีต้อนรับ :D
ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย :thup: สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ :arrow: ระบุ Version ของ Excel
:!: โปรดทราบ :!:
  1. กรุณาอ่านกฎการใช้บอร์ด (Forum rules) ในตำแหน่งด้านบนของแต่ละบอร์ด
  2. การสมัครสมาชิก การ Login การกู้คืนรหัสผ่าน
    1. สมัครสมาชิกดูขั้นตอนได้ที่ :arrow: สมัครสมาชิก
    2. Login เข้าระบบโดยคลิกปุ่ม Login ตรงมุมขวาบนของหน้านี้ :roll:
    3. การ Login ผ่าน Facebook ดูวิธีที่ :arrow: Login ผ่าน Facebook
    4. ลืมรหัสผ่านสามารถรับรหัสใหม่ได้ที่ :arrow: Reset รหัสผ่าน
  3. มีปัญหาการใช้งาน แจ้งผู้ดูแลระบบได้ที่ :arrow: ติดต่อผู้ดูแลระบบ
  4. กำหนดการตั้งค่าส่วนตัว เช่นตั้งค่าภาษาเป็นไทยหรืออังกฤษดูได้ที่ :arrow: ตั้งค่าส่วนตัว
  5. การตั้งและตอบกระทู้ดูได้ที่ :arrow: วิธีการตั้งและตอบกระทู้
  6. การจัดรูปแบบตัวอักษรด้วย bbcode ในช่องแสดงความคิดเห็นดูได้ที่ :arrow: จัดรูปแบบตัวอักษร
  7. กำหนดขนาดตัวอักษรใน Browser ดูได้ที่ :arrow: กำหนดขนาดตัวอักษรใน Browser

Import ข้อมูลจาก Excel มา Access

ฟอรัมถาม-ตอบปัญหาการใช้งาน MS Access and VBA
Forum rules
  1. ไม่อนุญาตให้ใช้ภาษาแชทในการถามและตอบปัญหา ไม่ใช้คำว่า "คับ" หรือ "อ่ะครับ" แทนคำว่า "ครับ" ไม่ใช้คำว่า "เด๋ว" แทนคำว่า "เดี๋ยว" เป็นต้น เนื่องจากเมื่อแปลเป็นภาษาต่างประเทศแล้วจะให้ความหมายผิดไปจากที่ควรจะเป็น
  2. ห้ามถามโดยระบุชื่อผู้ตอบและต้องตั้งชื่อกระทู้ให้สื่อถึงปัญหาที่จะถาม ไม่ตั้งชื่อว่า ช่วยด้วยครับ, มีปัญหามาปรึกษาครับ เป็นต้น
  3. กรุณาอธิบายปัญหาและระบุคำตอบที่ต้องการมาในกระทู้ด้วยเสมอถึงแม้จะอธิบายไว้ในไฟล์แนบแล้วก็ตาม ทั้งนี้เพื่ออำนวยความสะดวกแก่เพื่อนสมาชิกในการค้นหาข้อมูล
  4. กรุณาแนบไฟล์ตัวอย่างพร้อมแสดงคำตอบที่ถูกต้องมาในไฟล์ด้วยเพื่อให้ง่ายต่อการทำความเข้าใจและสะดวกต่อการตอบคำถาม (ขนาดไฟล์ไม่เกิน 500Kb ขนาดภาพไม่เกิน 800*600 Pixel) ไม่แนบเป็น Link มาจากแหล่งอื่นที่อาจจะถูกลบทิ้งไปโดยต้นทางในภายหลัง นอกจากนี้ไม่ควรแนบไฟล์ที่มีข้อมูลสำคัญอันก่อให้เกิดความเสียหายกับตนเองและผู้อื่น
  5. กรณีเป็นคำถามเกี่ยวกับ Programming เช่น VBA, VB.Net, C#, SQL ฯลฯ ต้องลองเขียนมาเองก่อนเสมอ ถามเฉพาะที่ติดปัญหา ระบุ Module, Procedure ที่ติดปัญหาให้ชัดเจน กรุณาโพสต์ Code ให้แสดงเป็น Code คือเปิดด้วย [code] และปิดด้วย [/code] ตัวอย่างเช่น [code]dim r as range[/code] เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่างเพิ่มเติม)
  6. กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
Xcelvba
Silver
Silver
Posts: 550
Joined: Thu Feb 03, 2022 9:04 am
Excel Ver: 2019

Import ข้อมูลจาก Excel มา Access

#1

Post by Xcelvba »

Form ชื่อ F-Q-EMP ครับ ปุ่ม Command 14 ขึ้น Error ครับ

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
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 30746
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: Import ข้อมูลจาก Excel มา Access

#2

Post by snasui »

:D จาก Error กรุณาตรวจสอบว่าหลัง Do ใส่ Loop ไว้ครบถ้วนแล้วหรือไม่ครับ
Xcelvba
Silver
Silver
Posts: 550
Joined: Thu Feb 03, 2022 9:04 am
Excel Ver: 2019

Re: Import ข้อมูลจาก Excel มา Access

#3

Post by Xcelvba »

เจอแล้วครับพอ กด open file จาก browse แล้ว error บรรทัดนี้ครับ

Code: Select all

rs.Open "SELECT * FROM [Sheet1$]", cnn, adOpenStatic, adLockOptimistic
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 30746
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: Import ข้อมูลจาก Excel มา Access

#4

Post by snasui »

:D จับภาพ Popup ที่แสดง Error มาด้วยครับ
Xcelvba
Silver
Silver
Posts: 550
Joined: Thu Feb 03, 2022 9:04 am
Excel Ver: 2019

Re: Import ข้อมูลจาก Excel มา Access

#5

Post by Xcelvba »

snasui wrote: Mon Apr 03, 2023 4:56 pm :D จับภาพ Popup ที่แสดง Error มาด้วยครับ
ขอเพิ่มเติมรูปภาพครับ +ไฟล์ที่ import ครับ
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 30746
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: Import ข้อมูลจาก Excel มา Access

#6

Post by snasui »

:D แนบไฟล์ Access ล่าสุดที่ได้ปรับ Code มาแล้วด้วยจะได้ตอบต่อไปจากนั้นครับ
Xcelvba
Silver
Silver
Posts: 550
Joined: Thu Feb 03, 2022 9:04 am
Excel Ver: 2019

Re: Import ข้อมูลจาก Excel มา Access

#7

Post by Xcelvba »

snasui wrote: Mon Apr 03, 2023 5:09 pm :D แนบไฟล์ Access ล่าสุดที่ได้ปรับ Code มาแล้วด้วยจะได้ตอบต่อไปจากนั้นครับ
ไฟล์ที่ปรับปรุงแล้วครับ
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 30746
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: Import ข้อมูลจาก Excel มา Access

#8

Post by snasui »

:D ลองปรับ recordset โดยไม่ใช้ Parameter 2 ตัวท้ายครับ

จาก rs.Open "SELECT * FROM [Sheet1$]", cnn , adOpenStatic, adLockOptimistic ปรับเปํน rs.Open "SELECT * FROM [Sheet1$]", cnn

ส่วนของ Loop ใน Code ใส่ไว้ผิดตำแหน่ง กรุณาตรวจสอบและวางไว้ยังตำแหน่งที่ถูกต้องด้วยครับ
Xcelvba
Silver
Silver
Posts: 550
Joined: Thu Feb 03, 2022 9:04 am
Excel Ver: 2019

Re: Import ข้อมูลจาก Excel มา Access

#9

Post by Xcelvba »

เรียนอาจารย์ยัง 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", 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
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 30746
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: Import ข้อมูลจาก Excel มา Access

#10

Post by snasui »

:D บรรทัดที่ Highlight กับ Popup Error ไม่สอดคล้องกัน กรุณาตรวจสอบใหม่ว่า Error ที่บรรทัดไหนครับ
Xcelvba
Silver
Silver
Posts: 550
Joined: Thu Feb 03, 2022 9:04 am
Excel Ver: 2019

Re: Import ข้อมูลจาก Excel มา Access

#11

Post by Xcelvba »

snasui wrote: Mon Apr 03, 2023 4:56 pm :D บรรทัดที่ Highlight กับ Popup Error ไม่สอดคล้องกัน กรุณาตรวจสอบใหม่ว่า Error ที่บรรทัดไหนครับ
ผมลอง Import ไฟล์ Excel 123 เข้าไปใหม่อีกรอบนึง pop up เด้งขึ้นมาตามรูปภาพเลยครับ พอกด debug เด้งไปที่ตรงเส้นสีเหลืองครับ
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 30746
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: Import ข้อมูลจาก Excel มา Access

#12

Post by snasui »

:D หากเป็นเช่นนั้นให้ตรวจสอบ 2 อย่างครับ
  1. Statement ที่เกิดใน Popup ได้ผลลัพธ์เป็นอะไร ซึ่งจะต้องเข้าใจ Statement ของ SQL และสามารถนำไปทดสอบเองได้ใน Access หากย้งไม่ทราบวิธีการ ควรศึกษามาก่อน
  2. Function Dcount มีส่วนประกอบอะไรบ้าง แต่ละส่วนประกอบมีความหมายว่าอย่างไร ข้อนี้สัมพันธ์กับข้อ 1 เพราะเป็นการนำข้อ 1 มาใช้เป็นส่วนประกอบของ Dcount และทำให้เกิด Popup เช่นนั้น
Xcelvba
Silver
Silver
Posts: 550
Joined: Thu Feb 03, 2022 9:04 am
Excel Ver: 2019

Re: Import ข้อมูลจาก Excel มา Access

#13

Post by Xcelvba »

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
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 30746
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: Import ข้อมูลจาก Excel มา Access

#14

Post by snasui »

:D ทำการ Debug Statement นี้ครับ

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 & "'"
โดย Debug ไปทีละค่าว่าค่าที่นำมาเชื่อมกันนั้นให้ผลลัพธ์เป็นอะไรบ้าง ยกตัวอย่างเช่น
rs("Pre_Name_TH").Value ให้ค่าเป็นอะไร
rs("Firstname_TH").Value ให้ค่าเป็นอะไร
ฯลฯ

การใช้ VBA ต้อง Debug โดยวิธีการติดตามตัวแปรเองให้เป็นหรืออย่างน้อยสามารถคีย์สอบถามใน Immediate Windows ได้ครับ

วิธีการคร่าว ๆ
  1. เปิดหน้าต่าง Immediate Windows จากเมนู View
  2. คีย์เพื่อตรวจสอบผลลัพธ์ของสิ่งที่สนใจเช่น
    ? strSql
    Enter
  3. สังเกตผลลัพธ์ที่ได้
Xcelvba
Silver
Silver
Posts: 550
Joined: Thu Feb 03, 2022 9:04 am
Excel Ver: 2019

Re: Import ข้อมูลจาก Excel มา Access

#15

Post by Xcelvba »

ผมลอง Debug แล้วครับ ได้ค่าดังนี้
[Pre_Name_TH]=Null
[Firstname_TH]='·´Òͺºº'
[Lastname_TH]='Åͧ'
[Pre_Name_EN]=Null
[Firstname_EN]=Null
[Lastname_EN]=Null
[Date of Birth]=13/4/2566
[Gender]='ªÒÂ'
[Nationality]='ä·Â'
[Contact Number]='081-1111111'
[Department]='·´Êͺ¢éÍÁÙÅ'
[Position]=Null
[Address]=Null
[ZIP Code]='50210'
[ID_card]=''
[Image]=rs('Image').Value WHERE [Employee ID]='10000'

ผมเจอบรรทัดนี้ แปลก ผมเลยปรับโค้ด เป็น

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
แต่ก็ยัง ERROR และ pop up เช่นเดิมครับอาจารย์
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 30746
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: Import ข้อมูลจาก Excel มา Access

#16

Post by snasui »

:D หลังจากปรับ Code แล้ว ตัวแปร strSql ได้เป็นค่าใดครับ :?:
Xcelvba
Silver
Silver
Posts: 550
Joined: Thu Feb 03, 2022 9:04 am
Excel Ver: 2019

Re: Import ข้อมูลจาก Excel มา Access

#17

Post by Xcelvba »

UPDATE Tb_Employee SET
[Pre_Name_TH]='',
[Firstname_TH]='·´Òͺºº',
[Lastname_TH]='Åͧ',
[Pre_Name_EN]='',
[Firstname_EN]='',
[Lastname_EN]='',
[Date of Birth]=13/4/2566,
[Gender]='ªÒÂ',
[Nationality]='ä·Â',
[Contact Number]='081-1111111',
[Department]='·´Êͺ¢éÍÁÙÅ',
[Position]='',
[Address]='',
[ZIP Code]='50210',
[ID_card]='',
[Image]= ''
WHERE [Employee ID]='10000'

ได้เป็นค่านี้ครับ
User avatar
snasui
Site Admin
Site Admin
Posts: 30746
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: Import ข้อมูลจาก Excel มา Access

#18

Post by snasui »

:D ดูแล้วสามารถรันได้ ลองทดสอบเอา Statement นี้ไปรันใน Query ของ Access (ซึ่งจะต้องเข้ามุมมอง SQL) เพื่อดูว่าได้ผลเป็นเช่นไรครับ

หากรันไม่ผ่านแสดงว่า Statement นี้มีปัญหา ให้แจ้งปัญหานั้นมาอีกรอบครับ

้สังเกตว่าวันที่ไม่ได้ครอบด้วย ' ในการรัน Query หากไม่ผ่านอาจทดสอบด้วยการครอบด้วย ' ดูอีกรอบว่าผ่านหรือไม่
Xcelvba
Silver
Silver
Posts: 550
Joined: Thu Feb 03, 2022 9:04 am
Excel Ver: 2019

Re: Import ข้อมูลจาก Excel มา Access

#19

Post by Xcelvba »

snasui wrote: Tue Apr 04, 2023 4:41 pm :D ดูแล้วสามารถรันได้ ลองทดสอบเอา Statement นี้ไปรันใน Query ของ Access (ซึ่งจะต้องเข้ามุมมอง SQL) เพื่อดูว่าได้ผลเป็นเช่นไรครับ

หากรันไม่ผ่านแสดงว่า Statement นี้มีปัญหา ให้แจ้งปัญหานั้นมาอีกรอบครับ

้สังเกตว่าวันที่ไม่ได้ครอบด้วย ' ในการรัน Query หากไม่ผ่านอาจทดสอบด้วยการครอบด้วย ' ดูอีกรอบว่าผ่านหรือไม่
ลองแก้วันที่แล้วครับ Error ตลอดครับ แต่ถ้านำ CurrentDb.Execute strSql ออกจะเด้ง pop-up ดังรูปครับ ส่วนโค็ดที่ทดสอบคือ

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 & "'
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 30746
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: Import ข้อมูลจาก Excel มา Access

#20

Post by snasui »

:D ต้องเอา Statement ที่ได้จากตัวแปร strSql ไปทดสอบ ไม่ใช่นำ Statement ของ VBA ไปใช้โดยตรงครับ
Post Reply