Sub InsertDataToSQLServer()
    Dim conn As Object
    Dim cmd As Object
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
    Dim labItemCode As String
    Dim testType As String
    Dim resultType As String
    
    ' ตั้งค่าเชื่อมต่อกับ SQL Server
    Set conn = CreateObject("ADODB.Connection")
    conn.Open "Provider=SQLOLEDB;Data Source=YOUR_SERVER_NAME;Initial Catalog=lab_db_GTR;User ID=YOUR_USERNAME;Password=YOUR_PASSWORD;"
    
    ' ตั้งค่า Worksheet และหาจำนวนแถวสุดท้าย
    Set ws = ThisWorkbook.Sheets("Sheet1") ' เปลี่ยนชื่อ Sheet ตามที่ต้องการ
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
    ' Loop ผ่านแต่ละแถวใน Excel และ Insert ข้อมูลเข้า SQL Server
    For i = 2 To lastRow ' เริ่มที่แถวที่ 2 เพื่อข้ามหัวตาราง
        labItemCode = ws.Cells(i, 1).Value ' สมมติว่า lab_items_code อยู่ในคอลัมน์ A
        testType = ws.Cells(i, 2).Value ' สมมติว่า test_type อยู่ในคอลัมน์ B
        resultType = ws.Cells(i, 3).Value ' สมมติว่า result_type อยู่ในคอลัมน์ C
        
        ' สร้างคำสั่ง SQL สำหรับ Insert ข้อมูล
        Set cmd = CreateObject("ADODB.Command")
        cmd.ActiveConnection = conn
        cmd.CommandText = "INSERT INTO lab_setup_test (lab_items_code, test_type, result_type) VALUES ('" & labItemCode & "', '" & testType & "', '" & resultType & "')"
        cmd.Execute
    Next i
    
    ' ปิดการเชื่อมต่อ
    conn.Close
    Set conn = Nothing
    Set cmd = Nothing
    
    MsgBox "Data inserted successfully!"
End Sub

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: