2011-10-26 29 views
1

我發現了一個非常好的示例,說明如何從經典ASP文件中獲取數據並將其吐出到新的MS Access文件中。我的一個問題是我需要將列的「必需」屬性設置爲false,以便如果該列中沒有數據存在,它不會崩潰。谷歌搜索讓我得到不同的語法結果,它似乎並沒有工作。如果有人可以幫助我設置這個例子的財產,我會很感激。將VBScript中的「Required = False」設置爲MS Access文件

dim provider : provider = "microsoft.jet.oledb.4.0" 
dim db : db = "c:\Documents and Settings\*****\Desktop\foodb.mdb" 
dim ds : ds = "provider=" & provider & "; data source=" & db 

dim catalog:set catalog=createobject("adox.catalog") 
catalog.create ds 'create db file 

'const for column types 
const adInteger=3 'integer 
const adVarChar=202 'variable character 

dim new_table:set new_table=createobject("adox.table") 
new_table.Name="customer" 
new_table.columns.append "id", adInteger 
new_table.columns.append "surname", adVarChar 

new_table.keys.append "pk_cust_id", 1, "id" 'primary key 

catalog.Tables.Append new_table 'append table to DB 

'release resources 
set new_table=nothing 
set catalog=nothing 

'By this point, DB is now created 

'populate table 

dim conn: set conn=createobject("adodb.connection") 'create connection 
conn.open ds 'open connection 

sql="insert into customer (id, surname) values (5, 'smith')" 
conn.Execute sql 

'close connection and reclaim resources 
    conn.close 
set conn=nothing 
+0

我沒有時間更改代碼並對其進行測試,但請在此處查看:http://allenbrowne.com/func-adox.html如果需要,請在頁面上搜索單詞「必需」 – HK1

回答

1

這個VBScript做我認爲你想要的,但它更簡潔的IMO。

Option Explicit 
Dim provider: provider = "microsoft.jet.oledb.4.0" 
Dim db: db = "C:\Access\webforums\foodb.mdb" 
Dim ds: ds = "provider=" & provider & "; data source=" & db 
Dim Sql 
Dim CreateTable 
Dim catalog: Set catalog = CreateObject("adox.catalog") 

catalog.create ds 'create db file 
Set catalog = Nothing 

'By this point, DB is now created 
'create and populate table 

Dim conn: Set conn = CreateObject("adodb.connection") 'create connection 
conn.Open ds 'open connection 
CreateTable = "CREATE TABLE customer (" & _ 
    "id COUNTER CONSTRAINT pk_cust_id PRIMARY KEY, " & _ 
    "surname TEXT(255));" 
conn.Execute CreateTable 
Sql = "insert into customer (id, surname) values (5, 'smith')" 
conn.Execute Sql 

'close connection and reclaim resources 
conn.Close 
Set conn = Nothing 

使用此方法,surname字段的屬性包括Required = No,這是我認爲你想要的。

如果你的意思是你想要在id字段中插入Null,那麼id不能作爲主鍵發生。主鍵約束不允許空值。

相關問題