สวัสดีอาจารย์และเพื่อนๆ ครับ
ผมกำลังหัดสร้างฐานข้อมูล Access โดยสั่งจาก Excel และสร้างตารางในฐานข้อมูลด้วย แต่ติดตรงบรรทัด
aCn.Execute "CREATE TABLE Transaction (FirstName CHAR, LastName CHAR)" ไม่สามารถสร้างตารางในไฟล์ฐานข้อมูลได้ครับ
อยากสอบถามคือ
1. ผมต้องปรับโค้ดอย่างไรเพื่อสร้างตารางในฐานข้อมูล
2. หากผมแค่ต้องการเก็บข้อมูลเฉยๆ (ประมาณ 30-40 คอลัมน์ และจำนวนแถวไม่จำกัด) ผมควรเลือกใช้ไฟล์ .mdb หรือ .accdb ดีครับ
(ผมลองอ่านจากไมโครซอฟท์ ว่าไฟล์ 2 แบบนี้ต่างกันยังไง อ่านแล้วก็ไม่เข้าใจครับ)
โค้ดอยู่ในโมดูล1 ในไฟล์แนบครับ
Code: Select all
Private Sub CreateDbFile()
Dim objAccess As Access.Application, aCn As ADODB.Connection _
, strCnString As String
strPath = ThisWorkbook.Path & "\dbstk"
'check if the database folder is existed
'if it not, create it
If Len(Dir(strPath, vbDirectory + vbHidden)) = 0 Then
Call MkDbFolder
End If
'check if actual database file is available
'if not, create Access database and some tables
strPath = strPath & "\" & Format(Now(), "[$- ]yyyy")
If Len(Dir(strPath & "*")) = 0 Then
Set objAccess = New Access.Application
Call objAccess.NewCurrentDatabase(strPath)
objAccess.Quit
strCnString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" _
& strPath & ".accdb;"
Set aCn = New ADODB.Connection
aCn.Open strCnString
aCn.Execute "CREATE TABLE Transaction (" _
& "FirstName CHAR, LastName CHAR)"
aCn.Close
End If
Set objAccess = Nothing
strPath = ""
End Sub
ทำไมมันยากจริง

ขอขอบคุณล่วงหน้าครับ