จากกระทู้เก่าอ.Yeadram การส่งออกตาราง1ตารางไปเป็นชื่อตารางเดิมแต่นามสกุล xls


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

18 ก.พ. 63 , 16:17:06
อ่าน 1047 ครั้ง

SakDa

จากโค้ดนี้ เขาส่งตารางเป้าหมายออกเป็นไฟล์ mdb

Private Sub Command0_Click()
'************ ส่วนที่ 1 การเรียกหน้าต่าง Dialog เพื่อเลือกพาธ และตั้งชื่อเซฟไฟล์เป้าหมาย*****************
    Const msoFileDialogFilePicker = 3
    Const msoFileDialogViewDetails = 2
 
    Dim fd As Object
    Dim varItems As Variant
    Dim flPath As Variant
    Dim LFilename As String

    Set fd = Application.FileDialog(msoFileDialogViewDetails)
    With fd
        .Title = "Save As"
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewDetails
        .InitialFileName = "*.mdb"
    End With

    If fd.Show = True Then
        For Each varItems In fd.SelectedItems
            flPath = varItems
        Next
    End If
    LFilename = flPath
   
'*************ส่วนที่ 2 การสร้างไฟล์ MS Access ใหม่******************
    If LFilename & "" <> "" Then
        If Dir(LFilename) = "" Then
            Dim ws As Workspace
            Dim db As DAO.Database
       
            Set ws = DBEngine.Workspaces(0)
            Set db = ws.CreateDatabase(LFilename, dbLangGeneral)
            db.Close
            Set db = Nothing
        End If
   
'***********ส่วนที่ 3 การส่งออกข้อมูลไปยังไฟล์เป้าหมาย********************
        DoCmd.TransferDatabase acExport, "Microsoft Access", LFilename, acTable, "Q1", "Table1", False
    End If
End Sub

ผมมีตารางชื่อ xFake อยากส่งออกไปไว้ d:\ชื่อตารางเดิมแต่นามสกุลเป็น .xls เอ็กเซลปี 2003 ครับ ทับไฟล์เก่าได้เลยถ้ามีอยู่แล้ว ตอนนี้ผมลองใส่ 'เพื่อไม่ต้อง dim แต่ก็เจอบักแถมเหลืองไม่ผ่านสักทีครับ

ขอบคุณครับ

 

18 ก.พ. 63 , 18:15:26
ตอบกลับ #1

PNR

อ้างถึง
ผมมีตารางชื่อ xFake อยากส่งออกไปไว้ d:\ชื่อตารางเดิมแต่นามสกุลเป็น .xls เอ็กเซลปี 2003 ครับ ทับไฟล์เก่าได้เลยถ้ามีอยู่แล้ว

ถ้าไม่เอาการเรียก ตำแหน่งจะวางก็ใส่แค่นี้พอครับ
โค๊ด: [Select]
private Sub Command0_Click()
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "xfake", "D:\xfake.xls", True
end sub

ถ้าอยากระบุตำแหน่งด้วยก็แบบนี้

โค๊ด: [Select]
Private Sub Command0_Click()
    Const msoFileDialogFilePicker = 3
    Const msoFileDialogViewDetails = 2
 
    Dim fd As Object
    Dim varItems As Variant
    Dim flPath As Variant
    Dim LFilename As String

    Set fd = Application.FileDialog(msoFileDialogViewDetails)
    With fd
        .Title = "Save As"
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewDetails
        .InitialFileName = "Xfake"
    End With

    If fd.Show = True Then
        For Each varItems In fd.SelectedItems
            flPath = varItems
        Next
    End If
    LFilename = flPath

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "xfake", LFilename, True

End Sub
« แก้ไขครั้งสุดท้าย: 18 ก.พ. 63 , 18:28:43 โดย PNR »
Time to stop for me  :dizzy:
 


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


 

Sitemap 1 2 3 4 5